List of usage examples for javax.swing.text Document addUndoableEditListener
public void addUndoableEditListener(UndoableEditListener listener);
From source file:Main.java
public static void main(String[] argv) { JTextComponent textcomp = new JTextArea(); final UndoManager undo = new UndoManager(); Document doc = textcomp.getDocument(); doc.addUndoableEditListener(new UndoableEditListener() { public void undoableEditHappened(UndoableEditEvent evt) { undo.addEdit(evt.getEdit()); }/*www . j a v a2s. c o m*/ }); textcomp.getActionMap().put("Undo", new AbstractAction("Undo") { public void actionPerformed(ActionEvent evt) { try { if (undo.canUndo()) { undo.undo(); } } catch (CannotUndoException e) { } } }); JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(new JScrollPane(textcomp)); frame.setSize(380, 320); frame.setLocationRelativeTo(null); frame.setVisible(true); }
From source file:Main.java
public static void main(String[] argv) { JTextComponent textcomp = new JTextArea(); final UndoManager undo = new UndoManager(); Document doc = textcomp.getDocument(); doc.addUndoableEditListener(new UndoableEditListener() { public void undoableEditHappened(UndoableEditEvent evt) { undo.addEdit(evt.getEdit()); }// w ww .j a va2 s . co m }); textcomp.getActionMap().put("Undo", new AbstractAction("Undo") { public void actionPerformed(ActionEvent evt) { try { if (undo.canUndo()) { undo.undo(); } } catch (CannotUndoException e) { } } }); textcomp.getInputMap().put(KeyStroke.getKeyStroke("control Z"), "Undo"); JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(new JScrollPane(textcomp)); frame.setSize(380, 320); frame.setLocationRelativeTo(null); frame.setVisible(true); }
From source file:Main.java
public static void main(String[] argv) throws Exception { JTextComponent textcomp = new JTextArea(); final UndoManager undo = new UndoManager(); Document doc = textcomp.getDocument(); doc.addUndoableEditListener(new UndoableEditListener() { public void undoableEditHappened(UndoableEditEvent evt) { undo.addEdit(evt.getEdit()); }// w w w . j a va2 s. c o m }); textcomp.getActionMap().put("Undo", new AbstractAction("Undo") { public void actionPerformed(ActionEvent evt) { try { if (undo.canUndo()) { undo.undo(); } } catch (CannotUndoException e) { } } }); textcomp.getInputMap().put(KeyStroke.getKeyStroke("control Z"), "Undo"); textcomp.getActionMap().put("Redo", new AbstractAction("Redo") { public void actionPerformed(ActionEvent evt) { try { if (undo.canRedo()) { undo.redo(); } } catch (CannotRedoException e) { } } }); textcomp.getInputMap().put(KeyStroke.getKeyStroke("control Y"), "Redo"); }
From source file:Main.java
public static void main(String[] argv) { JTextComponent textcomp = new JTextArea(); final UndoManager undo = new UndoManager(); Document doc = textcomp.getDocument(); doc.addUndoableEditListener(new UndoableEditListener() { public void undoableEditHappened(UndoableEditEvent evt) { undo.addEdit(evt.getEdit()); }//from w w w . ja va 2 s . c o m }); textcomp.getActionMap().put("Undo", new AbstractAction("Undo") { public void actionPerformed(ActionEvent evt) { try { if (undo.canUndo()) { undo.undo(); } } catch (CannotUndoException e) { } } }); textcomp.getInputMap().put(KeyStroke.getKeyStroke("control Z"), "Undo"); textcomp.getActionMap().put("Redo", new AbstractAction("Redo") { public void actionPerformed(ActionEvent evt) { try { if (undo.canRedo()) { undo.redo(); } } catch (CannotRedoException e) { } } }); textcomp.getInputMap().put(KeyStroke.getKeyStroke("control Y"), "Redo"); JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(new JScrollPane(textcomp)); frame.setSize(380, 320); frame.setLocationRelativeTo(null); frame.setVisible(true); }
From source file:Main.java
public static void main(String[] argv) throws Exception { JTextComponent textcomp = new JTextArea(); final UndoManager undo = new UndoManager(); Document doc = textcomp.getDocument(); JFrame f = new JFrame(); f.add(new JScrollPane(textcomp)); f.setSize(330, 300);//from www . j a v a 2 s . c o m f.setVisible(true); doc.addUndoableEditListener(new UndoableEditListener() { public void undoableEditHappened(UndoableEditEvent evt) { undo.addEdit(evt.getEdit()); } }); textcomp.getActionMap().put("Undo", new AbstractAction("Undo") { public void actionPerformed(ActionEvent evt) { try { if (undo.canUndo()) { undo.undo(); } } catch (CannotUndoException e) { } } }); textcomp.getInputMap().put(KeyStroke.getKeyStroke("control Z"), "Undo"); textcomp.getActionMap().put("Redo", new AbstractAction("Redo") { public void actionPerformed(ActionEvent evt) { try { if (undo.canRedo()) { undo.redo(); } } catch (CannotRedoException e) { } } }); textcomp.getInputMap().put(KeyStroke.getKeyStroke("control Y"), "Redo"); }
From source file:Main.java
@SuppressWarnings("serial") public static void installUndoManager(JTextComponent textComponent, final UndoManager undoManager) { Document doc = textComponent.getDocument(); doc.addUndoableEditListener(new UndoableEditListener() { public void undoableEditHappened(UndoableEditEvent e) { undoManager.addEdit(e.getEdit()); }/*from w w w . j av a2 s. c o m*/ }); ActionMap am = textComponent.getActionMap(); InputMap im = textComponent.getInputMap(); am.put("undo", new AbstractAction("undo") { @Override public void actionPerformed(ActionEvent e) { undoManager.undo(); } @Override public boolean isEnabled() { return undoManager.canUndo(); } }); im.put(KeyStroke.getKeyStroke(KeyEvent.VK_Z, getMenuShortcutKeyMask()), "undo"); am.put("redo", new AbstractAction("redo") { @Override public void actionPerformed(ActionEvent e) { undoManager.redo(); } @Override public boolean isEnabled() { return undoManager.canRedo(); } }); im.put(KeyStroke.getKeyStroke(KeyEvent.VK_Y, getMenuShortcutKeyMask()), "redo"); }
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/* ww w .j a v a2 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"); } }
From source file:com.kstenschke.copypastestack.ToolWindow.java
/** * Add undoManager to clip pane//ww w . java2 s . c om */ private void initInlineEditor() { Document document = this.form.textPanePreview.getDocument(); if (this.undoManager != null) { document.removeUndoableEditListener(this.undoManager); } this.undoManager = new UndoManager(); document.addUndoableEditListener(this.undoManager); final ToolWindow toolWindowFin = this; InputMap inputMap = this.form.textPanePreview.getInputMap(); // CTRL + Z = undo ( + Z on Mac OS) inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_Z, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()), new AbstractAction() { @Override public void actionPerformed(ActionEvent e) { if (toolWindowFin.undoManager.canUndo()) { toolWindowFin.undoManager.undo(); } } }); // CTRL + SHIFT + Z = redo ( + SHIFT + Z on Mac OS) inputMap.put( KeyStroke.getKeyStroke(KeyEvent.VK_Z, InputEvent.SHIFT_DOWN_MASK | Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()), new AbstractAction() { @Override public void actionPerformed(ActionEvent e) { if (toolWindowFin.undoManager.canRedo()) { toolWindowFin.undoManager.redo(); } } }); }