List of usage examples for javax.swing.undo UndoManager UndoManager
public UndoManager()
UndoManager
. From source file:UndoableDrawingPanel.java
public static void main(String args[]) { JFrame frame = new JFrame("Drawing Sample2"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); UndoableDrawingPanel drawingPanel = new UndoableDrawingPanel(); UndoManager manager = new UndoManager(); drawingPanel.addUndoableEditListener(manager); JToolBar toolbar = new JToolBar(); JButton undoButton = new JButton(UndoManagerHelper.getUndoAction(manager)); toolbar.add(undoButton);/*from w w w . j a v a2s. c om*/ JButton redoButton = new JButton(UndoManagerHelper.getRedoAction(manager)); toolbar.add(redoButton); frame.add(toolbar, BorderLayout.NORTH); frame.add(drawingPanel, BorderLayout.CENTER); frame.setSize(300, 150); frame.setVisible(true); }
From source file:UndoableDrawingPanel2.java
public static void main(String args[]) { JFrame frame = new JFrame("Drawing Sample2"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); UndoableDrawingPanel2 drawingPanel = new UndoableDrawingPanel2(); UndoManager manager = new UndoManager(); drawingPanel.addUndoableEditListener(manager); JToolBar toolbar = new JToolBar(); toolbar.add(UndoManagerHelper.getUndoAction(manager)); toolbar.add(UndoManagerHelper.getRedoAction(manager)); Container content = frame.getContentPane(); content.add(toolbar, BorderLayout.NORTH); content.add(drawingPanel, BorderLayout.CENTER); frame.setSize(300, 150);//w w w.j ava2 s . c o m frame.setVisible(true); }
From source file:UndoManagerHelper.java
public static void main(String args[]) { JFrame frame = new JFrame("Undo Sample"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JTextArea textArea = new JTextArea(); JScrollPane scrollPane = new JScrollPane(textArea); UndoManager manager = new UndoManager(); textArea.getDocument().addUndoableEditListener(manager); JToolBar toolbar = new JToolBar(); JButton undoButton = new JButton(UndoManagerHelper.getUndoAction(manager)); toolbar.add(undoButton);/*from www . java 2s .co m*/ JButton redoButton = new JButton(UndoManagerHelper.getRedoAction(manager)); toolbar.add(redoButton); frame.add(toolbar, BorderLayout.NORTH); frame.add(scrollPane, BorderLayout.CENTER); frame.setSize(300, 150); frame.setVisible(true); }
From source file:Main.java
public static void installUndoManager(JTextComponent textComponent) { installUndoManager(textComponent, new UndoManager()); }
From source file:UndoableTextArea.java
private void createUndoMananger() { m_undoManager = new UndoManager(); m_undoManager.setLimit(10); }
From source file:UndoExample3.java
public UndoExample3() { super("Undo/Redo Example 3"); DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode("root"); DefaultMutableTreeNode node = new DefaultMutableTreeNode("Apollo 8"); rootNode.add(node);//from w w w.ja va 2 s .c o m node.add(new DefaultMutableTreeNode("Borman")); node.add(new DefaultMutableTreeNode("Lovell")); node.add(new DefaultMutableTreeNode("Anders")); node = new DefaultMutableTreeNode("Apollo 11"); rootNode.add(node); node.add(new DefaultMutableTreeNode("Armstrong")); node.add(new DefaultMutableTreeNode("Aldrin")); node.add(new DefaultMutableTreeNode("Collins")); node = new DefaultMutableTreeNode("Apollo 12"); rootNode.add(node); node.add(new DefaultMutableTreeNode("Conrad")); node.add(new DefaultMutableTreeNode("Gordon")); node.add(new DefaultMutableTreeNode("Bean")); UndoableTree tree = new UndoableTree(rootNode); getContentPane().add(new JScrollPane(tree), BorderLayout.CENTER); // Create the undo manager and actions UndoManager manager = new UndoManager(); tree.addUndoableEditListener(manager); Action undoAction = new UndoAction(manager); Action redoAction = new RedoAction(manager); // Add the actions to buttons JPanel panel = new JPanel(); JButton undoButton = new JButton("Undo"); JButton redoButton = new JButton("Redo"); undoButton.addActionListener(undoAction); redoButton.addActionListener(redoAction); panel.add(undoButton); panel.add(redoButton); getContentPane().add(panel, BorderLayout.SOUTH); // Assign the actions to keys ((JComponent) getContentPane()).registerKeyboardAction(undoAction, KeyStroke.getKeyStroke(KeyEvent.VK_Z, InputEvent.CTRL_MASK), JComponent.WHEN_IN_FOCUSED_WINDOW); ((JComponent) getContentPane()).registerKeyboardAction(redoAction, KeyStroke.getKeyStroke(KeyEvent.VK_Y, InputEvent.CTRL_MASK), JComponent.WHEN_IN_FOCUSED_WINDOW); }
From source file:UndoExample4.java
public UndoExample4() { super("Undo/Redo Example 4"); DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode("root"); DefaultMutableTreeNode node = new DefaultMutableTreeNode("Apollo 8"); rootNode.add(node);//from ww w .j av a2 s .com node.add(new DefaultMutableTreeNode("Borman")); node.add(new DefaultMutableTreeNode("Lovell")); node.add(new DefaultMutableTreeNode("Anders")); node = new DefaultMutableTreeNode("Apollo 11"); rootNode.add(node); node.add(new DefaultMutableTreeNode("Armstrong")); node.add(new DefaultMutableTreeNode("Aldrin")); node.add(new DefaultMutableTreeNode("Collins")); node = new DefaultMutableTreeNode("Apollo 12"); rootNode.add(node); node.add(new DefaultMutableTreeNode("Conrad")); node.add(new DefaultMutableTreeNode("Gordon")); node.add(new DefaultMutableTreeNode("Bean")); UndoableTree2 tree = new UndoableTree2(rootNode); getContentPane().add(new JScrollPane(tree), BorderLayout.CENTER); // Create the undo manager and actions UndoManager manager = new UndoManager(); tree.addUndoableEditListener(manager); Action undoAction = new UndoAction(manager); Action redoAction = new RedoAction(manager); // Add the actions to buttons JPanel panel = new JPanel(); JButton undoButton = new JButton("Undo"); JButton redoButton = new JButton("Redo"); undoButton.addActionListener(undoAction); redoButton.addActionListener(redoAction); panel.add(undoButton); panel.add(redoButton); getContentPane().add(panel, BorderLayout.SOUTH); // Assign the actions to keys ((JComponent) getContentPane()).registerKeyboardAction(undoAction, KeyStroke.getKeyStroke(KeyEvent.VK_Z, InputEvent.CTRL_MASK), JComponent.WHEN_IN_FOCUSED_WINDOW); ((JComponent) getContentPane()).registerKeyboardAction(redoAction, KeyStroke.getKeyStroke(KeyEvent.VK_Y, InputEvent.CTRL_MASK), JComponent.WHEN_IN_FOCUSED_WINDOW); }
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//from w ww . j a va 2 s.c o m 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:UndoDemo.java
/** Construct a GUI that demonstrates use of UndoManager */ public UndoDemo() { Container cp = getContentPane(); cp.add(ta = new JTextArea(20, 60), BorderLayout.CENTER); JPanel bp;/*from w w w. j av a 2 s .c o m*/ cp.add(bp = new JPanel(), BorderLayout.SOUTH); // Create a javax.swing.undo.UndoManager; this is an amazing class that // keeps a Stack of UndoableEdits and lets you invoke them; // by registering it as a Listener on the TextComponent.Document, // the Document will create the UndoableEdit objects and send them // to the UndoManager. Between them they do ALL the work! um = new UndoManager(); ta.getDocument().addUndoableEditListener(um); // Create the buttons JButton cutButton, copyButton, pasteButton, undoButton, redoButton; bp.add(cutButton = new JButton("Cut")); cutButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { ta.cut(); } }); bp.add(copyButton = new JButton("Copy")); copyButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { ta.copy(); } }); bp.add(pasteButton = new JButton("Paste")); pasteButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { ta.paste(); } }); bp.add(undoButton = new JButton("UnDo")); undoButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (um.canUndo()) { um.undo(); } else { warn("Can't undo"); } } }); bp.add(redoButton = new JButton("ReDo")); redoButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (um.canRedo()) { um.redo(); } else { warn("Can't redo"); } } }); pack(); setDefaultCloseOperation(EXIT_ON_CLOSE); }
From source file:com.github.benchdoos.weblocopener.weblocOpener.gui.EditDialog.java
private void initTextField(String pathToEditingFile) { textField.addMouseListener(new ClickListener() { @Override//from w w w . j a v a 2s. 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); }