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