Search in sources :

Example 1 with Keymap

use of javax.swing.text.Keymap in project jabref by JabRef.

the class EmacsKeyBindings method createBackup.

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;
    }
    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());
    }
}
Also used : Action(javax.swing.Action) TextAction(javax.swing.text.TextAction) KeyStroke(javax.swing.KeyStroke) JTextComponent(javax.swing.text.JTextComponent) Keymap(javax.swing.text.Keymap)

Example 2 with Keymap

use of javax.swing.text.Keymap in project jabref by JabRef.

the class EmacsKeyBindings method unload.

/**
     * Restores the original keybindings for the concrete subclasses of
     * {@link JTextComponent}.
     */
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());
        }
    }
}
Also used : Action(javax.swing.Action) TextAction(javax.swing.text.TextAction) KeyStroke(javax.swing.KeyStroke) Keymap(javax.swing.text.Keymap)

Example 3 with Keymap

use of javax.swing.text.Keymap in project jabref by JabRef.

the class EmacsKeyBindings method loadEmacsKeyBindings.

/**
     * Activates Emacs keybindings for all text components extending {@link
     * JTextComponent}.
     */
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);
    }
}
Also used : Action(javax.swing.Action) TextAction(javax.swing.text.TextAction) JTextComponent(javax.swing.text.JTextComponent) Keymap(javax.swing.text.Keymap)

Example 4 with Keymap

use of javax.swing.text.Keymap in project jabref by JabRef.

the class JTextAreaWithHighlighting method setupUndoRedo.

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
        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(org.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(org.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");
    }
}
Also used : UndoManager(javax.swing.undo.UndoManager) ActionEvent(java.awt.event.ActionEvent) CannotRedoException(javax.swing.undo.CannotRedoException) KeyStroke(javax.swing.KeyStroke) CannotUndoException(javax.swing.undo.CannotUndoException) Document(javax.swing.text.Document) AbstractAction(javax.swing.AbstractAction) Keymap(javax.swing.text.Keymap)

Example 5 with Keymap

use of javax.swing.text.Keymap in project adempiere by adempiere.

the class CompiereTextAreaUI method createKeymap.

/**
     * 	Create Keymap
     *	@return key Map
     */
protected Keymap createKeymap() {
    Keymap map = super.createKeymap();
    map.addActionForKeyStroke(s_stroke, s_action);
    return map;
}
Also used : Keymap(javax.swing.text.Keymap)

Aggregations

Keymap (javax.swing.text.Keymap)5 Action (javax.swing.Action)3 KeyStroke (javax.swing.KeyStroke)3 TextAction (javax.swing.text.TextAction)3 JTextComponent (javax.swing.text.JTextComponent)2 ActionEvent (java.awt.event.ActionEvent)1 AbstractAction (javax.swing.AbstractAction)1 Document (javax.swing.text.Document)1 CannotRedoException (javax.swing.undo.CannotRedoException)1 CannotUndoException (javax.swing.undo.CannotUndoException)1 UndoManager (javax.swing.undo.UndoManager)1