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