1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.command;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.marktr;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
6 | import static org.openstreetmap.josm.tools.I18n.trn;
|
---|
7 |
|
---|
8 | import java.awt.GridBagLayout;
|
---|
9 | import java.util.ArrayList;
|
---|
10 | import java.util.Collection;
|
---|
11 | import java.util.Collections;
|
---|
12 | import java.util.EnumSet;
|
---|
13 | import java.util.HashMap;
|
---|
14 | import java.util.HashSet;
|
---|
15 | import java.util.Iterator;
|
---|
16 | import java.util.LinkedList;
|
---|
17 | import java.util.List;
|
---|
18 | import java.util.Map;
|
---|
19 | import java.util.Map.Entry;
|
---|
20 | import java.util.Set;
|
---|
21 |
|
---|
22 | import javax.swing.Icon;
|
---|
23 | import javax.swing.JOptionPane;
|
---|
24 | import javax.swing.JPanel;
|
---|
25 |
|
---|
26 | import org.openstreetmap.josm.Main;
|
---|
27 | import org.openstreetmap.josm.actions.SplitWayAction;
|
---|
28 | import org.openstreetmap.josm.data.osm.Node;
|
---|
29 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
30 | import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
|
---|
31 | import org.openstreetmap.josm.data.osm.PrimitiveData;
|
---|
32 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
33 | import org.openstreetmap.josm.data.osm.RelationToChildReference;
|
---|
34 | import org.openstreetmap.josm.data.osm.Way;
|
---|
35 | import org.openstreetmap.josm.data.osm.WaySegment;
|
---|
36 | import org.openstreetmap.josm.gui.ConditionalOptionPaneUtil;
|
---|
37 | import org.openstreetmap.josm.gui.DefaultNameFormatter;
|
---|
38 | import org.openstreetmap.josm.gui.dialogs.DeleteFromRelationConfirmationDialog;
|
---|
39 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
40 | import org.openstreetmap.josm.gui.widgets.JMultilineLabel;
|
---|
41 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
42 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
43 | import org.openstreetmap.josm.tools.Utils;
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * A command to delete a number of primitives from the dataset.
|
---|
47 | * @since 23
|
---|
48 | */
|
---|
49 | public class DeleteCommand extends Command {
|
---|
50 | /**
|
---|
51 | * The primitives that get deleted.
|
---|
52 | */
|
---|
53 | private final Collection<? extends OsmPrimitive> toDelete;
|
---|
54 | private final Map<OsmPrimitive, PrimitiveData> clonedPrimitives = new HashMap<>();
|
---|
55 |
|
---|
56 | /**
|
---|
57 | * Constructor. Deletes a collection of primitives in the current edit layer.
|
---|
58 | *
|
---|
59 | * @param data the primitives to delete. Must neither be null nor empty.
|
---|
60 | * @throws IllegalArgumentException if data is null or empty
|
---|
61 | */
|
---|
62 | public DeleteCommand(Collection<? extends OsmPrimitive> data) {
|
---|
63 | CheckParameterUtil.ensureParameterNotNull(data, "data");
|
---|
64 | if (data.isEmpty())
|
---|
65 | throw new IllegalArgumentException(tr("At least one object to delete required, got empty collection"));
|
---|
66 | this.toDelete = data;
|
---|
67 | checkConsistency();
|
---|
68 | }
|
---|
69 |
|
---|
70 | /**
|
---|
71 | * Constructor. Deletes a single primitive in the current edit layer.
|
---|
72 | *
|
---|
73 | * @param data the primitive to delete. Must not be null.
|
---|
74 | * @throws IllegalArgumentException if data is null
|
---|
75 | */
|
---|
76 | public DeleteCommand(OsmPrimitive data) {
|
---|
77 | this(Collections.singleton(data));
|
---|
78 | }
|
---|
79 |
|
---|
80 | /**
|
---|
81 | * Constructor for a single data item. Use the collection constructor to delete multiple
|
---|
82 | * objects.
|
---|
83 | *
|
---|
84 | * @param layer the layer context for deleting this primitive. Must not be null.
|
---|
85 | * @param data the primitive to delete. Must not be null.
|
---|
86 | * @throws IllegalArgumentException if data is null
|
---|
87 | * @throws IllegalArgumentException if layer is null
|
---|
88 | */
|
---|
89 | public DeleteCommand(OsmDataLayer layer, OsmPrimitive data) {
|
---|
90 | this(layer, Collections.singleton(data));
|
---|
91 | }
|
---|
92 |
|
---|
93 | /**
|
---|
94 | * Constructor for a collection of data to be deleted in the context of
|
---|
95 | * a specific layer
|
---|
96 | *
|
---|
97 | * @param layer the layer context for deleting these primitives. Must not be null.
|
---|
98 | * @param data the primitives to delete. Must neither be null nor empty.
|
---|
99 | * @throws IllegalArgumentException if layer is null
|
---|
100 | * @throws IllegalArgumentException if data is null or empty
|
---|
101 | */
|
---|
102 | public DeleteCommand(OsmDataLayer layer, Collection<? extends OsmPrimitive> data) {
|
---|
103 | super(layer);
|
---|
104 | CheckParameterUtil.ensureParameterNotNull(data, "data");
|
---|
105 | if (data.isEmpty())
|
---|
106 | throw new IllegalArgumentException(tr("At least one object to delete required, got empty collection"));
|
---|
107 | this.toDelete = data;
|
---|
108 | checkConsistency();
|
---|
109 | }
|
---|
110 |
|
---|
111 | private void checkConsistency() {
|
---|
112 | for (OsmPrimitive p : toDelete) {
|
---|
113 | if (p == null) {
|
---|
114 | throw new IllegalArgumentException("Primitive to delete must not be null");
|
---|
115 | } else if (p.getDataSet() == null) {
|
---|
116 | throw new IllegalArgumentException("Primitive to delete must be in a dataset");
|
---|
117 | }
|
---|
118 | }
|
---|
119 | }
|
---|
120 |
|
---|
121 | @Override
|
---|
122 | public boolean executeCommand() {
|
---|
123 | // Make copy and remove all references (to prevent inconsistent dataset (delete referenced) while command is executed)
|
---|
124 | for (OsmPrimitive osm: toDelete) {
|
---|
125 | if (osm.isDeleted())
|
---|
126 | throw new IllegalArgumentException(osm + " is already deleted");
|
---|
127 | clonedPrimitives.put(osm, osm.save());
|
---|
128 |
|
---|
129 | if (osm instanceof Way) {
|
---|
130 | ((Way) osm).setNodes(null);
|
---|
131 | } else if (osm instanceof Relation) {
|
---|
132 | ((Relation) osm).setMembers(null);
|
---|
133 | }
|
---|
134 | }
|
---|
135 |
|
---|
136 | for (OsmPrimitive osm: toDelete) {
|
---|
137 | osm.setDeleted(true);
|
---|
138 | }
|
---|
139 |
|
---|
140 | return true;
|
---|
141 | }
|
---|
142 |
|
---|
143 | @Override
|
---|
144 | public void undoCommand() {
|
---|
145 | for (OsmPrimitive osm: toDelete) {
|
---|
146 | osm.setDeleted(false);
|
---|
147 | }
|
---|
148 |
|
---|
149 | for (Entry<OsmPrimitive, PrimitiveData> entry: clonedPrimitives.entrySet()) {
|
---|
150 | entry.getKey().load(entry.getValue());
|
---|
151 | }
|
---|
152 | }
|
---|
153 |
|
---|
154 | @Override
|
---|
155 | public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted,
|
---|
156 | Collection<OsmPrimitive> added) {
|
---|
157 | }
|
---|
158 |
|
---|
159 | private Set<OsmPrimitiveType> getTypesToDelete() {
|
---|
160 | Set<OsmPrimitiveType> typesToDelete = EnumSet.noneOf(OsmPrimitiveType.class);
|
---|
161 | for (OsmPrimitive osm : toDelete) {
|
---|
162 | typesToDelete.add(OsmPrimitiveType.from(osm));
|
---|
163 | }
|
---|
164 | return typesToDelete;
|
---|
165 | }
|
---|
166 |
|
---|
167 | @Override
|
---|
168 | public String getDescriptionText() {
|
---|
169 | if (toDelete.size() == 1) {
|
---|
170 | OsmPrimitive primitive = toDelete.iterator().next();
|
---|
171 | String msg = "";
|
---|
172 | switch(OsmPrimitiveType.from(primitive)) {
|
---|
173 | case NODE: msg = marktr("Delete node {0}"); break;
|
---|
174 | case WAY: msg = marktr("Delete way {0}"); break;
|
---|
175 | case RELATION:msg = marktr("Delete relation {0}"); break;
|
---|
176 | }
|
---|
177 |
|
---|
178 | return tr(msg, primitive.getDisplayName(DefaultNameFormatter.getInstance()));
|
---|
179 | } else {
|
---|
180 | Set<OsmPrimitiveType> typesToDelete = getTypesToDelete();
|
---|
181 | String msg = "";
|
---|
182 | if (typesToDelete.size() > 1) {
|
---|
183 | msg = trn("Delete {0} object", "Delete {0} objects", toDelete.size(), toDelete.size());
|
---|
184 | } else {
|
---|
185 | OsmPrimitiveType t = typesToDelete.iterator().next();
|
---|
186 | switch(t) {
|
---|
187 | case NODE: msg = trn("Delete {0} node", "Delete {0} nodes", toDelete.size(), toDelete.size()); break;
|
---|
188 | case WAY: msg = trn("Delete {0} way", "Delete {0} ways", toDelete.size(), toDelete.size()); break;
|
---|
189 | case RELATION: msg = trn("Delete {0} relation", "Delete {0} relations", toDelete.size(), toDelete.size()); break;
|
---|
190 | }
|
---|
191 | }
|
---|
192 | return msg;
|
---|
193 | }
|
---|
194 | }
|
---|
195 |
|
---|
196 | @Override
|
---|
197 | public Icon getDescriptionIcon() {
|
---|
198 | if (toDelete.size() == 1)
|
---|
199 | return ImageProvider.get(toDelete.iterator().next().getDisplayType());
|
---|
200 | Set<OsmPrimitiveType> typesToDelete = getTypesToDelete();
|
---|
201 | if (typesToDelete.size() > 1)
|
---|
202 | return ImageProvider.get("data", "object");
|
---|
203 | else
|
---|
204 | return ImageProvider.get(typesToDelete.iterator().next());
|
---|
205 | }
|
---|
206 |
|
---|
207 | @Override public Collection<PseudoCommand> getChildren() {
|
---|
208 | if (toDelete.size() == 1)
|
---|
209 | return null;
|
---|
210 | else {
|
---|
211 | List<PseudoCommand> children = new ArrayList<>(toDelete.size());
|
---|
212 | for (final OsmPrimitive osm : toDelete) {
|
---|
213 | children.add(new PseudoCommand() {
|
---|
214 |
|
---|
215 | @Override public String getDescriptionText() {
|
---|
216 | return tr("Deleted ''{0}''", osm.getDisplayName(DefaultNameFormatter.getInstance()));
|
---|
217 | }
|
---|
218 |
|
---|
219 | @Override public Icon getDescriptionIcon() {
|
---|
220 | return ImageProvider.get(osm.getDisplayType());
|
---|
221 | }
|
---|
222 |
|
---|
223 | @Override public Collection<? extends OsmPrimitive> getParticipatingPrimitives() {
|
---|
224 | return Collections.singleton(osm);
|
---|
225 | }
|
---|
226 |
|
---|
227 | });
|
---|
228 | }
|
---|
229 | return children;
|
---|
230 |
|
---|
231 | }
|
---|
232 | }
|
---|
233 |
|
---|
234 | @Override public Collection<? extends OsmPrimitive> getParticipatingPrimitives() {
|
---|
235 | return toDelete;
|
---|
236 | }
|
---|
237 |
|
---|
238 | /**
|
---|
239 | * Delete the primitives and everything they reference.
|
---|
240 | *
|
---|
241 | * If a node is deleted, the node and all ways and relations the node is part of are deleted as well.
|
---|
242 | * If a way is deleted, all relations the way is member of are also deleted.
|
---|
243 | * If a way is deleted, only the way and no nodes are deleted.
|
---|
244 | *
|
---|
245 | * @param layer the {@link OsmDataLayer} in whose context primitives are deleted. Must not be null.
|
---|
246 | * @param selection The list of all object to be deleted.
|
---|
247 | * @param silent Set to true if the user should not be bugged with additional dialogs
|
---|
248 | * @return command A command to perform the deletions, or null of there is nothing to delete.
|
---|
249 | * @throws IllegalArgumentException if layer is null
|
---|
250 | */
|
---|
251 | public static Command deleteWithReferences(OsmDataLayer layer, Collection<? extends OsmPrimitive> selection, boolean silent) {
|
---|
252 | CheckParameterUtil.ensureParameterNotNull(layer, "layer");
|
---|
253 | if (selection == null || selection.isEmpty()) return null;
|
---|
254 | Set<OsmPrimitive> parents = OsmPrimitive.getReferrer(selection);
|
---|
255 | parents.addAll(selection);
|
---|
256 |
|
---|
257 | if (parents.isEmpty())
|
---|
258 | return null;
|
---|
259 | if (!silent && !checkAndConfirmOutlyingDelete(parents, null))
|
---|
260 | return null;
|
---|
261 | return new DeleteCommand(layer, parents);
|
---|
262 | }
|
---|
263 |
|
---|
264 | /**
|
---|
265 | * Delete the primitives and everything they reference.
|
---|
266 | *
|
---|
267 | * If a node is deleted, the node and all ways and relations the node is part of are deleted as well.
|
---|
268 | * If a way is deleted, all relations the way is member of are also deleted.
|
---|
269 | * If a way is deleted, only the way and no nodes are deleted.
|
---|
270 | *
|
---|
271 | * @param layer the {@link OsmDataLayer} in whose context primitives are deleted. Must not be null.
|
---|
272 | * @param selection The list of all object to be deleted.
|
---|
273 | * @return command A command to perform the deletions, or null of there is nothing to delete.
|
---|
274 | * @throws IllegalArgumentException if layer is null
|
---|
275 | */
|
---|
276 | public static Command deleteWithReferences(OsmDataLayer layer, Collection<? extends OsmPrimitive> selection) {
|
---|
277 | return deleteWithReferences(layer, selection, false);
|
---|
278 | }
|
---|
279 |
|
---|
280 | /**
|
---|
281 | * Try to delete all given primitives.
|
---|
282 | *
|
---|
283 | * If a node is used by a way, it's removed from that way. If a node or a way is used by a
|
---|
284 | * relation, inform the user and do not delete.
|
---|
285 | *
|
---|
286 | * If this would cause ways with less than 2 nodes to be created, delete these ways instead. If
|
---|
287 | * they are part of a relation, inform the user and do not delete.
|
---|
288 | *
|
---|
289 | * @param layer the {@link OsmDataLayer} in whose context the primitives are deleted
|
---|
290 | * @param selection the objects to delete.
|
---|
291 | * @return command a command to perform the deletions, or null if there is nothing to delete.
|
---|
292 | */
|
---|
293 | public static Command delete(OsmDataLayer layer, Collection<? extends OsmPrimitive> selection) {
|
---|
294 | return delete(layer, selection, true, false);
|
---|
295 | }
|
---|
296 |
|
---|
297 | /**
|
---|
298 | * Replies the collection of nodes referred to by primitives in <code>primitivesToDelete</code> which
|
---|
299 | * can be deleted too. A node can be deleted if
|
---|
300 | * <ul>
|
---|
301 | * <li>it is untagged (see {@link Node#isTagged()}</li>
|
---|
302 | * <li>it is not referred to by other non-deleted primitives outside of <code>primitivesToDelete</code></li>
|
---|
303 | * </ul>
|
---|
304 | * @param layer the layer in whose context primitives are deleted
|
---|
305 | * @param primitivesToDelete the primitives to delete
|
---|
306 | * @return the collection of nodes referred to by primitives in <code>primitivesToDelete</code> which
|
---|
307 | * can be deleted too
|
---|
308 | */
|
---|
309 | protected static Collection<Node> computeNodesToDelete(OsmDataLayer layer, Collection<OsmPrimitive> primitivesToDelete) {
|
---|
310 | Collection<Node> nodesToDelete = new HashSet<>();
|
---|
311 | for (Way way : OsmPrimitive.getFilteredList(primitivesToDelete, Way.class)) {
|
---|
312 | for (Node n : way.getNodes()) {
|
---|
313 | if (n.isTagged()) {
|
---|
314 | continue;
|
---|
315 | }
|
---|
316 | Collection<OsmPrimitive> referringPrimitives = n.getReferrers();
|
---|
317 | referringPrimitives.removeAll(primitivesToDelete);
|
---|
318 | int count = 0;
|
---|
319 | for (OsmPrimitive p : referringPrimitives) {
|
---|
320 | if (!p.isDeleted()) {
|
---|
321 | count++;
|
---|
322 | }
|
---|
323 | }
|
---|
324 | if (count == 0) {
|
---|
325 | nodesToDelete.add(n);
|
---|
326 | }
|
---|
327 | }
|
---|
328 | }
|
---|
329 | return nodesToDelete;
|
---|
330 | }
|
---|
331 |
|
---|
332 | /**
|
---|
333 | * Try to delete all given primitives.
|
---|
334 | *
|
---|
335 | * If a node is used by a way, it's removed from that way. If a node or a way is used by a
|
---|
336 | * relation, inform the user and do not delete.
|
---|
337 | *
|
---|
338 | * If this would cause ways with less than 2 nodes to be created, delete these ways instead. If
|
---|
339 | * they are part of a relation, inform the user and do not delete.
|
---|
340 | *
|
---|
341 | * @param layer the {@link OsmDataLayer} in whose context the primitives are deleted
|
---|
342 | * @param selection the objects to delete.
|
---|
343 | * @param alsoDeleteNodesInWay <code>true</code> if nodes should be deleted as well
|
---|
344 | * @return command a command to perform the deletions, or null if there is nothing to delete.
|
---|
345 | */
|
---|
346 | public static Command delete(OsmDataLayer layer, Collection<? extends OsmPrimitive> selection,
|
---|
347 | boolean alsoDeleteNodesInWay) {
|
---|
348 | return delete(layer, selection, alsoDeleteNodesInWay, false /* not silent */);
|
---|
349 | }
|
---|
350 |
|
---|
351 | /**
|
---|
352 | * Try to delete all given primitives.
|
---|
353 | *
|
---|
354 | * If a node is used by a way, it's removed from that way. If a node or a way is used by a
|
---|
355 | * relation, inform the user and do not delete.
|
---|
356 | *
|
---|
357 | * If this would cause ways with less than 2 nodes to be created, delete these ways instead. If
|
---|
358 | * they are part of a relation, inform the user and do not delete.
|
---|
359 | *
|
---|
360 | * @param layer the {@link OsmDataLayer} in whose context the primitives are deleted
|
---|
361 | * @param selection the objects to delete.
|
---|
362 | * @param alsoDeleteNodesInWay <code>true</code> if nodes should be deleted as well
|
---|
363 | * @param silent set to true if the user should not be bugged with additional questions
|
---|
364 | * @return command a command to perform the deletions, or null if there is nothing to delete.
|
---|
365 | */
|
---|
366 | public static Command delete(OsmDataLayer layer, Collection<? extends OsmPrimitive> selection,
|
---|
367 | boolean alsoDeleteNodesInWay, boolean silent) {
|
---|
368 | if (selection == null || selection.isEmpty())
|
---|
369 | return null;
|
---|
370 |
|
---|
371 | Set<OsmPrimitive> primitivesToDelete = new HashSet<OsmPrimitive>(selection);
|
---|
372 |
|
---|
373 | Collection<Relation> relationsToDelete = Utils.filteredCollection(primitivesToDelete, Relation.class);
|
---|
374 | if (!relationsToDelete.isEmpty() && !silent && !confirmRelationDeletion(relationsToDelete))
|
---|
375 | return null;
|
---|
376 |
|
---|
377 | Collection<Way> waysToBeChanged = new HashSet<>();
|
---|
378 |
|
---|
379 | if (alsoDeleteNodesInWay) {
|
---|
380 | // delete untagged nodes only referenced by primitives in primitivesToDelete, too
|
---|
381 | Collection<Node> nodesToDelete = computeNodesToDelete(layer, primitivesToDelete);
|
---|
382 | primitivesToDelete.addAll(nodesToDelete);
|
---|
383 | }
|
---|
384 |
|
---|
385 | if (!silent && !checkAndConfirmOutlyingDelete(
|
---|
386 | primitivesToDelete, Utils.filteredCollection(primitivesToDelete, Way.class)))
|
---|
387 | return null;
|
---|
388 |
|
---|
389 | waysToBeChanged.addAll(OsmPrimitive.getFilteredSet(OsmPrimitive.getReferrer(primitivesToDelete), Way.class));
|
---|
390 |
|
---|
391 | Collection<Command> cmds = new LinkedList<>();
|
---|
392 | for (Way w : waysToBeChanged) {
|
---|
393 | Way wnew = new Way(w);
|
---|
394 | wnew.removeNodes(OsmPrimitive.getFilteredSet(primitivesToDelete, Node.class));
|
---|
395 | if (wnew.getNodesCount() < 2) {
|
---|
396 | primitivesToDelete.add(w);
|
---|
397 | } else {
|
---|
398 | cmds.add(new ChangeNodesCommand(w, wnew.getNodes()));
|
---|
399 | }
|
---|
400 | }
|
---|
401 |
|
---|
402 | // get a confirmation that the objects to delete can be removed from their parent relations
|
---|
403 | //
|
---|
404 | if (!silent) {
|
---|
405 | Set<RelationToChildReference> references = RelationToChildReference.getRelationToChildReferences(primitivesToDelete);
|
---|
406 | Iterator<RelationToChildReference> it = references.iterator();
|
---|
407 | while (it.hasNext()) {
|
---|
408 | RelationToChildReference ref = it.next();
|
---|
409 | if (ref.getParent().isDeleted()) {
|
---|
410 | it.remove();
|
---|
411 | }
|
---|
412 | }
|
---|
413 | if (!references.isEmpty()) {
|
---|
414 | DeleteFromRelationConfirmationDialog dialog = DeleteFromRelationConfirmationDialog.getInstance();
|
---|
415 | dialog.getModel().populate(references);
|
---|
416 | dialog.setVisible(true);
|
---|
417 | if (dialog.isCanceled())
|
---|
418 | return null;
|
---|
419 | }
|
---|
420 | }
|
---|
421 |
|
---|
422 | // remove the objects from their parent relations
|
---|
423 | //
|
---|
424 | for (Relation cur : OsmPrimitive.getFilteredSet(OsmPrimitive.getReferrer(primitivesToDelete), Relation.class)) {
|
---|
425 | Relation rel = new Relation(cur);
|
---|
426 | rel.removeMembersFor(primitivesToDelete);
|
---|
427 | cmds.add(new ChangeCommand(cur, rel));
|
---|
428 | }
|
---|
429 |
|
---|
430 | // build the delete command
|
---|
431 | //
|
---|
432 | if (!primitivesToDelete.isEmpty()) {
|
---|
433 | cmds.add(new DeleteCommand(layer, primitivesToDelete));
|
---|
434 | }
|
---|
435 |
|
---|
436 | return new SequenceCommand(tr("Delete"), cmds);
|
---|
437 | }
|
---|
438 |
|
---|
439 | public static Command deleteWaySegment(OsmDataLayer layer, WaySegment ws) {
|
---|
440 | if (ws.way.getNodesCount() < 3)
|
---|
441 | return delete(layer, Collections.singleton(ws.way), false);
|
---|
442 |
|
---|
443 | if (ws.way.isClosed()) {
|
---|
444 | // If the way is circular (first and last nodes are the same), the way shouldn't be splitted
|
---|
445 |
|
---|
446 | List<Node> n = new ArrayList<>();
|
---|
447 |
|
---|
448 | n.addAll(ws.way.getNodes().subList(ws.lowerIndex + 1, ws.way.getNodesCount() - 1));
|
---|
449 | n.addAll(ws.way.getNodes().subList(0, ws.lowerIndex + 1));
|
---|
450 |
|
---|
451 | Way wnew = new Way(ws.way);
|
---|
452 | wnew.setNodes(n);
|
---|
453 |
|
---|
454 | return new ChangeCommand(ws.way, wnew);
|
---|
455 | }
|
---|
456 |
|
---|
457 | List<Node> n1 = new ArrayList<>();
|
---|
458 | List<Node> n2 = new ArrayList<>();
|
---|
459 |
|
---|
460 | n1.addAll(ws.way.getNodes().subList(0, ws.lowerIndex + 1));
|
---|
461 | n2.addAll(ws.way.getNodes().subList(ws.lowerIndex + 1, ws.way.getNodesCount()));
|
---|
462 |
|
---|
463 | Way wnew = new Way(ws.way);
|
---|
464 |
|
---|
465 | if (n1.size() < 2) {
|
---|
466 | wnew.setNodes(n2);
|
---|
467 | return new ChangeCommand(ws.way, wnew);
|
---|
468 | } else if (n2.size() < 2) {
|
---|
469 | wnew.setNodes(n1);
|
---|
470 | return new ChangeCommand(ws.way, wnew);
|
---|
471 | } else {
|
---|
472 | List<List<Node>> chunks = new ArrayList<>(2);
|
---|
473 | chunks.add(n1);
|
---|
474 | chunks.add(n2);
|
---|
475 | return SplitWayAction.splitWay(layer, ws.way, chunks, Collections.<OsmPrimitive>emptyList()).getCommand();
|
---|
476 | }
|
---|
477 | }
|
---|
478 |
|
---|
479 | public static boolean checkAndConfirmOutlyingDelete(Collection<? extends OsmPrimitive> primitives,
|
---|
480 | Collection<? extends OsmPrimitive> ignore) {
|
---|
481 | return Command.checkAndConfirmOutlyingOperation("delete",
|
---|
482 | tr("Delete confirmation"),
|
---|
483 | tr("You are about to delete nodes outside of the area you have downloaded."
|
---|
484 | + "<br>"
|
---|
485 | + "This can cause problems because other objects (that you do not see) might use them."
|
---|
486 | + "<br>"
|
---|
487 | + "Do you really want to delete?"),
|
---|
488 | tr("You are about to delete incomplete objects."
|
---|
489 | + "<br>"
|
---|
490 | + "This will cause problems because you don''t see the real object."
|
---|
491 | + "<br>" + "Do you really want to delete?"),
|
---|
492 | primitives, ignore);
|
---|
493 | }
|
---|
494 |
|
---|
495 | private static boolean confirmRelationDeletion(Collection<Relation> relations) {
|
---|
496 | JPanel msg = new JPanel(new GridBagLayout());
|
---|
497 | msg.add(new JMultilineLabel("<html>" + trn(
|
---|
498 | "You are about to delete {0} relation: {1}"
|
---|
499 | + "<br/>"
|
---|
500 | + "This step is rarely necessary and cannot be undone easily after being uploaded to the server."
|
---|
501 | + "<br/>"
|
---|
502 | + "Do you really want to delete?",
|
---|
503 | "You are about to delete {0} relations: {1}"
|
---|
504 | + "<br/>"
|
---|
505 | + "This step is rarely necessary and cannot be undone easily after being uploaded to the server."
|
---|
506 | + "<br/>"
|
---|
507 | + "Do you really want to delete?",
|
---|
508 | relations.size(), relations.size(), DefaultNameFormatter.getInstance().formatAsHtmlUnorderedList(relations))
|
---|
509 | + "</html>"));
|
---|
510 | return ConditionalOptionPaneUtil.showConfirmationDialog(
|
---|
511 | "delete_relations",
|
---|
512 | Main.parent,
|
---|
513 | msg,
|
---|
514 | tr("Delete relation?"),
|
---|
515 | JOptionPane.YES_NO_OPTION,
|
---|
516 | JOptionPane.QUESTION_MESSAGE,
|
---|
517 | JOptionPane.YES_OPTION);
|
---|
518 | }
|
---|
519 |
|
---|
520 | @Override
|
---|
521 | public int hashCode() {
|
---|
522 | final int prime = 31;
|
---|
523 | int result = super.hashCode();
|
---|
524 | result = prime * result + ((clonedPrimitives == null) ? 0 : clonedPrimitives.hashCode());
|
---|
525 | result = prime * result + ((toDelete == null) ? 0 : toDelete.hashCode());
|
---|
526 | return result;
|
---|
527 | }
|
---|
528 |
|
---|
529 | @Override
|
---|
530 | public boolean equals(Object obj) {
|
---|
531 | if (this == obj)
|
---|
532 | return true;
|
---|
533 | if (!super.equals(obj))
|
---|
534 | return false;
|
---|
535 | if (getClass() != obj.getClass())
|
---|
536 | return false;
|
---|
537 | DeleteCommand other = (DeleteCommand) obj;
|
---|
538 | if (clonedPrimitives == null) {
|
---|
539 | if (other.clonedPrimitives != null)
|
---|
540 | return false;
|
---|
541 | } else if (!clonedPrimitives.equals(other.clonedPrimitives))
|
---|
542 | return false;
|
---|
543 | if (toDelete == null) {
|
---|
544 | if (other.toDelete != null)
|
---|
545 | return false;
|
---|
546 | } else if (!toDelete.equals(other.toDelete))
|
---|
547 | return false;
|
---|
548 | return true;
|
---|
549 | }
|
---|
550 | }
|
---|