Java Code Examples for javax.swing.text.Keymap#addActionForKeyStroke()

The following examples show how to use javax.swing.text.Keymap#addActionForKeyStroke() . 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: ContextAwareActionInTopComponentTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
protected void setUp () throws Exception {
    tc = new TopComponent ();
    tc.requestActive();
    
    MockServices.setServices( MyKeymap.class );
    Keymap km = Lookup.getDefault().lookup(Keymap.class);
    km.addActionForKeyStroke( KEY_STROKE, myGlobalAction );

    MyContextAwareAction.globalActionWasPerformed = false;
    MyContextAwareAction.contextActionWasPerformed = false;
}
 
Example 2
Source File: MultiKeymap.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/** Loads key to action mappings into this keymap
* @param bindings array of bindings
* @param actions map of [action_name, action] pairs
*/
public void load(JTextComponent.KeyBinding[] bindings, Map actions) {
    // now create bindings in keymap(s)
    for (int i = 0; i < bindings.length; i++) {
        Action a = (Action)actions.get(bindings[i].actionName);
        if (a != null) {
            boolean added = false;
            if (bindings[i] instanceof MultiKeyBinding) {
                MultiKeyBinding mb = (MultiKeyBinding)bindings[i];
                if (mb.keys != null) {
                    Keymap cur = delegate;
                    for (int j = 0; j < mb.keys.length; j++) {
                        if (j == mb.keys.length - 1) { // last keystroke in sequence
                            cur.addActionForKeyStroke(mb.keys[j], a);
                        } else { // not the last keystroke
                            Action sca = cur.getAction(mb.keys[j]);
                            if (!(sca instanceof KeymapSetContextAction)) {
                                sca = new KeymapSetContextAction(JTextComponent.addKeymap(null, null));
                                cur.addActionForKeyStroke(mb.keys[j], sca);
                            }
                            cur = ((KeymapSetContextAction)sca).contextKeymap;
                        }
                    }
                    added = true;
                }
            }
            if (!added) {
                if (bindings[i].key != null) {
                    delegate.addActionForKeyStroke(bindings[i].key, a);
                } else { // key is null -> set default action
                    setDefaultAction(a);
                }
            }
        }
    }
}
 
Example 3
Source File: DefaultSyntaxKit.java    From visualvm with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Add keyboard actions to this control using the Configuration we have
 * @param map
 * @param prefix 
 */
public void addSyntaxActions(Keymap map, String prefix) {
    // look at all keys that either start with prefix.Action, or
    // that start with Action.

    Pattern splitter = CONFIG.getValueSeparator(prefix);
    Configuration actionsConf = CONFIG.subConfig(prefix, "Action.");

    for (String actionName : actionsConf.stringPropertyNames()) {
        String[] values = splitter.split(
                actionsConf.getProperty(actionName));
        String actionClass = values[0];
        SyntaxAction action = editorActions.get(actionClass);
        if (action == null) {
            action = createAction(actionClass);
            action.config(CONFIG, prefix, actionName);
        }
        String keyStrokeString = values[1];
        KeyStroke ks = KeyStroke.getKeyStroke(keyStrokeString);
        // KeyEvent.VK_QUOTEDBL
        if (ks == null) {
            throw new IllegalArgumentException("Invalid KeyStroke: " +
                    keyStrokeString);
        }
        TextAction ta = action.getAction(actionName);
        if(ta == null) {
            throw new IllegalArgumentException("Invalid ActionName: " +
                    actionName);
        }
        map.addActionForKeyStroke(ks, ta);
    }
}
 
Example 4
Source File: TextPanel.java    From BotLibre with Eclipse Public License 1.0 4 votes vote down vote up
public void initKeyMap(JTextArea text) {
	Keymap keyMap = JTextArea.addKeymap("EnterSubmit",  text.getKeymap());
	KeyStroke key = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);
	keyMap.addActionForKeyStroke(key, new SubmitAction());
	text.setKeymap(keyMap);
}
 
Example 5
Source File: HttpPanel.java    From BotLibre with Eclipse Public License 1.0 4 votes vote down vote up
public void initKeyMap(JTextArea text) {
	Keymap keyMap = JTextArea.addKeymap("EnterSubmit",  text.getKeymap());
	KeyStroke key = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);
	keyMap.addActionForKeyStroke(key, new SubmitAction());
	text.setKeymap(keyMap);
}
 
Example 6
Source File: ContextPanel.java    From BotLibre with Eclipse Public License 1.0 4 votes vote down vote up
public void initKeyMap(JTextArea text) {
	Keymap keyMap = JTextArea.addKeymap("EnterSubmit",  text.getKeymap());
	KeyStroke key = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);
	keyMap.addActionForKeyStroke(key, new RefreshAction());
	text.setKeymap(keyMap);
}