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