Example usage for javax.swing.text JTextComponent getKeymap

List of usage examples for javax.swing.text JTextComponent getKeymap

Introduction

In this page you can find the example usage for javax.swing.text JTextComponent getKeymap.

Prototype

public Keymap getKeymap() 

Source Link

Document

Fetches the keymap currently active in this text component.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JTextComponent component = new JTextField();
    if (component instanceof JTextComponent) {
        JTextComponent textComp = (JTextComponent) component;
        Keymap keymap = textComp.getKeymap();

        while (keymap != null) {
            Action[] actions = keymap.getBoundActions();
            for (int i = 0; i < actions.length; i++) {
                Action action = actions[i];
            }//from  w w w.  java 2 s . co  m
            Action defaultAction = keymap.getDefaultAction();
            keymap = keymap.getResolveParent();
        }
    }
}

From source file:Main.java

public static Action findDefaultAction(JTextComponent c) {
    Keymap kmap = c.getKeymap();
    if (kmap.getDefaultAction() != null) {
        return kmap.getDefaultAction();
    }/*from  w w w .  j  av  a2  s  .c o m*/

    kmap = kmap.getResolveParent();
    while (kmap != null) {
        if (kmap.getDefaultAction() != null) {
            return kmap.getDefaultAction();
        }
        kmap = kmap.getResolveParent();
    }
    return null;
}

From source file:com.mirth.connect.client.ui.Mirth.java

private static void keyMapBindings(JTextComponent comp, KeyBinding[] bindings) {
    JTextComponent.loadKeymap(comp.getKeymap(), bindings, comp.getActions());
}

From source file:net.pandoragames.far.ui.swing.dialog.SubWindow.java

/**
 * Registers an event listener for the Enter key on some text component.
 * /*  w w w  .  j a va2  s  .c om*/
 * @param component on which to register
 * @param action what is registered
 */
protected void registerEnterKeyListener(JTextComponent component, Action action) {
    component.getKeymap().addActionForKeyStroke(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), action);
}

From source file:net.sf.jabref.gui.keyboard.EmacsKeyBindings.java

/**
 * Activates Emacs keybindings for all text components extending {@link
 * JTextComponent}.//from ww  w  . ja va  2 s. c om
 */
private static void loadEmacsKeyBindings() {
    EmacsKeyBindings.LOGGER.debug("Loading emacs keybindings");

    for (JTextComponent jtc : EmacsKeyBindings.JTCS) {
        Action[] origActions = jtc.getActions();
        Action[] actions = new Action[origActions.length + EmacsKeyBindings.EMACS_ACTIONS.length];
        System.arraycopy(origActions, 0, actions, 0, origActions.length);
        System.arraycopy(EmacsKeyBindings.EMACS_ACTIONS, 0, actions, origActions.length,
                EmacsKeyBindings.EMACS_ACTIONS.length);

        Keymap k = jtc.getKeymap();

        JTextComponent.KeyBinding[] keybindings;
        boolean rebindCA = JabRefPreferences.getInstance()
                .getBoolean(JabRefPreferences.EDITOR_EMACS_KEYBINDINGS_REBIND_CA);
        boolean rebindCF = JabRefPreferences.getInstance()
                .getBoolean(JabRefPreferences.EDITOR_EMACS_KEYBINDINGS_REBIND_CF);
        if (rebindCA || rebindCF) {
            // if we additionally rebind C-a or C-f, we have to add the shortcuts to EmacsKeyBindings.EMACS_KEY_BINDINGS_BASE

            // determine size of new array and position of the new key bindings in the array
            int size = EmacsKeyBindings.EMACS_KEY_BINDINGS_BASE.length;
            int posCA = -1;
            int posCF = -1;
            if (rebindCA) {
                posCA = size;
                size++;
            }
            if (rebindCF) {
                posCF = size;
                size++;
            }

            // generate new array
            keybindings = new JTextComponent.KeyBinding[size];
            System.arraycopy(EmacsKeyBindings.EMACS_KEY_BINDINGS_BASE, 0, keybindings, 0,
                    EmacsKeyBindings.EMACS_KEY_BINDINGS_BASE.length);
            if (rebindCA) {
                keybindings[posCA] = EmacsKeyBindings.EMACS_KEY_BINDING_C_A;
            }
            if (rebindCF) {
                keybindings[posCF] = EmacsKeyBindings.EMACS_KEY_BINDING_C_F;
            }
        } else {
            keybindings = EmacsKeyBindings.EMACS_KEY_BINDINGS_BASE;
        }
        JTextComponent.loadKeymap(k, keybindings, actions);
    }
}