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