Example usage for javax.swing.text Keymap getBoundKeyStrokes

List of usage examples for javax.swing.text Keymap getBoundKeyStrokes

Introduction

In this page you can find the example usage for javax.swing.text Keymap getBoundKeyStrokes.

Prototype

public KeyStroke[] getBoundKeyStrokes();

Source Link

Document

Fetches all of the keystrokes in this map that are bound to some action.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JTextArea component = new JTextArea();
    Keymap map = component.getKeymap();

    while (map != null) {
        KeyStroke[] keys = map.getBoundKeyStrokes();

        for (int i = 0; i < keys.length; i++) {
            System.out.println(keys[i].getKeyChar());
            Action action = (Action) map.getAction(keys[i]);
            System.out.println(action);
        }//from ww  w. j  av  a  2  s . c  o  m
        Action defAction = map.getDefaultAction();
        System.out.println(defAction);
        map = map.getResolveParent();
    }
}

From source file:net.sf.jabref.gui.fieldeditors.JTextAreaWithHighlighting.java

private void setupUndoRedo() {
    undo = new UndoManager();
    Document doc = getDocument();

    // Listen for undo and redo events
    doc.addUndoableEditListener(evt -> undo.addEdit(evt.getEdit()));

    // Create an undo action and add it to the text component
    getActionMap().put("Undo", new AbstractAction("Undo") {

        @Override/* www .  ja  va 2 s .  c om*/
        public void actionPerformed(ActionEvent evt) {
            try {
                if (undo.canUndo()) {
                    undo.undo();
                }
            } catch (CannotUndoException ignored) {
                // Ignored
            }
        }
    });

    // Bind the undo action to ctl-Z
    getInputMap().put(Globals.getKeyPrefs().getKey(net.sf.jabref.gui.keyboard.KeyBinding.UNDO), "Undo");

    // Create a redo action and add it to the text component
    getActionMap().put("Redo", new AbstractAction(Actions.REDO) {

        @Override
        public void actionPerformed(ActionEvent evt) {
            try {
                if (undo.canRedo()) {
                    undo.redo();
                }
            } catch (CannotRedoException ignored) {
                // Ignored
            }
        }
    });

    // Bind the redo action to ctrl-Y
    boolean bind = true;
    KeyStroke redoKey = Globals.getKeyPrefs().getKey(net.sf.jabref.gui.keyboard.KeyBinding.REDO);
    if (Globals.prefs.getBoolean(JabRefPreferences.EDITOR_EMACS_KEYBINDINGS)) {
        // If emacs is enabled, check if we have a conflict at keys
        // If yes, do not bind
        // Typically, we have: CTRL+y is "yank" in emacs and REDO in 'normal' settings
        // The Emacs key bindings are stored in the keymap, not in the input map.
        Keymap keymap = getKeymap();
        KeyStroke[] keys = keymap.getBoundKeyStrokes();
        int i = 0;
        while ((i < keys.length) && !keys[i].equals(redoKey)) {
            i++;
        }
        if (i < keys.length) {
            // conflict found -> do not bind
            bind = false;
        }
    }
    if (bind) {
        getInputMap().put(redoKey, "Redo");
    }
}