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

Last change on this file since 10452 was 10452, checked in by Don-vip, 8 years ago

fix #13019 - Make commands trigger an implicit layer redraw (patch by michael2402) - gsoc-core

  • Property svn:eol-style set to native
File size: 7.0 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.DataSet;
11import org.openstreetmap.josm.data.osm.OsmPrimitive;
12import org.openstreetmap.josm.gui.layer.Layer;
13import org.openstreetmap.josm.gui.layer.LayerManager.LayerAddEvent;
14import org.openstreetmap.josm.gui.layer.LayerManager.LayerChangeListener;
15import org.openstreetmap.josm.gui.layer.LayerManager.LayerOrderChangeEvent;
16import org.openstreetmap.josm.gui.layer.LayerManager.LayerRemoveEvent;
17import org.openstreetmap.josm.gui.layer.OsmDataLayer;
18import org.openstreetmap.josm.gui.layer.OsmDataLayer.CommandQueueListener;
19import org.openstreetmap.josm.tools.CheckParameterUtil;
20
21/**
22 * This is the global undo/redo handler for all {@link OsmDataLayer}s.
23 * <p>
24 * If you want to change a data layer, you can use {@link #add(Command)} to execute a command on it and make that command undoable.
25 */
26public class UndoRedoHandler implements LayerChangeListener {
27
28 /**
29 * All commands that were made on the dataset. Don't write from outside!
30 */
31 public final LinkedList<Command> commands = new LinkedList<>();
32 /**
33 * The stack for redoing commands
34 */
35 public final LinkedList<Command> redoCommands = new LinkedList<>();
36
37 private final LinkedList<CommandQueueListener> listenerCommands = new LinkedList<>();
38
39 /**
40 * Constructs a new {@code UndoRedoHandler}.
41 */
42 public UndoRedoHandler() {
43 Main.getLayerManager().addLayerChangeListener(this);
44 }
45
46 /**
47 * Executes the command and add it to the intern command queue.
48 * @param c The command to execute. Must not be {@code null}.
49 */
50 public void addNoRedraw(final Command c) {
51 CheckParameterUtil.ensureParameterNotNull(c, "c");
52 c.executeCommand();
53 c.invalidateAffectedLayers();
54 commands.add(c);
55 // Limit the number of commands in the undo list.
56 // Currently you have to undo the commands one by one. If
57 // this changes, a higher default value may be reasonable.
58 if (commands.size() > Main.pref.getInteger("undo.max", 1000)) {
59 commands.removeFirst();
60 }
61 redoCommands.clear();
62 }
63
64 /**
65 * Fires a commands change event after adding a command.
66 */
67 public void afterAdd() {
68 fireCommandsChanged();
69 }
70
71 /**
72 * Executes the command and add it to the intern command queue.
73 * @param c The command to execute. Must not be {@code null}.
74 */
75 public synchronized void add(final Command c) {
76 DataSet ds = Main.getLayerManager().getEditDataSet();
77 Collection<? extends OsmPrimitive> oldSelection = ds.getSelected();
78 addNoRedraw(c);
79 afterAdd();
80
81 // the command may have changed the selection so tell the listeners about the current situation
82 fireIfSelectionChanged(ds, oldSelection);
83 }
84
85 /**
86 * Undoes the last added command.
87 */
88 public void undo() {
89 undo(1);
90 }
91
92 /**
93 * Undoes multiple commands.
94 * @param num The number of commands to undo
95 */
96 public synchronized void undo(int num) {
97 if (commands.isEmpty())
98 return;
99 DataSet ds = Main.getLayerManager().getEditDataSet();
100 Collection<? extends OsmPrimitive> oldSelection = ds.getSelected();
101 ds.beginUpdate();
102 try {
103 for (int i = 1; i <= num; ++i) {
104 final Command c = commands.removeLast();
105 c.undoCommand();
106 c.invalidateAffectedLayers();
107 redoCommands.addFirst(c);
108 if (commands.isEmpty()) {
109 break;
110 }
111 }
112 } finally {
113 ds.endUpdate();
114 }
115 fireCommandsChanged();
116 fireIfSelectionChanged(ds, oldSelection);
117 }
118
119 /**
120 * Redoes the last undoed command.
121 */
122 public void redo() {
123 redo(1);
124 }
125
126 /**
127 * Redoes multiple commands.
128 * @param num The number of commands to redo
129 */
130 public void redo(int num) {
131 if (redoCommands.isEmpty())
132 return;
133 DataSet ds = Main.getLayerManager().getEditDataSet();
134 Collection<? extends OsmPrimitive> oldSelection = ds.getSelected();
135 for (int i = 0; i < num; ++i) {
136 final Command c = redoCommands.removeFirst();
137 c.executeCommand();
138 c.invalidateAffectedLayers();
139 commands.add(c);
140 if (redoCommands.isEmpty()) {
141 break;
142 }
143 }
144 fireCommandsChanged();
145 fireIfSelectionChanged(ds, oldSelection);
146 }
147
148 private static void fireIfSelectionChanged(DataSet ds, Collection<? extends OsmPrimitive> oldSelection) {
149 Collection<? extends OsmPrimitive> newSelection = ds.getSelected();
150 if (!oldSelection.equals(newSelection)) {
151 ds.fireSelectionChanged();
152 }
153 }
154
155 /**
156 * Fires a command change to all listeners.
157 */
158 private void fireCommandsChanged() {
159 for (final CommandQueueListener l : listenerCommands) {
160 l.commandChanged(commands.size(), redoCommands.size());
161 }
162 }
163
164 /**
165 * Resets the undo/redo list.
166 */
167 public void clean() {
168 redoCommands.clear();
169 commands.clear();
170 fireCommandsChanged();
171 }
172
173 /**
174 * Resets all commands that affect the given layer.
175 * @param layer The layer that was affected.
176 */
177 public void clean(Layer layer) {
178 if (layer == null)
179 return;
180 boolean changed = false;
181 for (Iterator<Command> it = commands.iterator(); it.hasNext();) {
182 if (it.next().invalidBecauselayerRemoved(layer)) {
183 it.remove();
184 changed = true;
185 }
186 }
187 for (Iterator<Command> it = redoCommands.iterator(); it.hasNext();) {
188 if (it.next().invalidBecauselayerRemoved(layer)) {
189 it.remove();
190 changed = true;
191 }
192 }
193 if (changed) {
194 fireCommandsChanged();
195 }
196 }
197
198 @Override
199 public void layerRemoving(LayerRemoveEvent e) {
200 clean(e.getRemovedLayer());
201 }
202
203 @Override
204 public void layerAdded(LayerAddEvent e) {
205 // Do nothing
206 }
207
208 @Override
209 public void layerOrderChanged(LayerOrderChangeEvent e) {
210 // Do nothing
211 }
212
213 /**
214 * Removes a command queue listener.
215 * @param l The command queue listener to remove
216 */
217 public void removeCommandQueueListener(CommandQueueListener l) {
218 listenerCommands.remove(l);
219 }
220
221 /**
222 * Adds a command queue listener.
223 * @param l The commands queue listener to add
224 * @return {@code true} if the listener has been added, {@code false} otherwise
225 */
226 public boolean addCommandQueueListener(CommandQueueListener l) {
227 return listenerCommands.add(l);
228 }
229}
Note: See TracBrowser for help on using the repository browser.