1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.dialogs;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.Component;
|
---|
7 | import java.awt.Dimension;
|
---|
8 | import java.awt.GridBagLayout;
|
---|
9 | import java.awt.event.ActionEvent;
|
---|
10 | import java.awt.event.KeyEvent;
|
---|
11 | import java.awt.event.MouseEvent;
|
---|
12 | import java.util.ArrayList;
|
---|
13 | import java.util.Arrays;
|
---|
14 | import java.util.LinkedHashSet;
|
---|
15 | import java.util.List;
|
---|
16 | import java.util.Set;
|
---|
17 |
|
---|
18 | import javax.swing.AbstractAction;
|
---|
19 | import javax.swing.Box;
|
---|
20 | import javax.swing.JComponent;
|
---|
21 | import javax.swing.JLabel;
|
---|
22 | import javax.swing.JPanel;
|
---|
23 | import javax.swing.JPopupMenu;
|
---|
24 | import javax.swing.JScrollPane;
|
---|
25 | import javax.swing.JSeparator;
|
---|
26 | import javax.swing.JTree;
|
---|
27 | import javax.swing.event.TreeModelEvent;
|
---|
28 | import javax.swing.event.TreeModelListener;
|
---|
29 | import javax.swing.event.TreeSelectionEvent;
|
---|
30 | import javax.swing.event.TreeSelectionListener;
|
---|
31 | import javax.swing.tree.DefaultMutableTreeNode;
|
---|
32 | import javax.swing.tree.DefaultTreeCellRenderer;
|
---|
33 | import javax.swing.tree.DefaultTreeModel;
|
---|
34 | import javax.swing.tree.TreePath;
|
---|
35 | import javax.swing.tree.TreeSelectionModel;
|
---|
36 |
|
---|
37 | import org.openstreetmap.josm.Main;
|
---|
38 | import org.openstreetmap.josm.actions.AutoScaleAction;
|
---|
39 | import org.openstreetmap.josm.command.Command;
|
---|
40 | import org.openstreetmap.josm.command.PseudoCommand;
|
---|
41 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
42 | import org.openstreetmap.josm.gui.SideButton;
|
---|
43 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
44 | import org.openstreetmap.josm.gui.layer.OsmDataLayer.CommandQueueListener;
|
---|
45 | import org.openstreetmap.josm.gui.widgets.PopupMenuLauncher;
|
---|
46 | import org.openstreetmap.josm.tools.FilteredCollection;
|
---|
47 | import org.openstreetmap.josm.tools.GBC;
|
---|
48 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
49 | import org.openstreetmap.josm.tools.InputMapUtils;
|
---|
50 | import org.openstreetmap.josm.tools.Predicate;
|
---|
51 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
52 |
|
---|
53 | /**
|
---|
54 | * Dialog displaying list of all executed commands (undo/redo buffer).
|
---|
55 | * @since 94
|
---|
56 | */
|
---|
57 | public class CommandStackDialog extends ToggleDialog implements CommandQueueListener {
|
---|
58 |
|
---|
59 | private final DefaultTreeModel undoTreeModel = new DefaultTreeModel(new DefaultMutableTreeNode());
|
---|
60 | private final DefaultTreeModel redoTreeModel = new DefaultTreeModel(new DefaultMutableTreeNode());
|
---|
61 |
|
---|
62 | private final JTree undoTree = new JTree(undoTreeModel);
|
---|
63 | private final JTree redoTree = new JTree(redoTreeModel);
|
---|
64 |
|
---|
65 | private transient UndoRedoSelectionListener undoSelectionListener;
|
---|
66 | private transient UndoRedoSelectionListener redoSelectionListener;
|
---|
67 |
|
---|
68 | private JScrollPane scrollPane;
|
---|
69 | private JSeparator separator = new JSeparator();
|
---|
70 | // only visible, if separator is the top most component
|
---|
71 | private Component spacer = Box.createRigidArea(new Dimension(0, 3));
|
---|
72 |
|
---|
73 | // last operation is remembered to select the next undo/redo entry in the list
|
---|
74 | // after undo/redo command
|
---|
75 | private UndoRedoType lastOperation = UndoRedoType.UNDO;
|
---|
76 |
|
---|
77 | // Actions for context menu and Enter key
|
---|
78 | private SelectAction selectAction = new SelectAction();
|
---|
79 | private SelectAndZoomAction selectAndZoomAction = new SelectAndZoomAction();
|
---|
80 |
|
---|
81 | /**
|
---|
82 | * Constructs a new {@code CommandStackDialog}.
|
---|
83 | */
|
---|
84 | public CommandStackDialog() {
|
---|
85 | super(tr("Command Stack"), "commandstack", tr("Open a list of all commands (undo buffer)."),
|
---|
86 | Shortcut.registerShortcut("subwindow:commandstack", tr("Toggle: {0}",
|
---|
87 | tr("Command Stack")), KeyEvent.VK_O, Shortcut.ALT_SHIFT), 100);
|
---|
88 | undoTree.addMouseListener(new MouseEventHandler());
|
---|
89 | undoTree.setRootVisible(false);
|
---|
90 | undoTree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
|
---|
91 | undoTree.setShowsRootHandles(true);
|
---|
92 | undoTree.expandRow(0);
|
---|
93 | undoTree.setCellRenderer(new CommandCellRenderer());
|
---|
94 | undoSelectionListener = new UndoRedoSelectionListener(undoTree);
|
---|
95 | undoTree.getSelectionModel().addTreeSelectionListener(undoSelectionListener);
|
---|
96 | InputMapUtils.unassignCtrlShiftUpDown(undoTree, JComponent.WHEN_FOCUSED);
|
---|
97 |
|
---|
98 | redoTree.addMouseListener(new MouseEventHandler());
|
---|
99 | redoTree.setRootVisible(false);
|
---|
100 | redoTree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
|
---|
101 | redoTree.setShowsRootHandles(true);
|
---|
102 | redoTree.expandRow(0);
|
---|
103 | redoTree.setCellRenderer(new CommandCellRenderer());
|
---|
104 | redoSelectionListener = new UndoRedoSelectionListener(redoTree);
|
---|
105 | redoTree.getSelectionModel().addTreeSelectionListener(redoSelectionListener);
|
---|
106 | InputMapUtils.unassignCtrlShiftUpDown(redoTree, JComponent.WHEN_FOCUSED);
|
---|
107 |
|
---|
108 | JPanel treesPanel = new JPanel(new GridBagLayout());
|
---|
109 |
|
---|
110 | treesPanel.add(spacer, GBC.eol());
|
---|
111 | spacer.setVisible(false);
|
---|
112 | treesPanel.add(undoTree, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
113 | separator.setVisible(false);
|
---|
114 | treesPanel.add(separator, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
115 | treesPanel.add(redoTree, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
116 | treesPanel.add(Box.createRigidArea(new Dimension(0, 0)), GBC.std().weight(0, 1));
|
---|
117 | treesPanel.setBackground(redoTree.getBackground());
|
---|
118 |
|
---|
119 | wireUpdateEnabledStateUpdater(selectAction, undoTree);
|
---|
120 | wireUpdateEnabledStateUpdater(selectAction, redoTree);
|
---|
121 |
|
---|
122 | UndoRedoAction undoAction = new UndoRedoAction(UndoRedoType.UNDO);
|
---|
123 | wireUpdateEnabledStateUpdater(undoAction, undoTree);
|
---|
124 |
|
---|
125 | UndoRedoAction redoAction = new UndoRedoAction(UndoRedoType.REDO);
|
---|
126 | wireUpdateEnabledStateUpdater(redoAction, redoTree);
|
---|
127 |
|
---|
128 | scrollPane = (JScrollPane) createLayout(treesPanel, true, Arrays.asList(new SideButton[] {
|
---|
129 | new SideButton(selectAction),
|
---|
130 | new SideButton(undoAction),
|
---|
131 | new SideButton(redoAction)
|
---|
132 | }));
|
---|
133 |
|
---|
134 | InputMapUtils.addEnterAction(undoTree, selectAndZoomAction);
|
---|
135 | InputMapUtils.addEnterAction(redoTree, selectAndZoomAction);
|
---|
136 | }
|
---|
137 |
|
---|
138 | private static class CommandCellRenderer extends DefaultTreeCellRenderer {
|
---|
139 | @Override
|
---|
140 | public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row,
|
---|
141 | boolean hasFocus) {
|
---|
142 | super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
|
---|
143 | DefaultMutableTreeNode v = (DefaultMutableTreeNode) value;
|
---|
144 | if (v.getUserObject() instanceof JLabel) {
|
---|
145 | JLabel l = (JLabel) v.getUserObject();
|
---|
146 | setIcon(l.getIcon());
|
---|
147 | setText(l.getText());
|
---|
148 | }
|
---|
149 | return this;
|
---|
150 | }
|
---|
151 | }
|
---|
152 |
|
---|
153 | private void updateTitle() {
|
---|
154 | int undo = undoTreeModel.getChildCount(undoTreeModel.getRoot());
|
---|
155 | int redo = redoTreeModel.getChildCount(redoTreeModel.getRoot());
|
---|
156 | if (undo > 0 || redo > 0) {
|
---|
157 | setTitle(tr("Command Stack: Undo: {0} / Redo: {1}", undo, redo));
|
---|
158 | } else {
|
---|
159 | setTitle(tr("Command Stack"));
|
---|
160 | }
|
---|
161 | }
|
---|
162 |
|
---|
163 | /**
|
---|
164 | * Selection listener for undo and redo area.
|
---|
165 | * If one is clicked, takes away the selection from the other, so
|
---|
166 | * it behaves as if it was one component.
|
---|
167 | */
|
---|
168 | private class UndoRedoSelectionListener implements TreeSelectionListener {
|
---|
169 | private JTree source;
|
---|
170 |
|
---|
171 | public UndoRedoSelectionListener(JTree source) {
|
---|
172 | this.source = source;
|
---|
173 | }
|
---|
174 |
|
---|
175 | @Override
|
---|
176 | public void valueChanged(TreeSelectionEvent e) {
|
---|
177 | if (source == undoTree) {
|
---|
178 | redoTree.getSelectionModel().removeTreeSelectionListener(redoSelectionListener);
|
---|
179 | redoTree.clearSelection();
|
---|
180 | redoTree.getSelectionModel().addTreeSelectionListener(redoSelectionListener);
|
---|
181 | }
|
---|
182 | if (source == redoTree) {
|
---|
183 | undoTree.getSelectionModel().removeTreeSelectionListener(undoSelectionListener);
|
---|
184 | undoTree.clearSelection();
|
---|
185 | undoTree.getSelectionModel().addTreeSelectionListener(undoSelectionListener);
|
---|
186 | }
|
---|
187 | }
|
---|
188 | }
|
---|
189 |
|
---|
190 | /**
|
---|
191 | * Interface to provide a callback for enabled state update.
|
---|
192 | */
|
---|
193 | protected interface IEnabledStateUpdating {
|
---|
194 | void updateEnabledState();
|
---|
195 | }
|
---|
196 |
|
---|
197 | /**
|
---|
198 | * Wires updater for enabled state to the events. Also updates dialog title if needed.
|
---|
199 | */
|
---|
200 | protected void wireUpdateEnabledStateUpdater(final IEnabledStateUpdating updater, JTree tree) {
|
---|
201 | addShowNotifyListener(updater);
|
---|
202 |
|
---|
203 | tree.addTreeSelectionListener(new TreeSelectionListener() {
|
---|
204 | @Override
|
---|
205 | public void valueChanged(TreeSelectionEvent e) {
|
---|
206 | updater.updateEnabledState();
|
---|
207 | }
|
---|
208 | });
|
---|
209 |
|
---|
210 | tree.getModel().addTreeModelListener(new TreeModelListener() {
|
---|
211 | @Override
|
---|
212 | public void treeNodesChanged(TreeModelEvent e) {
|
---|
213 | updater.updateEnabledState();
|
---|
214 | updateTitle();
|
---|
215 | }
|
---|
216 |
|
---|
217 | @Override
|
---|
218 | public void treeNodesInserted(TreeModelEvent e) {
|
---|
219 | updater.updateEnabledState();
|
---|
220 | updateTitle();
|
---|
221 | }
|
---|
222 |
|
---|
223 | @Override
|
---|
224 | public void treeNodesRemoved(TreeModelEvent e) {
|
---|
225 | updater.updateEnabledState();
|
---|
226 | updateTitle();
|
---|
227 | }
|
---|
228 |
|
---|
229 | @Override
|
---|
230 | public void treeStructureChanged(TreeModelEvent e) {
|
---|
231 | updater.updateEnabledState();
|
---|
232 | updateTitle();
|
---|
233 | }
|
---|
234 | });
|
---|
235 | }
|
---|
236 |
|
---|
237 | @Override
|
---|
238 | public void showNotify() {
|
---|
239 | buildTrees();
|
---|
240 | for (IEnabledStateUpdating listener : showNotifyListener) {
|
---|
241 | listener.updateEnabledState();
|
---|
242 | }
|
---|
243 | Main.main.undoRedo.addCommandQueueListener(this);
|
---|
244 | }
|
---|
245 |
|
---|
246 | /**
|
---|
247 | * Simple listener setup to update the button enabled state when the side dialog shows.
|
---|
248 | */
|
---|
249 | private transient Set<IEnabledStateUpdating> showNotifyListener = new LinkedHashSet<>();
|
---|
250 |
|
---|
251 | private void addShowNotifyListener(IEnabledStateUpdating listener) {
|
---|
252 | showNotifyListener.add(listener);
|
---|
253 | }
|
---|
254 |
|
---|
255 | @Override
|
---|
256 | public void hideNotify() {
|
---|
257 | undoTreeModel.setRoot(new DefaultMutableTreeNode());
|
---|
258 | redoTreeModel.setRoot(new DefaultMutableTreeNode());
|
---|
259 | Main.main.undoRedo.removeCommandQueueListener(this);
|
---|
260 | }
|
---|
261 |
|
---|
262 | /**
|
---|
263 | * Build the trees of undo and redo commands (initially or when
|
---|
264 | * they have changed).
|
---|
265 | */
|
---|
266 | private void buildTrees() {
|
---|
267 | setTitle(tr("Command Stack"));
|
---|
268 | if (!Main.main.hasEditLayer())
|
---|
269 | return;
|
---|
270 |
|
---|
271 | List<Command> undoCommands = Main.main.undoRedo.commands;
|
---|
272 | DefaultMutableTreeNode undoRoot = new DefaultMutableTreeNode();
|
---|
273 | for (int i = 0; i < undoCommands.size(); ++i) {
|
---|
274 | undoRoot.add(getNodeForCommand(undoCommands.get(i), i));
|
---|
275 | }
|
---|
276 | undoTreeModel.setRoot(undoRoot);
|
---|
277 |
|
---|
278 | List<Command> redoCommands = Main.main.undoRedo.redoCommands;
|
---|
279 | DefaultMutableTreeNode redoRoot = new DefaultMutableTreeNode();
|
---|
280 | for (int i = 0; i < redoCommands.size(); ++i) {
|
---|
281 | redoRoot.add(getNodeForCommand(redoCommands.get(i), i));
|
---|
282 | }
|
---|
283 | redoTreeModel.setRoot(redoRoot);
|
---|
284 | if (redoTreeModel.getChildCount(redoRoot) > 0) {
|
---|
285 | redoTree.scrollRowToVisible(0);
|
---|
286 | scrollPane.getHorizontalScrollBar().setValue(0);
|
---|
287 | }
|
---|
288 |
|
---|
289 | separator.setVisible(!undoCommands.isEmpty() || !redoCommands.isEmpty());
|
---|
290 | spacer.setVisible(undoCommands.isEmpty() && !redoCommands.isEmpty());
|
---|
291 |
|
---|
292 | // if one tree is empty, move selection to the other
|
---|
293 | switch (lastOperation) {
|
---|
294 | case UNDO:
|
---|
295 | if (undoCommands.isEmpty()) {
|
---|
296 | lastOperation = UndoRedoType.REDO;
|
---|
297 | }
|
---|
298 | break;
|
---|
299 | case REDO:
|
---|
300 | if (redoCommands.isEmpty()) {
|
---|
301 | lastOperation = UndoRedoType.UNDO;
|
---|
302 | }
|
---|
303 | break;
|
---|
304 | }
|
---|
305 |
|
---|
306 | // select the next command to undo/redo
|
---|
307 | switch (lastOperation) {
|
---|
308 | case UNDO:
|
---|
309 | undoTree.setSelectionRow(undoTree.getRowCount()-1);
|
---|
310 | break;
|
---|
311 | case REDO:
|
---|
312 | redoTree.setSelectionRow(0);
|
---|
313 | break;
|
---|
314 | }
|
---|
315 |
|
---|
316 | undoTree.scrollRowToVisible(undoTreeModel.getChildCount(undoRoot)-1);
|
---|
317 | scrollPane.getHorizontalScrollBar().setValue(0);
|
---|
318 | }
|
---|
319 |
|
---|
320 | /**
|
---|
321 | * Wraps a command in a CommandListMutableTreeNode.
|
---|
322 | * Recursively adds child commands.
|
---|
323 | */
|
---|
324 | protected CommandListMutableTreeNode getNodeForCommand(PseudoCommand c, int idx) {
|
---|
325 | CommandListMutableTreeNode node = new CommandListMutableTreeNode(c, idx);
|
---|
326 | if (c.getChildren() != null) {
|
---|
327 | List<PseudoCommand> children = new ArrayList<>(c.getChildren());
|
---|
328 | for (int i = 0; i < children.size(); ++i) {
|
---|
329 | node.add(getNodeForCommand(children.get(i), i));
|
---|
330 | }
|
---|
331 | }
|
---|
332 | return node;
|
---|
333 | }
|
---|
334 |
|
---|
335 | /**
|
---|
336 | * Return primitives that are affected by some command
|
---|
337 | * @param path GUI elements
|
---|
338 | * @return collection of affected primitives, onluy usable ones
|
---|
339 | */
|
---|
340 | protected static FilteredCollection<OsmPrimitive> getAffectedPrimitives(TreePath path) {
|
---|
341 | PseudoCommand c = ((CommandListMutableTreeNode) path.getLastPathComponent()).getCommand();
|
---|
342 | final OsmDataLayer currentLayer = Main.main.getEditLayer();
|
---|
343 | return new FilteredCollection<>(
|
---|
344 | c.getParticipatingPrimitives(),
|
---|
345 | new Predicate<OsmPrimitive>() {
|
---|
346 | @Override
|
---|
347 | public boolean evaluate(OsmPrimitive o) {
|
---|
348 | OsmPrimitive p = currentLayer.data.getPrimitiveById(o);
|
---|
349 | return p != null && p.isUsable();
|
---|
350 | }
|
---|
351 | }
|
---|
352 | );
|
---|
353 | }
|
---|
354 |
|
---|
355 | @Override
|
---|
356 | public void commandChanged(int queueSize, int redoSize) {
|
---|
357 | if (!isVisible())
|
---|
358 | return;
|
---|
359 | buildTrees();
|
---|
360 | }
|
---|
361 |
|
---|
362 | /**
|
---|
363 | * Action that selects the objects that take part in a command.
|
---|
364 | */
|
---|
365 | public class SelectAction extends AbstractAction implements IEnabledStateUpdating {
|
---|
366 |
|
---|
367 | /**
|
---|
368 | * Constructs a new {@code SelectAction}.
|
---|
369 | */
|
---|
370 | public SelectAction() {
|
---|
371 | putValue(NAME, tr("Select"));
|
---|
372 | putValue(SHORT_DESCRIPTION, tr("Selects the objects that take part in this command (unless currently deleted)"));
|
---|
373 | putValue(SMALL_ICON, ImageProvider.get("dialogs", "select"));
|
---|
374 | }
|
---|
375 |
|
---|
376 | @Override
|
---|
377 | public void actionPerformed(ActionEvent e) {
|
---|
378 | TreePath path;
|
---|
379 | if (!undoTree.isSelectionEmpty()) {
|
---|
380 | path = undoTree.getSelectionPath();
|
---|
381 | } else if (!redoTree.isSelectionEmpty()) {
|
---|
382 | path = redoTree.getSelectionPath();
|
---|
383 | } else
|
---|
384 | throw new IllegalStateException();
|
---|
385 |
|
---|
386 | OsmDataLayer editLayer = Main.main.getEditLayer();
|
---|
387 | if (editLayer == null) return;
|
---|
388 | editLayer.data.setSelected(getAffectedPrimitives(path));
|
---|
389 | }
|
---|
390 |
|
---|
391 | @Override
|
---|
392 | public void updateEnabledState() {
|
---|
393 | setEnabled(!undoTree.isSelectionEmpty() || !redoTree.isSelectionEmpty());
|
---|
394 | }
|
---|
395 | }
|
---|
396 |
|
---|
397 | /**
|
---|
398 | * Action that selects the objects that take part in a command, then zoom to them.
|
---|
399 | */
|
---|
400 | public class SelectAndZoomAction extends SelectAction {
|
---|
401 | /**
|
---|
402 | * Constructs a new {@code SelectAndZoomAction}.
|
---|
403 | */
|
---|
404 | public SelectAndZoomAction() {
|
---|
405 | putValue(NAME, tr("Select and zoom"));
|
---|
406 | putValue(SHORT_DESCRIPTION, tr("Selects the objects that take part in this command (unless currently deleted), then and zooms to it"));
|
---|
407 | putValue(SMALL_ICON, ImageProvider.get("dialogs/autoscale", "selection"));
|
---|
408 | }
|
---|
409 |
|
---|
410 | @Override
|
---|
411 | public void actionPerformed(ActionEvent e) {
|
---|
412 | super.actionPerformed(e);
|
---|
413 | if (!Main.main.hasEditLayer()) return;
|
---|
414 | AutoScaleAction.autoScale("selection");
|
---|
415 | }
|
---|
416 | }
|
---|
417 |
|
---|
418 | /**
|
---|
419 | * undo / redo switch to reduce duplicate code
|
---|
420 | */
|
---|
421 | protected enum UndoRedoType {UNDO, REDO}
|
---|
422 |
|
---|
423 | /**
|
---|
424 | * Action to undo or redo all commands up to (and including) the seleced item.
|
---|
425 | */
|
---|
426 | protected class UndoRedoAction extends AbstractAction implements IEnabledStateUpdating {
|
---|
427 | private UndoRedoType type;
|
---|
428 | private JTree tree;
|
---|
429 |
|
---|
430 | /**
|
---|
431 | * constructor
|
---|
432 | * @param type decide whether it is an undo action or a redo action
|
---|
433 | */
|
---|
434 | public UndoRedoAction(UndoRedoType type) {
|
---|
435 | super();
|
---|
436 | this.type = type;
|
---|
437 | switch (type) {
|
---|
438 | case UNDO:
|
---|
439 | tree = undoTree;
|
---|
440 | putValue(NAME, tr("Undo"));
|
---|
441 | putValue(SHORT_DESCRIPTION, tr("Undo the selected and all later commands"));
|
---|
442 | putValue(SMALL_ICON, ImageProvider.get("undo"));
|
---|
443 | break;
|
---|
444 | case REDO:
|
---|
445 | tree = redoTree;
|
---|
446 | putValue(NAME, tr("Redo"));
|
---|
447 | putValue(SHORT_DESCRIPTION, tr("Redo the selected and all earlier commands"));
|
---|
448 | putValue(SMALL_ICON, ImageProvider.get("redo"));
|
---|
449 | break;
|
---|
450 | }
|
---|
451 | }
|
---|
452 |
|
---|
453 | @Override
|
---|
454 | public void actionPerformed(ActionEvent e) {
|
---|
455 | lastOperation = type;
|
---|
456 | TreePath path = tree.getSelectionPath();
|
---|
457 |
|
---|
458 | // we can only undo top level commands
|
---|
459 | if (path.getPathCount() != 2)
|
---|
460 | throw new IllegalStateException();
|
---|
461 |
|
---|
462 | int idx = ((CommandListMutableTreeNode) path.getLastPathComponent()).getIndex();
|
---|
463 |
|
---|
464 | // calculate the number of commands to undo/redo; then do it
|
---|
465 | switch (type) {
|
---|
466 | case UNDO:
|
---|
467 | int numUndo = ((DefaultMutableTreeNode) undoTreeModel.getRoot()).getChildCount() - idx;
|
---|
468 | Main.main.undoRedo.undo(numUndo);
|
---|
469 | break;
|
---|
470 | case REDO:
|
---|
471 | int numRedo = idx+1;
|
---|
472 | Main.main.undoRedo.redo(numRedo);
|
---|
473 | break;
|
---|
474 | }
|
---|
475 | Main.map.repaint();
|
---|
476 | }
|
---|
477 |
|
---|
478 | @Override
|
---|
479 | public void updateEnabledState() {
|
---|
480 | // do not allow execution if nothing is selected or a sub command was selected
|
---|
481 | setEnabled(!tree.isSelectionEmpty() && tree.getSelectionPath().getPathCount() == 2);
|
---|
482 | }
|
---|
483 | }
|
---|
484 |
|
---|
485 | class MouseEventHandler extends PopupMenuLauncher {
|
---|
486 |
|
---|
487 | public MouseEventHandler() {
|
---|
488 | super(new CommandStackPopup());
|
---|
489 | }
|
---|
490 |
|
---|
491 | @Override
|
---|
492 | public void mouseClicked(MouseEvent evt) {
|
---|
493 | if (isDoubleClick(evt)) {
|
---|
494 | selectAndZoomAction.actionPerformed(null);
|
---|
495 | }
|
---|
496 | }
|
---|
497 | }
|
---|
498 |
|
---|
499 | private class CommandStackPopup extends JPopupMenu {
|
---|
500 | public CommandStackPopup() {
|
---|
501 | add(selectAction);
|
---|
502 | add(selectAndZoomAction);
|
---|
503 | }
|
---|
504 | }
|
---|
505 | }
|
---|