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());
}
}
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());
}
}
}
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);
}
}
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");
}
}
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;
}
Aggregations