List of usage examples for javax.swing.undo UndoManager undo
public void undo() throws CannotUndoException
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 . 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) { } } }); 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()); }/*from 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 ww .j a v a2s.com*/ }); 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) { 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 www . j a v a 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:UndoManagerDetails.java
public static void main(String[] args) { UndoManager mgr = new UndoManager(); mgr.addEdit(new SampleUndoableEdit(1, false, true, false)); mgr.addEdit(new SampleUndoableEdit(2, false, true, false)); mgr.addEdit(new SampleUndoableEdit(3, false, false, false)); mgr.addEdit(new SampleUndoableEdit(4, false, false, false)); System.out.println("Insignificant edit example"); mgr.undo(); mgr.redo();/* ww w .j a v a 2 s .com*/ System.out.println(mgr.canRedo()); // No more sig. edits // Show how edits that call add/replace are used. // // # adds? sig? replace? mgr.addEdit(new SampleUndoableEdit(5, true, true, false)); mgr.addEdit(new SampleUndoableEdit(6, false, true, false)); System.out.println("Absorbed (by addEdit) edit example"); mgr.undo(); mgr.discardAllEdits(); // # adds? sig? replace? mgr.addEdit(new SampleUndoableEdit(1, false, true, false)); mgr.addEdit(new SampleUndoableEdit(2, false, true, true)); System.out.println("Absorbed (by replaceEdit) edit example"); mgr.undo(); System.out.println(mgr.canUndo()); // Show how changing limit works. mgr.discardAllEdits(); // # adds? sig? replace? mgr.addEdit(new SampleUndoableEdit(1, false, true, false)); mgr.addEdit(new SampleUndoableEdit(2, false, true, false)); mgr.addEdit(new SampleUndoableEdit(3, false, true, false)); mgr.addEdit(new SampleUndoableEdit(4, false, true, false)); mgr.addEdit(new SampleUndoableEdit(5, false, true, false)); mgr.addEdit(new SampleUndoableEdit(6, false, true, false)); System.out.println("Changing limit example"); mgr.undo(); mgr.undo(); mgr.undo(); // Now 3 undoable, 3 redoable mgr.setLimit(4); // Now 2 undoable, 2 redoable! while (mgr.canUndo()) mgr.undo(); while (mgr.canRedo()) mgr.redo(); // undoOrRedo example mgr.discardAllEdits(); mgr.setLimit(1); // # adds? sig? replace? mgr.addEdit(new SampleUndoableEdit(1, false, true, false)); System.out.println("undoOrRedo example"); System.out.println(mgr.getUndoOrRedoPresentationName()); mgr.undoOrRedo(); System.out.println(mgr.getUndoOrRedoPresentationName()); mgr.undoOrRedo(); // Show how UndoManager becomes a CompositeEdit. mgr.discardAllEdits(); mgr.setLimit(100); // # adds? sig? replace? mgr.addEdit(new SampleUndoableEdit(1, false, true, false)); mgr.addEdit(new SampleUndoableEdit(2, false, true, false)); mgr.addEdit(new SampleUndoableEdit(3, false, true, false)); System.out.println("Transform to composite example"); mgr.end(); mgr.undo(); mgr.redo(); // Show that adds are no longer allowed. Note that addEdit() returns true in // pre-JDK 1.2 Swing releases. This is fixed in JDK 1.2. System.out.println(mgr.addEdit(new SampleUndoableEdit(4, false, true, false))); mgr.undo(); // note that edit 4 is not there }
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);/* w ww . j a v a 2 s . c om*/ 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
public static void bindUndoManager(final JTextComponent text, final UndoManager undo) { text.getDocument().addUndoableEditListener(new UndoableEditListener() { @Override/*from w w w . j a v a2 s. co m*/ public void undoableEditHappened(UndoableEditEvent e) { undo.addEdit(e.getEdit()); } }); text.getActionMap().put("Undo", new AbstractAction("Undo") { private static final long serialVersionUID = 1L; public void actionPerformed(ActionEvent evt) { try { if (undo.canUndo()) { undo.undo(); } } catch (CannotUndoException e) { } } }); // Bind the undo action to ctl-Z text.getInputMap().put(KeyStroke.getKeyStroke("control Z"), "Undo"); // Create a redo action and add it to the text component text.getActionMap().put("Redo", new AbstractAction("Redo") { private static final long serialVersionUID = 1L; public void actionPerformed(ActionEvent evt) { try { if (undo.canRedo()) { undo.redo(); } } catch (CannotRedoException e) { } } }); // Bind the redo action to ctl-Y text.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 www. j av a 2 s. co 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:com.github.benchdoos.weblocopener.weblocOpener.gui.EditDialog.java
private void initTextField(String pathToEditingFile) { textField.addMouseListener(new ClickListener() { @Override// w ww .java 2 s . co m public void doubleClick(MouseEvent e) { textField.selectAll(); } }); textField.getDocument().addDocumentListener(new DocumentListener() { @Override public void changedUpdate(DocumentEvent e) { } @Override public void insertUpdate(DocumentEvent e) { updateTextFont(); } @Override public void removeUpdate(DocumentEvent e) { updateTextFont(); } private void updateTextFont() { UrlValidator urlValidator = new UrlValidator(); if (urlValidator.isValid(textField.getText())) { if (textField != null) { setTextFieldFont(textField.getFont(), TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON); textField.setForeground(Color.BLUE); } } else { if (textField != null) { setTextFieldFont(textField.getFont(), TextAttribute.UNDERLINE, -1); textField.setForeground(Color.BLACK); } } } }); UndoManager undoManager = new UndoManager(); textField.getDocument().addUndoableEditListener(new UndoableEditListener() { public void undoableEditHappened(UndoableEditEvent evt) { undoManager.addEdit(evt.getEdit()); } }); textField.getActionMap().put("Undo", new AbstractAction("Undo") { public void actionPerformed(ActionEvent evt) { try { if (undoManager.canUndo()) { undoManager.undo(); } } catch (CannotUndoException e) { } } }); textField.getInputMap().put(KeyStroke.getKeyStroke("control Z"), "Undo"); textField.getActionMap().put("Redo", new AbstractAction("Redo") { public void actionPerformed(ActionEvent evt) { try { if (undoManager.canRedo()) { undoManager.redo(); } } catch (CannotRedoException e) { } } }); textField.getInputMap().put(KeyStroke.getKeyStroke("control shift Z"), "Redo"); fillTextField(pathToEditingFile); }
From source file:net.sourceforge.pmd.util.designer.Designer.java
private static void makeTextComponentUndoable(JTextComponent textConponent) { final UndoManager undoManager = new UndoManager(); textConponent.getDocument().addUndoableEditListener(new UndoableEditListener() { @Override//from w w w. j av a2 s . c o m public void undoableEditHappened(UndoableEditEvent evt) { undoManager.addEdit(evt.getEdit()); } }); ActionMap actionMap = textConponent.getActionMap(); InputMap inputMap = textConponent.getInputMap(); actionMap.put("Undo", new AbstractAction("Undo") { @Override public void actionPerformed(ActionEvent evt) { try { if (undoManager.canUndo()) { undoManager.undo(); } } catch (CannotUndoException e) { throw new RuntimeException(e); } } }); inputMap.put(KeyStroke.getKeyStroke("control Z"), "Undo"); actionMap.put("Redo", new AbstractAction("Redo") { @Override public void actionPerformed(ActionEvent evt) { try { if (undoManager.canRedo()) { undoManager.redo(); } } catch (CannotRedoException e) { throw new RuntimeException(e); } } }); inputMap.put(KeyStroke.getKeyStroke("control Y"), "Redo"); }