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