source: josm/trunk/src/org/openstreetmap/josm/gui/dialogs/CommandStackDialog.java@ 3275

Last change on this file since 3275 was 3275, checked in by bastiK, 14 years ago

remove unnecessary java 6 usage

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