Example usage for javax.swing.undo UndoManager canUndo

List of usage examples for javax.swing.undo UndoManager canUndo

Introduction

In this page you can find the example usage for javax.swing.undo UndoManager canUndo.

Prototype

public synchronized boolean canUndo() 

Source Link

Document

Returns true if edits may be undone.

Usage

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 ava 2 s . co 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 ww  .ja  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) {
            }
        }
    });
    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 www  . ja va2s  . 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 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");

    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);/*  w w w. j ava 2s .  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: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();//from www  . j a v  a  2s.c  o m
    mgr.redo();
    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 bindUndoManager(final JTextComponent text, final UndoManager undo) {

    text.getDocument().addUndoableEditListener(new UndoableEditListener() {
        @Override/*from w ww.  jav a 2s . c  om*/
        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());
        }// w w w  . j a va  2s  .com
    });

    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:ee.ioc.cs.vsle.editor.Editor.java

/**
 * Updates Undo and Redo actions. When a new scheme tab is selected/opened
 * or an action that modifies the scheme is performed the undo and redo
 * action objects have to be updated to reflect the current state, this
 * includes presentation name and enabled/disabled status.
 *///  www.  jav  a 2s  .  co  m
public void refreshUndoRedo() {
    Canvas canvas = getCurrentCanvas();
    if (canvas != null) {
        UndoManager um = canvas.undoManager;
        undoAction.setEnabled(!canvas.isActionInProgress() && um.canUndo());
        redoAction.setEnabled(!canvas.isActionInProgress() && um.canRedo());
        undoAction.putValue(Action.NAME, um.getUndoPresentationName());
        redoAction.putValue(Action.NAME, um.getRedoPresentationName());
    } else {
        undoAction.setEnabled(false);
        redoAction.setEnabled(false);
        undoAction.putValue(Action.NAME, Menu.UNDO);
        redoAction.putValue(Action.NAME, Menu.REDO);
    }
}

From source file:com.github.benchdoos.weblocopener.weblocOpener.gui.EditDialog.java

private void initTextField(String pathToEditingFile) {
    textField.addMouseListener(new ClickListener() {
        @Override/* w w w  . ja  v a  2  s. com*/
        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);
}