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

Last change on this file since 6890 was 6413, checked in by Don-vip, 10 years ago

undo/redo: param check + javadoc

  • Property svn:eol-style set to native
File size: 5.9 KB
Line 
1//License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data;
3
4import java.util.Collection;
5import java.util.Iterator;
6import java.util.LinkedList;
7
8import org.openstreetmap.josm.Main;
9import org.openstreetmap.josm.command.Command;
10import org.openstreetmap.josm.data.osm.OsmPrimitive;
11import org.openstreetmap.josm.gui.MapView;
12import org.openstreetmap.josm.gui.layer.Layer;
13import org.openstreetmap.josm.gui.layer.OsmDataLayer.CommandQueueListener;
14import org.openstreetmap.josm.tools.CheckParameterUtil;
15
16public class UndoRedoHandler implements MapView.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 public final LinkedList<Command> redoCommands = new LinkedList<Command>();
26
27 private final LinkedList<CommandQueueListener> listenerCommands = new LinkedList<CommandQueueListener>();
28
29 /**
30 * Constructs a new {@code UndoRedoHandler}.
31 */
32 public UndoRedoHandler() {
33 MapView.addLayerChangeListener(this);
34 }
35
36 /**
37 * Executes the command and add it to the intern command queue.
38 * @param c The command to execute. Must not be {@code null}.
39 */
40 public void addNoRedraw(final Command c) {
41 CheckParameterUtil.ensureParameterNotNull(c, "c");
42 c.executeCommand();
43 commands.add(c);
44 // Limit the number of commands in the undo list.
45 // Currently you have to undo the commands one by one. If
46 // this changes, a higher default value may be reasonable.
47 if (commands.size() > Main.pref.getInteger("undo.max", 1000)) {
48 commands.removeFirst();
49 }
50 redoCommands.clear();
51 }
52
53 public void afterAdd() {
54 fireCommandsChanged();
55
56 // the command may have changed the selection so tell the listeners about the current situation
57 Main.main.getCurrentDataSet().fireSelectionChanged();
58 }
59
60 /**
61 * Executes the command and add it to the intern command queue.
62 * @param c The command to execute. Must not be {@code null}.
63 */
64 synchronized public void add(final Command c) {
65 addNoRedraw(c);
66 afterAdd();
67 }
68
69 /**
70 * Undoes the last added command.
71 */
72 public void undo() {
73 undo(1);
74 }
75
76 /**
77 * Undoes multiple commands.
78 * @param num The number of commands to undo
79 */
80 synchronized public void undo(int num) {
81 if (commands.isEmpty())
82 return;
83 Collection<? extends OsmPrimitive> oldSelection = Main.main.getCurrentDataSet().getSelected();
84 Main.main.getCurrentDataSet().beginUpdate();
85 try {
86 for (int i=1; i<=num; ++i) {
87 final Command c = commands.removeLast();
88 c.undoCommand();
89 redoCommands.addFirst(c);
90 if (commands.isEmpty()) {
91 break;
92 }
93 }
94 }
95 finally {
96 Main.main.getCurrentDataSet().endUpdate();
97 }
98 fireCommandsChanged();
99 Collection<? extends OsmPrimitive> newSelection = Main.main.getCurrentDataSet().getSelected();
100 if (!oldSelection.equals(newSelection)) {
101 Main.main.getCurrentDataSet().fireSelectionChanged();
102 }
103 }
104
105 /**
106 * Redoes the last undoed command.
107 */
108 public void redo() {
109 redo(1);
110 }
111
112 /**
113 * Redoes multiple commands.
114 * @param num The number of commands to redo
115 */
116 public void redo(int num) {
117 if (redoCommands.isEmpty())
118 return;
119 Collection<? extends OsmPrimitive> oldSelection = Main.main.getCurrentDataSet().getSelected();
120 for (int i=0; i<num; ++i) {
121 final Command c = redoCommands.removeFirst();
122 c.executeCommand();
123 commands.add(c);
124 if (redoCommands.isEmpty()) {
125 break;
126 }
127 }
128 fireCommandsChanged();
129 Collection<? extends OsmPrimitive> newSelection = Main.main.getCurrentDataSet().getSelected();
130 if (!oldSelection.equals(newSelection)) {
131 Main.main.getCurrentDataSet().fireSelectionChanged();
132 }
133 }
134
135 public void fireCommandsChanged() {
136 for (final CommandQueueListener l : listenerCommands) {
137 l.commandChanged(commands.size(), redoCommands.size());
138 }
139 }
140
141 public void clean() {
142 redoCommands.clear();
143 commands.clear();
144 fireCommandsChanged();
145 }
146
147 public void clean(Layer layer) {
148 if (layer == null)
149 return;
150 boolean changed = false;
151 for (Iterator<Command> it = commands.iterator(); it.hasNext();) {
152 if (it.next().invalidBecauselayerRemoved(layer)) {
153 it.remove();
154 changed = true;
155 }
156 }
157 for (Iterator<Command> it = redoCommands.iterator(); it.hasNext();) {
158 if (it.next().invalidBecauselayerRemoved(layer)) {
159 it.remove();
160 changed = true;
161 }
162 }
163 if (changed) {
164 fireCommandsChanged();
165 }
166 }
167
168 @Override
169 public void layerRemoved(Layer oldLayer) {
170 clean(oldLayer);
171 }
172
173 @Override
174 public void layerAdded(Layer newLayer) {}
175 @Override
176 public void activeLayerChange(Layer oldLayer, Layer newLayer) {}
177
178 /**
179 * Removes a command queue listener.
180 * @param l The command queue listener to remove
181 */
182 public void removeCommandQueueListener(CommandQueueListener l) {
183 listenerCommands.remove(l);
184 }
185
186 /**
187 * Adds a command queue listener.
188 * @param l The commands queue listener to add
189 * @return {@code true} if the listener has been added, {@code false} otherwise
190 */
191 public boolean addCommandQueueListener(CommandQueueListener l) {
192 return listenerCommands.add(l);
193 }
194}
Note: See TracBrowser for help on using the repository browser.