Java Code Examples for org.openide.util.Utilities#stringToKey()

The following examples show how to use org.openide.util.Utilities#stringToKey() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ShortcutListener.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private void addKeyStroke(KeyStroke keyStroke, boolean add) {
    String s = Utilities.keyToString(keyStroke, true);
    KeyStroke mappedStroke = Utilities.stringToKey(s);
    if (!keyStroke.equals(mappedStroke)) {
        return;
    }
    String k = KeyStrokeUtils.getKeyStrokeAsText(keyStroke);
    // check if the text can be mapped back
    if (key.equals("")) { //NOI18N
        textField.setText(k);
        if (add)
            key = k;
    } else {
        textField.setText(key + " " + k); //NOI18N
        if (add)
            key += " " + k; //NOI18N
    }
}
 
Example 2
Source File: WizardUtils.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public static KeyStroke stringToKeyStroke(String keyStroke) {
    int modifiers = 0;
    if (keyStroke.startsWith("Ctrl+")) { // NOI18N
        modifiers |= InputEvent.CTRL_DOWN_MASK;
        keyStroke = keyStroke.substring(5);
    }
    if (keyStroke.startsWith("Alt+")) { // NOI18N
        modifiers |= InputEvent.ALT_DOWN_MASK;
        keyStroke = keyStroke.substring(4);
    }
    if (keyStroke.startsWith("Shift+")) { // NOI18N
        modifiers |= InputEvent.SHIFT_DOWN_MASK;
        keyStroke = keyStroke.substring(6);
    }
    if (keyStroke.startsWith("Meta+")) { // NOI18N
        modifiers |= InputEvent.META_DOWN_MASK;
        keyStroke = keyStroke.substring(5);
    }
    KeyStroke ks = Utilities.stringToKey(keyStroke);
    if (ks == null) {
        return null;
    }
    KeyStroke result = KeyStroke.getKeyStroke(ks.getKeyCode(), modifiers);
    return result;
}
 
Example 3
Source File: EditorSettingsStorageTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private void checkKeyBinding(MultiKeyBinding kb, String... keyStrokes) {
    assertNotNull("Key binding should not be null", kb);
    
    ArrayList<KeyStroke> list = new ArrayList<KeyStroke>();
    for(String s : keyStrokes) {
        KeyStroke stroke = Utilities.stringToKey(s);
        if (stroke != null) {
            list.add(stroke);
        }
    }
    
    assertEquals("Wrong number of key strokes", list.size(), kb.getKeyStrokeCount());
    for(int i = 0; i < list.size(); i++) {
        assertEquals("KeyStroke[" + i + "] is different", 
            list.get(i), kb.getKeyStroke(i));
    }        
}
 
Example 4
Source File: EditorBridge.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private static KeyStroke[] stringToKeyStrokes2(String key) {
    List<KeyStroke> result = new ArrayList<KeyStroke>();

    for (StringTokenizer st = new StringTokenizer(key, " "); st.hasMoreTokens();) { //NOI18N
        String ks = st.nextToken().trim();
        KeyStroke keyStroke = Utilities.stringToKey(ks);

        if (keyStroke == null) {
            LOG.warning("'" + ks + "' is not a valid keystroke"); //NOI18N
            return null;
        }

        result.add(keyStroke);
    }

    return result.toArray(new KeyStroke[result.size()]);
}
 
Example 5
Source File: KeyStrokeUtils.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Convert human-readable keystroke name to {@link KeyStroke} object.
 */
public static @CheckForNull KeyStroke getKeyStroke(
        @NonNull String keyStroke) {

    int modifiers = 0;
    while (true) {
        if (keyStroke.startsWith(EMACS_CTRL)) {
            modifiers |= InputEvent.CTRL_DOWN_MASK;
            keyStroke = keyStroke.substring(EMACS_CTRL.length());
        } else if (keyStroke.startsWith(EMACS_ALT)) {
            modifiers |= InputEvent.ALT_DOWN_MASK;
            keyStroke = keyStroke.substring(EMACS_ALT.length());
        } else if (keyStroke.startsWith(EMACS_SHIFT)) {
            modifiers |= InputEvent.SHIFT_DOWN_MASK;
            keyStroke = keyStroke.substring(EMACS_SHIFT.length());
        } else if (keyStroke.startsWith(EMACS_META)) {
            modifiers |= InputEvent.META_DOWN_MASK;
            keyStroke = keyStroke.substring(EMACS_META.length());
        } else if (keyStroke.startsWith(STRING_ALT)) {
            modifiers |= InputEvent.ALT_DOWN_MASK;
            keyStroke = keyStroke.substring(STRING_ALT.length());
        } else if (keyStroke.startsWith(STRING_META)) {
            modifiers |= InputEvent.META_DOWN_MASK;
            keyStroke = keyStroke.substring(STRING_META.length());
        } else {
            break;
        }
    }
    KeyStroke ks = Utilities.stringToKey (keyStroke);
    if (ks == null) { // Return null to indicate an invalid keystroke
        return null;
    } else {
        KeyStroke result = KeyStroke.getKeyStroke (ks.getKeyCode (), modifiers);
        return result;
    }
}
 
Example 6
Source File: EditorSettingsStorageTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testKeyBindingsImmutability() {
    MimePath mimePath = MimePath.parse("text/x-type-A");
    Lookup lookup = MimeLookup.getLookup(mimePath);
    
    KeyBindingSettings kbs = lookup.lookup(KeyBindingSettings.class);
    assertNotNull("Can't find KeyBindingSettings", kbs);
    
    // Check preconditions
    List<MultiKeyBinding> bindings = kbs.getKeyBindings();
    assertNotNull("Key bindings should not be null", bindings);
    MultiKeyBinding kb = findBindingForAction("test-action-1", bindings);
    checkKeyBinding(kb, "O-O");
    
    // Change the coloring
    MultiKeyBinding newKb = new MultiKeyBinding(Utilities.stringToKey("DS-D"), "test-action-1");
    setOneKeyBinding("text/x-type-A", newKb);

    // Check that the original KeyBindingSettings has not changed
    bindings = kbs.getKeyBindings();
    assertNotNull("Key bindings should not be null", bindings);
    kb = findBindingForAction("test-action-1", bindings);
    checkKeyBinding(kb, "O-O");
    
    // Check that the new attributes were set
    kbs = lookup.lookup(KeyBindingSettings.class);
    assertNotNull("Can't find KeyBindingSettings", kbs);
    bindings = kbs.getKeyBindings();
    assertNotNull("Key bindings should not be null", bindings);
    kb = findBindingForAction("test-action-1", bindings);
    checkKeyBinding(kb, "DS-D");
}
 
Example 7
Source File: CodeTemplateSettingsImpl.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public KeyStroke getExpandKey() {
    Preferences prefs = MimeLookup.getLookup(MimePath.EMPTY).lookup(Preferences.class);
    String ks = prefs.get(CODE_TEMPLATE_EXPAND_KEY, null);
    if (ks != null) {
        KeyStroke keyStroke = Utilities.stringToKey(ks);
        if (keyStroke != null) {
            return keyStroke;
        }
    }
    return DEFAULT_EXPANSION_KEY;
}
 
Example 8
Source File: RecentViewListAction.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
public void actionPerformed(ActionEvent evt) {
    boolean editors = true;
    boolean views = !documentsOnly;
    if( "immediately".equals( evt.getActionCommand() ) ) {
        TopComponent activeTc = TopComponent.getRegistry().getActivated();
        if( null != activeTc ) {
            if( TopComponentTracker.getDefault().isEditorTopComponent( activeTc ) ) {
                //switching in a document, go to some other document
                views = false;
            } else {
                //switching in a view, go to some other view
                editors = false;
                views = true;
            }
        }
    }
    
    TopComponent[] documents = getRecentWindows(editors, views);
    
    if (documents.length < 2) {
        return;
    }
    
    if(!"immediately".equals(evt.getActionCommand()) && // NOI18N
            !(evt.getSource() instanceof javax.swing.JMenuItem)) {
        // #46800: fetch key directly from action command
        KeyStroke keyStroke = Utilities.stringToKey(evt.getActionCommand());
        
        if(keyStroke != null) {
            int triggerKey = keyStroke.getKeyCode();
            int reverseKey = KeyEvent.VK_SHIFT;
            int releaseKey = 0;
            
            int modifiers = keyStroke.getModifiers();
            if((InputEvent.CTRL_MASK & modifiers) != 0) {
                releaseKey = KeyEvent.VK_CONTROL;
            } else if((InputEvent.ALT_MASK & modifiers) != 0) {
                releaseKey = KeyEvent.VK_ALT;
            } else if((InputEvent.META_MASK & modifiers) != 0) {
                releaseKey = KeyEvent.VK_META;
            }
            
            if(releaseKey != 0) {
                if (!KeyboardPopupSwitcher.isShown()) {
                    KeyboardPopupSwitcher.showPopup(documentsOnly, releaseKey, triggerKey, (evt.getModifiers() & KeyEvent.SHIFT_MASK) == 0);
                }
                return;
            }
        }
    }

    int documentIndex = (evt.getModifiers() & KeyEvent.SHIFT_MASK) == 0 ? 1 : documents.length-1;
    TopComponent tc = documents[documentIndex];
    // #37226 Unmaximized the other mode if needed.
    WindowManagerImpl wm = WindowManagerImpl.getInstance();
    ModeImpl mode = (ModeImpl) wm.findMode(tc);
    if(mode != null && mode != wm.getCurrentMaximizedMode()) {
        wm.switchMaximizedMode(null);
    }
    
    tc.requestActive();
}
 
Example 9
Source File: ToggleProfilingPointAction.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/**
 * Invoked when an action occurs.
 */
public void actionPerformed(final ActionEvent e) {
    acceleratorKeyStroke = Utilities.stringToKey(e.getActionCommand());
    if (acceleratorKeyStroke == null || acceleratorKeyStroke.getModifiers() == 0) {
        ProfilerDialogs.displayError(
                Bundle.ToggleProfilingPointAction_InvalidShortcutMsg(
                Bundle.ToggleProfilingPointAction_ActionName()));
        return;
    }
    
    if (warningDialogOpened) {
        return;
    }

    if (ProfilingPointsManager.getDefault().isProfilingSessionInProgress()) {
        warningDialogOpened = true;
        ProfilerDialogs.displayWarning(
                Bundle.ToggleProfilingPointAction_ProfilingProgressMsg());
        warningDialogOpened = false;

        return;
    }

    if (Utils.getCurrentLocation(0).equals(CodeProfilingPoint.Location.EMPTY)) {
        warningDialogOpened = true;
        ProfilerDialogs.displayWarning(
                Bundle.ToggleProfilingPointAction_BadSourceMsg());
        warningDialogOpened = false;

        return;
    }
    
    ProfilingPointsSwitcher chooserFrame = getChooserFrame();
    
    boolean toggleOff = false;
    CodeProfilingPoint.Location location = Utils.getCurrentLocation(CodeProfilingPoint.Location.OFFSET_START);
    for(CodeProfilingPoint pp : ProfilingPointsManager.getDefault().getProfilingPoints(CodeProfilingPoint.class, Utils.getCurrentProject(), false, false)) {
        if (location.equals(pp.getLocation())) {
            ProfilingPointsManager.getDefault().removeProfilingPoint(pp);
            toggleOff = true;
        }
    }
    if (!toggleOff) {
        if (chooserFrame.isVisible()) {
            nextFactory();
            chooserFrame.setProfilingPointFactory((currentFactory == ppFactories.length) ? null : ppFactories[currentFactory],
                                                  currentFactory);
        } else {
            if (EditorSupport.currentlyInJavaEditor()) {
                resetFactories();
                chooserFrame.setProfilingPointFactory((currentFactory == ppFactories.length) ? null : ppFactories[currentFactory],
                                                      currentFactory);
                Toolkit.getDefaultToolkit().addAWTEventListener(this, AWTEvent.KEY_EVENT_MASK);
                chooserFrame.setVisible(true);
            }
        }
    }
}
 
Example 10
Source File: StorageFilterTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
public void afterLoad(Map<Collection<KeyStroke>, MultiKeyBinding> map, MimePath mimePath, String profile, boolean defaults) throws IOException {
    KeyStroke key = Utilities.stringToKey("CAS-Q");
    map.put(Arrays.asList(key), new MultiKeyBinding(key, "filterB-injected-action-1"));
}
 
Example 11
Source File: StorageFilterTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
public void beforeSave(Map<Collection<KeyStroke>, MultiKeyBinding> map, MimePath mimePath, String profile, boolean defaults) throws IOException {
    KeyStroke key = Utilities.stringToKey("CAS-Q");
    map.remove(Arrays.asList(key));
}
 
Example 12
Source File: ThreadsHistoryAction.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
public void actionPerformed(ActionEvent evt) {
    List<DVThread> threads = getThreads();
    int threadsCount = threads.size();
    if (threadsCount < 1) {
        Toolkit.getDefaultToolkit().beep();
        return;
    }
    
    if(!"immediately".equals(evt.getActionCommand()) && // NOI18N
            !(evt.getSource() instanceof javax.swing.JMenuItem)) {
        // #46800: fetch key directly from action command
        KeyStroke keyStroke = Utilities.stringToKey(evt.getActionCommand());
        
        if(keyStroke != null) {
            int triggerKey = keyStroke.getKeyCode();
            int releaseKey = 0;
            
            int modifiers = keyStroke.getModifiers();
            if((InputEvent.CTRL_MASK & modifiers) != 0) {
                releaseKey = KeyEvent.VK_CONTROL;
            } else if((InputEvent.ALT_MASK & modifiers) != 0) {
                releaseKey = KeyEvent.VK_ALT;
            } else if((InputEvent.META_MASK & modifiers) != 0) {
                releaseKey = InputEvent.META_MASK;
            }
            
            if(releaseKey != 0) {
                if (!KeyboardPopupSwitcher.isShown()) {
                    KeyboardPopupSwitcher.selectItem(
                            createSwitcherItems(threads),
                            releaseKey, triggerKey, true, true); // (evt.getModifiers() & KeyEvent.SHIFT_MASK) == 0
                }
                return;
            }
        }
    }
    
    if (threadsCount == 1) {
        threads.get(0).makeCurrent();
    } else {
        int index = (evt.getModifiers() & KeyEvent.SHIFT_MASK) == 0 ? 1 : threadsCount - 1;
        threads.get(index).makeCurrent();
    }
}