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 static Keymap getKeymap(String nm) 

Source Link

Document

Fetches a named keymap previously added to the document.

Usage

From source file:Main.java

/**
 * initialize keystroke bindings/*from ww w  .ja va 2 s .  c o m*/
 */
public final static void InitializeKeyStrokeBindings() {
    String selectAllAction = DefaultEditorKit.selectAllAction;
    String cutAction = DefaultEditorKit.cutAction;
    String copyAction = DefaultEditorKit.copyAction;
    String pasteAction = DefaultEditorKit.pasteAction;

    int mask = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();

    JTextComponent.KeyBinding ctrlA = new JTextComponent.KeyBinding(KeyStroke.getKeyStroke(KeyEvent.VK_A, mask),
            selectAllAction);
    JTextComponent.KeyBinding ctrlX = new JTextComponent.KeyBinding(KeyStroke.getKeyStroke(KeyEvent.VK_X, mask),
            cutAction);
    JTextComponent.KeyBinding ctrlC = new JTextComponent.KeyBinding(KeyStroke.getKeyStroke(KeyEvent.VK_C, mask),
            copyAction);
    JTextComponent.KeyBinding ctrlV = new JTextComponent.KeyBinding(KeyStroke.getKeyStroke(KeyEvent.VK_V, mask),
            pasteAction);

    JTextComponent.KeyBinding[] extraBindings = new JTextComponent.KeyBinding[] { ctrlA, ctrlX, ctrlC, ctrlV };

    Keymap defaultKeyMap = JTextComponent.getKeymap(JTextComponent.DEFAULT_KEYMAP);

    JTextComponent dummy = new JTextField();
    JTextComponent.loadKeymap(defaultKeyMap, extraBindings, dummy.getActions());
}

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

private static void createBackup() {
    Keymap oldBackup = JTextComponent.getKeymap(EmacsKeyBindings.JTCS[0].getClass().getName());
    if (oldBackup != null) {
        // if there is already a backup, do not create a new backup
        return;/*  w  w  w. j  a v a  2s  .c om*/
    }

    for (JTextComponent jtc : EmacsKeyBindings.JTCS) {
        Keymap orig = jtc.getKeymap();
        Keymap backup = JTextComponent.addKeymap(jtc.getClass().getName(), null);
        Action[] bound = orig.getBoundActions();
        for (Action aBound : bound) {
            KeyStroke[] strokes = orig.getKeyStrokesForAction(aBound);
            for (KeyStroke stroke : strokes) {
                backup.addActionForKeyStroke(stroke, aBound);
            }
        }
        backup.setDefaultAction(orig.getDefaultAction());
    }
}

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

/**
 * Restores the original keybindings for the concrete subclasses of
 * {@link JTextComponent}.// w w  w  .  j a v  a2 s  .c  om
 *
 */
public static void unload() {
    for (int i = 0; i < EmacsKeyBindings.JTCS.length; i++) {
        Keymap backup = JTextComponent.getKeymap(EmacsKeyBindings.JTCS[i].getClass().getName());

        if (backup != null) {
            Keymap current = EmacsKeyBindings.JTCS[i].getKeymap();
            current.removeBindings();

            Action[] bound = backup.getBoundActions();
            for (Action aBound : bound) {
                KeyStroke[] strokes = backup.getKeyStrokesForAction(bound[i]);
                for (KeyStroke stroke : strokes) {
                    current.addActionForKeyStroke(stroke, aBound);
                }
            }
            current.setDefaultAction(backup.getDefaultAction());
        }
    }
}