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

Last change on this file since 9870 was 8510, checked in by Don-vip, 9 years ago

checkstyle: enable relevant whitespace checks and fix them

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