source: josm/trunk/src/org/openstreetmap/josm/command/Command.java@ 9827

Last change on this file since 9827 was 9371, checked in by simon04, 8 years ago

Java 7: use Objects.equals and Objects.hash

  • Property svn:eol-style set to native
File size: 10.1 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.command;
3
4import java.awt.GridBagLayout;
5import java.util.ArrayList;
6import java.util.Collection;
7import java.util.HashMap;
8import java.util.LinkedHashMap;
9import java.util.Map;
10import java.util.Map.Entry;
11import java.util.Objects;
12
13import javax.swing.JOptionPane;
14import javax.swing.JPanel;
15
16import org.openstreetmap.josm.Main;
17import org.openstreetmap.josm.data.coor.EastNorth;
18import org.openstreetmap.josm.data.coor.LatLon;
19import org.openstreetmap.josm.data.osm.Node;
20import org.openstreetmap.josm.data.osm.OsmPrimitive;
21import org.openstreetmap.josm.data.osm.PrimitiveData;
22import org.openstreetmap.josm.data.osm.Relation;
23import org.openstreetmap.josm.data.osm.Way;
24import org.openstreetmap.josm.data.osm.visitor.AbstractVisitor;
25import org.openstreetmap.josm.gui.ConditionalOptionPaneUtil;
26import org.openstreetmap.josm.gui.layer.Layer;
27import org.openstreetmap.josm.gui.layer.OsmDataLayer;
28import org.openstreetmap.josm.gui.widgets.JMultilineLabel;
29import org.openstreetmap.josm.tools.CheckParameterUtil;
30
31/**
32 * Classes implementing Command modify a dataset in a specific way. A command is
33 * one atomic action on a specific dataset, such as move or delete.
34 *
35 * The command remembers the {@link OsmDataLayer} it is operating on.
36 *
37 * @author imi
38 */
39public abstract class Command extends PseudoCommand {
40
41 private static final class CloneVisitor extends AbstractVisitor {
42 public final Map<OsmPrimitive, PrimitiveData> orig = new LinkedHashMap<>();
43
44 @Override
45 public void visit(Node n) {
46 orig.put(n, n.save());
47 }
48
49 @Override
50 public void visit(Way w) {
51 orig.put(w, w.save());
52 }
53
54 @Override
55 public void visit(Relation e) {
56 orig.put(e, e.save());
57 }
58 }
59
60 /**
61 * Small helper for holding the interesting part of the old data state of the objects.
62 */
63 public static class OldNodeState {
64
65 private final LatLon latlon;
66 private final EastNorth eastNorth; // cached EastNorth to be used for applying exact displacement
67 private final boolean modified;
68
69 /**
70 * Constructs a new {@code OldNodeState} for the given node.
71 * @param node The node whose state has to be remembered
72 */
73 public OldNodeState(Node node) {
74 latlon = node.getCoor();
75 eastNorth = node.getEastNorth();
76 modified = node.isModified();
77 }
78
79 /**
80 * Returns old lat/lon.
81 * @return old lat/lon
82 * @see Node#getCoor()
83 */
84 public final LatLon getLatlon() {
85 return latlon;
86 }
87
88 /**
89 * Returns old east/north.
90 * @return old east/north
91 * @see Node#getEastNorth()
92 */
93 public final EastNorth getEastNorth() {
94 return eastNorth;
95 }
96
97 /**
98 * Returns old modified state.
99 * @return old modified state
100 * @see Node #isModified()
101 */
102 public final boolean isModified() {
103 return modified;
104 }
105
106 @Override
107 public int hashCode() {
108 return Objects.hash(latlon, eastNorth, modified);
109 }
110
111 @Override
112 public boolean equals(Object obj) {
113 if (this == obj) return true;
114 if (obj == null || getClass() != obj.getClass()) return false;
115 OldNodeState that = (OldNodeState) obj;
116 return modified == that.modified &&
117 Objects.equals(latlon, that.latlon) &&
118 Objects.equals(eastNorth, that.eastNorth);
119 }
120 }
121
122 /** the map of OsmPrimitives in the original state to OsmPrimitives in cloned state */
123 private Map<OsmPrimitive, PrimitiveData> cloneMap = new HashMap<>();
124
125 /** the layer which this command is applied to */
126 private final OsmDataLayer layer;
127
128 /**
129 * Creates a new command in the context of the current edit layer, if any
130 */
131 public Command() {
132 this.layer = Main.main == null ? null : Main.main.getEditLayer();
133 }
134
135 /**
136 * Creates a new command in the context of a specific data layer
137 *
138 * @param layer the data layer. Must not be null.
139 * @throws IllegalArgumentException if layer is null
140 */
141 public Command(OsmDataLayer layer) {
142 CheckParameterUtil.ensureParameterNotNull(layer, "layer");
143 this.layer = layer;
144 }
145
146 /**
147 * Executes the command on the dataset. This implementation will remember all
148 * primitives returned by fillModifiedData for restoring them on undo.
149 * @return true
150 */
151 public boolean executeCommand() {
152 CloneVisitor visitor = new CloneVisitor();
153 Collection<OsmPrimitive> all = new ArrayList<>();
154 fillModifiedData(all, all, all);
155 for (OsmPrimitive osm : all) {
156 osm.accept(visitor);
157 }
158 cloneMap = visitor.orig;
159 return true;
160 }
161
162 /**
163 * Undoes the command.
164 * It can be assumed that all objects are in the same state they were before.
165 * It can also be assumed that executeCommand was called exactly once before.
166 *
167 * This implementation undoes all objects stored by a former call to executeCommand.
168 */
169 public void undoCommand() {
170 for (Entry<OsmPrimitive, PrimitiveData> e : cloneMap.entrySet()) {
171 OsmPrimitive primitive = e.getKey();
172 if (primitive.getDataSet() != null) {
173 e.getKey().load(e.getValue());
174 }
175 }
176 }
177
178 /**
179 * Called when a layer has been removed to have the command remove itself from
180 * any buffer if it is not longer applicable to the dataset (e.g. it was part of
181 * the removed layer)
182 *
183 * @param oldLayer the old layer
184 * @return true if this command
185 */
186 public boolean invalidBecauselayerRemoved(Layer oldLayer) {
187 if (!(oldLayer instanceof OsmDataLayer))
188 return false;
189 return layer == oldLayer;
190 }
191
192 /**
193 * Lets other commands access the original version
194 * of the object. Usually for undoing.
195 * @param osm The requested OSM object
196 * @return The original version of the requested object, if any
197 */
198 public PrimitiveData getOrig(OsmPrimitive osm) {
199 return cloneMap.get(osm);
200 }
201
202 /**
203 * Replies the layer this command is (or was) applied to.
204 * @return the layer this command is (or was) applied to
205 */
206 protected OsmDataLayer getLayer() {
207 return layer;
208 }
209
210 /**
211 * Fill in the changed data this command operates on.
212 * Add to the lists, don't clear them.
213 *
214 * @param modified The modified primitives
215 * @param deleted The deleted primitives
216 * @param added The added primitives
217 */
218 public abstract void fillModifiedData(Collection<OsmPrimitive> modified,
219 Collection<OsmPrimitive> deleted,
220 Collection<OsmPrimitive> added);
221
222 /**
223 * Return the primitives that take part in this command.
224 * The collection is computed during execution.
225 */
226 @Override
227 public Collection<? extends OsmPrimitive> getParticipatingPrimitives() {
228 return cloneMap.keySet();
229 }
230
231 /**
232 * Check whether user is about to operate on data outside of the download area.
233 * Request confirmation if he is.
234 *
235 * @param operation the operation name which is used for setting some preferences
236 * @param dialogTitle the title of the dialog being displayed
237 * @param outsideDialogMessage the message text to be displayed when data is outside of the download area
238 * @param incompleteDialogMessage the message text to be displayed when data is incomplete
239 * @param primitives the primitives to operate on
240 * @param ignore {@code null} or a primitive to be ignored
241 * @return true, if operating on outlying primitives is OK; false, otherwise
242 */
243 public static boolean checkAndConfirmOutlyingOperation(String operation,
244 String dialogTitle, String outsideDialogMessage, String incompleteDialogMessage,
245 Collection<? extends OsmPrimitive> primitives,
246 Collection<? extends OsmPrimitive> ignore) {
247 boolean outside = false;
248 boolean incomplete = false;
249 for (OsmPrimitive osm : primitives) {
250 if (osm.isIncomplete()) {
251 incomplete = true;
252 } else if (osm.isOutsideDownloadArea()
253 && (ignore == null || !ignore.contains(osm))) {
254 outside = true;
255 }
256 }
257 if (outside) {
258 JPanel msg = new JPanel(new GridBagLayout());
259 msg.add(new JMultilineLabel("<html>" + outsideDialogMessage + "</html>"));
260 boolean answer = ConditionalOptionPaneUtil.showConfirmationDialog(
261 operation + "_outside_nodes",
262 Main.parent,
263 msg,
264 dialogTitle,
265 JOptionPane.YES_NO_OPTION,
266 JOptionPane.QUESTION_MESSAGE,
267 JOptionPane.YES_OPTION);
268 if (!answer)
269 return false;
270 }
271 if (incomplete) {
272 JPanel msg = new JPanel(new GridBagLayout());
273 msg.add(new JMultilineLabel("<html>" + incompleteDialogMessage + "</html>"));
274 boolean answer = ConditionalOptionPaneUtil.showConfirmationDialog(
275 operation + "_incomplete",
276 Main.parent,
277 msg,
278 dialogTitle,
279 JOptionPane.YES_NO_OPTION,
280 JOptionPane.QUESTION_MESSAGE,
281 JOptionPane.YES_OPTION);
282 if (!answer)
283 return false;
284 }
285 return true;
286 }
287
288 @Override
289 public int hashCode() {
290 return Objects.hash(cloneMap, layer);
291 }
292
293 @Override
294 public boolean equals(Object obj) {
295 if (this == obj) return true;
296 if (obj == null || getClass() != obj.getClass()) return false;
297 Command command = (Command) obj;
298 return Objects.equals(cloneMap, command.cloneMap) &&
299 Objects.equals(layer, command.layer);
300 }
301}
Note: See TracBrowser for help on using the repository browser.