Search in sources :

Example 1 with UndoManager

use of javax.swing.undo.UndoManager in project jabref by JabRef.

the class TextField 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 ctl-Y
    getInputMap().put(Globals.getKeyPrefs().getKey(org.jabref.gui.keyboard.KeyBinding.REDO), "Redo");
}
Also used : UndoManager(javax.swing.undo.UndoManager) ActionEvent(java.awt.event.ActionEvent) CannotRedoException(javax.swing.undo.CannotRedoException) CannotUndoException(javax.swing.undo.CannotUndoException) Document(javax.swing.text.Document) AbstractAction(javax.swing.AbstractAction)

Example 2 with UndoManager

use of javax.swing.undo.UndoManager 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 3 with UndoManager

use of javax.swing.undo.UndoManager in project intellij-community by JetBrains.

the class UIUtil method addUndoRedoActions.

public static void addUndoRedoActions(@NotNull final JTextComponent textComponent) {
    if (textComponent.getClientProperty(UNDO_MANAGER) instanceof UndoManager) {
        return;
    }
    UndoManager undoManager = new UndoManager();
    textComponent.putClientProperty(UNDO_MANAGER, undoManager);
    textComponent.getDocument().addUndoableEditListener(undoManager);
    textComponent.getDocument().addDocumentListener(SET_TEXT_CHECKER);
    textComponent.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_Z, SystemInfo.isMac ? InputEvent.META_MASK : InputEvent.CTRL_MASK), "undoKeystroke");
    textComponent.getActionMap().put("undoKeystroke", UNDO_ACTION);
    textComponent.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_Z, (SystemInfo.isMac ? InputEvent.META_MASK : InputEvent.CTRL_MASK) | InputEvent.SHIFT_MASK), "redoKeystroke");
    textComponent.getActionMap().put("redoKeystroke", REDO_ACTION);
}
Also used : UndoManager(javax.swing.undo.UndoManager)

Aggregations

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