Ignore:
Timestamp:
2015-02-02T21:46:25+01:00 (10 years ago)
Author:
Don-vip
Message:

fix #11046 - bind Undo action to Ctrl-Z in JosmTextField

Location:
trunk/src/org/openstreetmap/josm/gui/widgets
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/widgets/JosmTextField.java

    r7937 r8000  
    1818/**
    1919 * Subclass of {@link JTextField} that:<ul>
    20  * <li>adds a "native" context menu (cut/copy/paste/select all)</li>
     20 * <li>adds a "native" context menu (undo/cut/copy/paste/select all)</li>
    2121 * <li>adds an optional "hint" displayed when no text has been entered</li>
    2222 * <li>disables the global advanced key press detector when focused</li>
  • trunk/src/org/openstreetmap/josm/gui/widgets/TextContextualPopupMenu.java

    r7937 r8000  
    44import static org.openstreetmap.josm.tools.I18n.tr;
    55
     6import java.awt.Toolkit;
    67import java.awt.event.ActionEvent;
     8import java.awt.event.KeyEvent;
    79import java.beans.PropertyChangeEvent;
    810import java.beans.PropertyChangeListener;
     
    1315import javax.swing.JMenuItem;
    1416import javax.swing.JPopupMenu;
     17import javax.swing.KeyStroke;
    1518import javax.swing.event.UndoableEditEvent;
    1619import javax.swing.event.UndoableEditListener;
     
    3740public class TextContextualPopupMenu extends JPopupMenu {
    3841
     42    private static final String EDITABLE = "editable";
     43
    3944    protected JTextComponent component = null;
    4045    protected UndoAction undoAction = null;
     
    4247    protected final PropertyChangeListener propertyChangeListener = new PropertyChangeListener() {
    4348        @Override public void propertyChange(PropertyChangeEvent evt) {
    44             if ("editable".equals(evt.getPropertyName())) {
     49            if (EDITABLE.equals(evt.getPropertyName())) {
    4550                removeAll();
    4651                addMenuEntries();
     
    6873                undoAction = new UndoAction();
    6974                component.getDocument().addUndoableEditListener(undoAction);
     75                component.getInputMap().put(
     76                        KeyStroke.getKeyStroke(KeyEvent.VK_Z, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()), undoAction);
    7077            }
    7178            addMenuEntries();
    72             component.addPropertyChangeListener("editable", propertyChangeListener);
     79            component.addPropertyChangeListener(EDITABLE, propertyChangeListener);
    7380        }
    7481        return this;
     
    97104    protected TextContextualPopupMenu detach() {
    98105        if (isAttached()) {
    99             component.removePropertyChangeListener("editable", propertyChangeListener);
     106            component.removePropertyChangeListener(EDITABLE, propertyChangeListener);
    100107            removeAll();
    101108            if (undoAction != null) {
     
    161168        private final UndoManager undoManager = new UndoManager();
    162169
     170        /**
     171         * Constructs a new {@code UndoAction}.
     172         */
    163173        public UndoAction() {
    164174            super(tr("Undo"));
     
    177187                undoManager.undo();
    178188            } catch (CannotUndoException ex) {
    179                 // Ignored
     189                if (Main.isTraceEnabled()) {
     190                    Main.trace(ex.getMessage());
     191                }
    180192            } finally {
    181193                setEnabled(undoManager.canUndo());
Note: See TracChangeset for help on using the changeset viewer.