source: josm/trunk/src/org/openstreetmap/josm/data/UndoRedoHandler.java@ 2285

Last change on this file since 2285 was 2098, checked in by Gubaer, 15 years ago

see #3347: patches by singularita@…: Merging nodes, fixing duplicate nodes, unglueing ways and splitting ways is very slow with large datasets

  • Property svn:eol-style set to native
File size: 4.1 KB
Line 
1//License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.data;
3
4import java.util.Iterator;
5import java.util.LinkedList;
6import java.util.Stack;
7
8import org.openstreetmap.josm.Main;
9import org.openstreetmap.josm.command.Command;
10import org.openstreetmap.josm.data.osm.DataSet;
11import org.openstreetmap.josm.gui.layer.Layer;
12import org.openstreetmap.josm.gui.layer.OsmDataLayer;
13import org.openstreetmap.josm.gui.layer.Layer.LayerChangeListener;
14import org.openstreetmap.josm.gui.layer.OsmDataLayer.CommandQueueListener;
15
16public class UndoRedoHandler implements LayerChangeListener {
17
18 /**
19 * All commands that were made on the dataset. Don't write from outside!
20 */
21 public final LinkedList<Command> commands = new LinkedList<Command>();
22 /**
23 * The stack for redoing commands
24 */
25 private final Stack<Command> redoCommands = new Stack<Command>();
26
27 public final LinkedList<CommandQueueListener> listenerCommands = new LinkedList<CommandQueueListener>();
28
29
30 public UndoRedoHandler() {
31 Layer.listeners.add(this);
32 }
33
34
35 /**
36 * Execute the command and add it to the intern command queue.
37 */
38 public void addNoRedraw(final Command c) {
39 c.executeCommand();
40 commands.add(c);
41 redoCommands.clear();
42 }
43
44 public void afterAdd() {
45 if (Main.map != null && Main.map.mapView.getActiveLayer() instanceof OsmDataLayer) {
46 OsmDataLayer data = (OsmDataLayer)Main.map.mapView.getActiveLayer();
47 data.fireDataChange();
48 }
49 fireCommandsChanged();
50
51 // the command may have changed the selection so tell the listeners about the current situation
52 DataSet.fireSelectionChanged(Main.main.getCurrentDataSet().getSelected());
53 }
54
55 /**
56 * Execute the command and add it to the intern command queue.
57 */
58 public void add(final Command c) {
59 addNoRedraw(c);
60 afterAdd();
61 }
62
63 /**
64 * Undoes the last added command.
65 */
66 public void undo() {
67 if (commands.isEmpty())
68 return;
69 final Command c = commands.removeLast();
70 c.undoCommand();
71 redoCommands.push(c);
72 if (Main.map != null && Main.map.mapView.getActiveLayer() instanceof OsmDataLayer) {
73 OsmDataLayer data = (OsmDataLayer)Main.map.mapView.getActiveLayer();
74 data.fireDataChange();
75 }
76 fireCommandsChanged();
77 Main.main.getCurrentDataSet().setSelected();
78 }
79
80 /**
81 * Redoes the last undoed command.
82 * TODO: This has to be moved to a central place in order to support multiple layers.
83 */
84 public void redo() {
85 if (redoCommands.isEmpty())
86 return;
87 final Command c = redoCommands.pop();
88 c.executeCommand();
89 commands.add(c);
90 if (Main.map != null && Main.map.mapView.getActiveLayer() instanceof OsmDataLayer) {
91 OsmDataLayer data = (OsmDataLayer)Main.map.mapView.getActiveLayer();
92 data.fireDataChange();
93 }
94 fireCommandsChanged();
95 }
96
97 public void fireCommandsChanged() {
98 for (final CommandQueueListener l : listenerCommands) {
99 l.commandChanged(commands.size(), redoCommands.size());
100 }
101 }
102
103 public void clean() {
104 redoCommands.clear();
105 commands.clear();
106 fireCommandsChanged();
107 }
108
109 public void clean(Layer layer) {
110 if (layer == null)
111 return;
112 boolean changed = false;
113 for (Iterator<Command> it = commands.iterator(); it.hasNext();) {
114 if (it.next().invalidBecauselayerRemoved(layer)) {
115 it.remove();
116 changed = true;
117 }
118 }
119 for (Iterator<Command> it = redoCommands.iterator(); it.hasNext();) {
120 if (it.next().invalidBecauselayerRemoved(layer)) {
121 it.remove();
122 changed = true;
123 }
124 }
125 if (changed) {
126 fireCommandsChanged();
127 }
128 }
129
130 public void layerRemoved(Layer oldLayer) {
131 clean(oldLayer);
132 }
133
134 public void layerAdded(Layer newLayer) {}
135 public void activeLayerChange(Layer oldLayer, Layer newLayer) {}
136}
Note: See TracBrowser for help on using the repository browser.