- Timestamp:
- 2015-02-02T23:56:07+01:00 (10 years ago)
- Location:
- trunk/src/org/openstreetmap/josm/gui/widgets
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/gui/widgets/JosmTextField.java
r8000 r8002 18 18 /** 19 19 * Subclass of {@link JTextField} that:<ul> 20 * <li>adds a "native" context menu (undo/cut/copy/paste/select all)</li> 20 * <li>adds a "native" context menu (undo/redo/cut/copy/paste/select all)</li> 21 21 * <li>adds an optional "hint" displayed when no text has been entered</li> 22 22 * <li>disables the global advanced key press detector when focused</li> -
trunk/src/org/openstreetmap/josm/gui/widgets/TextContextualPopupMenu.java
r8000 r8002 4 4 import static org.openstreetmap.josm.tools.I18n.tr; 5 5 6 import java.awt.GraphicsEnvironment; 6 7 import java.awt.Toolkit; 7 8 import java.awt.event.ActionEvent; … … 20 21 import javax.swing.text.DefaultEditorKit; 21 22 import javax.swing.text.JTextComponent; 23 import javax.swing.undo.CannotRedoException; 22 24 import javax.swing.undo.CannotUndoException; 23 25 import javax.swing.undo.UndoManager; … … 30 32 * <ul> 31 33 * <li>Undo</li> 34 * <li>Redo</li> 32 35 * <li>Cut</li> 33 36 * <li>Copy</li> … … 43 46 44 47 protected JTextComponent component = null; 45 protected UndoAction undoAction = null; 48 protected final UndoAction undoAction = new UndoAction(); 49 protected final RedoAction redoAction = new RedoAction(); 50 protected final UndoManager undo = new UndoManager(); 51 52 protected final UndoableEditListener undoEditListener = new UndoableEditListener() { 53 @Override 54 public void undoableEditHappened(UndoableEditEvent e) { 55 undo.addEdit(e.getEdit()); 56 undoAction.updateUndoState(); 57 redoAction.updateRedoState(); 58 } 59 }; 46 60 47 61 protected final PropertyChangeListener propertyChangeListener = new PropertyChangeListener() { 48 @Override public void propertyChange(PropertyChangeEvent evt) { 62 @Override 63 public void propertyChange(PropertyChangeEvent evt) { 49 64 if (EDITABLE.equals(evt.getPropertyName())) { 50 65 removeAll(); … … 71 86 this.component = component; 72 87 if (component.isEditable()) { 73 undoAction = new UndoAction(); 74 component.getDocument().addUndoableEditListener(undoAction); 75 component.getInputMap().put( 76 KeyStroke.getKeyStroke(KeyEvent.VK_Z, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()), undoAction); 88 component.getDocument().addUndoableEditListener(undoEditListener); 89 if (!GraphicsEnvironment.isHeadless()) { 90 component.getInputMap().put( 91 KeyStroke.getKeyStroke(KeyEvent.VK_Z, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()), undoAction); 92 component.getInputMap().put( 93 KeyStroke.getKeyStroke(KeyEvent.VK_Y, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()), redoAction); 94 } 77 95 } 78 96 addMenuEntries(); … … 85 103 if (component.isEditable()) { 86 104 add(new JMenuItem(undoAction)); 105 add(new JMenuItem(redoAction)); 87 106 addSeparator(); 88 107 addMenuEntry(component, tr("Cut"), DefaultEditorKit.cutAction, null); … … 106 125 component.removePropertyChangeListener(EDITABLE, propertyChangeListener); 107 126 removeAll(); 108 if (undoAction != null) { 109 component.getDocument().removeUndoableEditListener(undoAction); 110 undoAction = null; 111 } 112 this.component = null; 127 component.getDocument().removeUndoableEditListener(undoEditListener); 128 component = null; 113 129 } 114 130 return this; … … 164 180 } 165 181 166 protected static class UndoAction extends AbstractAction implements UndoableEditListener { 167 168 private final UndoManager undoManager = new UndoManager(); 182 protected class UndoAction extends AbstractAction { 169 183 170 184 /** … … 177 191 178 192 @Override 179 public void undoableEditHappened(UndoableEditEvent e) {180 undoManager.addEdit(e.getEdit());181 setEnabled(undoManager.canUndo());182 }183 184 @Override185 193 public void actionPerformed(ActionEvent e) { 186 194 try { 187 undo Manager.undo();195 undo.undo(); 188 196 } catch (CannotUndoException ex) { 189 197 if (Main.isTraceEnabled()) { … … 191 199 } 192 200 } finally { 193 setEnabled(undoManager.canUndo()); 201 updateUndoState(); 202 redoAction.updateRedoState(); 203 } 204 } 205 206 public void updateUndoState() { 207 if (undo.canUndo()) { 208 setEnabled(true); 209 putValue(Action.NAME, undo.getUndoPresentationName()); 210 } else { 211 setEnabled(false); 212 putValue(Action.NAME, tr("Undo")); 213 } 214 } 215 } 216 217 protected class RedoAction extends AbstractAction { 218 219 /** 220 * Constructs a new {@code RedoAction}. 221 */ 222 public RedoAction() { 223 super(tr("Redo")); 224 setEnabled(false); 225 } 226 227 @Override 228 public void actionPerformed(ActionEvent e) { 229 try { 230 undo.redo(); 231 } catch (CannotRedoException ex) { 232 if (Main.isTraceEnabled()) { 233 Main.trace(ex.getMessage()); 234 } 235 } finally { 236 updateRedoState(); 237 undoAction.updateUndoState(); 238 } 239 } 240 241 public void updateRedoState() { 242 if (undo.canRedo()) { 243 setEnabled(true); 244 putValue(Action.NAME, undo.getRedoPresentationName()); 245 } else { 246 setEnabled(false); 247 putValue(Action.NAME, tr("Redo")); 194 248 } 195 249 }
Note:
See TracChangeset
for help on using the changeset viewer.