source: josm/trunk/src/org/openstreetmap/josm/command/DeleteCommand.java@ 18801

Last change on this file since 18801 was 18801, checked in by taylor.smock, 9 months ago

Fix #22832: Code cleanup and some simplification, documentation fixes (patch by gaben)

There should not be any functional changes in this patch; it is intended to do
the following:

  • Simplify and cleanup code (example: Arrays.asList(item) -> Collections.singletonList(item))
  • Fix typos in documentation (which also corrects the documentation to match what actually happens, in some cases)
  • Property svn:eol-style set to native
File size: 22.4 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.command;
3
4import static org.openstreetmap.josm.tools.I18n.marktr;
5import static org.openstreetmap.josm.tools.I18n.tr;
6import static org.openstreetmap.josm.tools.I18n.trn;
7
8import java.util.ArrayList;
9import java.util.Arrays;
10import java.util.Collection;
11import java.util.Collections;
12import java.util.HashMap;
13import java.util.HashSet;
14import java.util.LinkedList;
15import java.util.List;
16import java.util.Map;
17import java.util.Map.Entry;
18import java.util.Objects;
19import java.util.Set;
20import java.util.stream.Collectors;
21
22import javax.swing.Icon;
23
24import org.openstreetmap.josm.data.osm.DataSet;
25import org.openstreetmap.josm.data.osm.DefaultNameFormatter;
26import org.openstreetmap.josm.data.osm.IPrimitive;
27import org.openstreetmap.josm.data.osm.Node;
28import org.openstreetmap.josm.data.osm.OsmPrimitive;
29import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
30import org.openstreetmap.josm.data.osm.PrimitiveData;
31import org.openstreetmap.josm.data.osm.Relation;
32import org.openstreetmap.josm.data.osm.RelationMember;
33import org.openstreetmap.josm.data.osm.RelationToChildReference;
34import org.openstreetmap.josm.data.osm.Way;
35import org.openstreetmap.josm.data.osm.WaySegment;
36import org.openstreetmap.josm.tools.CheckParameterUtil;
37import org.openstreetmap.josm.tools.ImageProvider;
38import org.openstreetmap.josm.tools.Pair;
39import 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 */
47public 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 * Confirm before removing a collection of primitives from their parent relations, with the probability of
108 * deleting the parents as well.
109 * @param references the list of relation-to-child references
110 * @param parentsToDelete the list of parents to delete (the boolean part will be {@code true} if the user wants
111 * to delete the relation).
112 * @return {@code true} if the user confirms the deletion
113 * @since 18395
114 */
115 default boolean confirmDeletionFromRelation(Collection<RelationToChildReference> references,
116 Collection<Pair<Relation, Boolean>> parentsToDelete) {
117 // This is a default method. Ensure that all the booleans are false.
118 parentsToDelete.forEach(pair -> pair.b = false);
119 return confirmDeletionFromRelation(references);
120 }
121 }
122
123 private static volatile DeletionCallback callback;
124
125 /**
126 * Sets the global {@link DeletionCallback}.
127 * @param deletionCallback the new {@code DeletionCallback}. Must not be null
128 * @throws NullPointerException if {@code deletionCallback} is null
129 * @since 12749
130 */
131 public static void setDeletionCallback(DeletionCallback deletionCallback) {
132 callback = Objects.requireNonNull(deletionCallback);
133 }
134
135 /**
136 * The primitives that get deleted.
137 */
138 private final Collection<? extends OsmPrimitive> toDelete;
139 private final Map<OsmPrimitive, PrimitiveData> clonedPrimitives = new HashMap<>();
140
141 /**
142 * Constructor. Deletes a collection of primitives in the current edit layer.
143 *
144 * @param data the primitives to delete. Must neither be null nor empty, and belong to a data set
145 * @throws IllegalArgumentException if data is null or empty
146 */
147 public DeleteCommand(Collection<? extends OsmPrimitive> data) {
148 this(data.iterator().next().getDataSet(), data);
149 }
150
151 /**
152 * Constructor. Deletes a single primitive in the current edit layer.
153 *
154 * @param data the primitive to delete. Must not be null.
155 * @throws IllegalArgumentException if data is null
156 */
157 public DeleteCommand(OsmPrimitive data) {
158 this(Collections.singleton(data));
159 }
160
161 /**
162 * Constructor for a single data item. Use the collection constructor to delete multiple objects.
163 *
164 * @param dataset the data set context for deleting this primitive. Must not be null.
165 * @param data the primitive to delete. Must not be null.
166 * @throws IllegalArgumentException if data is null
167 * @throws IllegalArgumentException if layer is null
168 * @since 12718
169 */
170 public DeleteCommand(DataSet dataset, OsmPrimitive data) {
171 this(dataset, Collections.singleton(data));
172 }
173
174 /**
175 * Constructor for a collection of data to be deleted in the context of a specific data set
176 *
177 * @param dataset the dataset context for deleting these primitives. Must not be null.
178 * @param data the primitives to delete. Must neither be null nor empty.
179 * @throws IllegalArgumentException if dataset is null
180 * @throws IllegalArgumentException if data is null or empty
181 * @since 11240
182 */
183 public DeleteCommand(DataSet dataset, Collection<? extends OsmPrimitive> data) {
184 super(dataset);
185 CheckParameterUtil.ensureParameterNotNull(data, "data");
186 this.toDelete = data;
187 checkConsistency();
188 }
189
190 private void checkConsistency() {
191 if (toDelete.isEmpty()) {
192 throw new IllegalArgumentException(tr("At least one object to delete required, got empty collection"));
193 }
194 for (OsmPrimitive p : toDelete) {
195 if (p == null) {
196 throw new IllegalArgumentException("Primitive to delete must not be null");
197 } else if (p.getDataSet() == null) {
198 throw new IllegalArgumentException("Primitive to delete must be in a dataset");
199 }
200 }
201 }
202
203 @Override
204 public boolean executeCommand() {
205 ensurePrimitivesAreInDataset();
206
207 getAffectedDataSet().update(() -> {
208 // Make copy and remove all references (to prevent inconsistent dataset (delete referenced) while command is executed)
209 for (OsmPrimitive osm : toDelete) {
210 if (osm.isDeleted())
211 throw new IllegalArgumentException(osm + " is already deleted");
212 clonedPrimitives.put(osm, osm.save());
213 IPrimitive.resetPrimitiveChildren(osm);
214 }
215
216 for (OsmPrimitive osm : toDelete) {
217 osm.setDeleted(true);
218 }
219 });
220 return true;
221 }
222
223 @Override
224 public void undoCommand() {
225 ensurePrimitivesAreInDataset();
226
227 getAffectedDataSet().update(() -> {
228 for (OsmPrimitive osm : toDelete) {
229 osm.setDeleted(false);
230 }
231
232 for (Entry<OsmPrimitive, PrimitiveData> entry : clonedPrimitives.entrySet()) {
233 entry.getKey().load(entry.getValue());
234 }
235 });
236 }
237
238 @Override
239 public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
240 // Do nothing
241 }
242
243 private Set<OsmPrimitiveType> getTypesToDelete() {
244 return toDelete.stream().map(OsmPrimitiveType::from).collect(Collectors.toSet());
245 }
246
247 @Override
248 public String getDescriptionText() {
249 if (toDelete.size() == 1) {
250 OsmPrimitive primitive = toDelete.iterator().next();
251 String msg;
252 switch(OsmPrimitiveType.from(primitive)) {
253 case NODE: msg = marktr("Delete node {0}"); break;
254 case WAY: msg = marktr("Delete way {0}"); break;
255 case RELATION:msg = marktr("Delete relation {0}"); break;
256 default: throw new AssertionError();
257 }
258
259 return tr(msg, primitive.getDisplayName(DefaultNameFormatter.getInstance()));
260 } else {
261 Set<OsmPrimitiveType> typesToDelete = getTypesToDelete();
262 String msg;
263 if (typesToDelete.size() > 1) {
264 msg = trn("Delete {0} object", "Delete {0} objects", toDelete.size(), toDelete.size());
265 } else {
266 OsmPrimitiveType t = typesToDelete.iterator().next();
267 switch(t) {
268 case NODE: msg = trn("Delete {0} node", "Delete {0} nodes", toDelete.size(), toDelete.size()); break;
269 case WAY: msg = trn("Delete {0} way", "Delete {0} ways", toDelete.size(), toDelete.size()); break;
270 case RELATION: msg = trn("Delete {0} relation", "Delete {0} relations", toDelete.size(), toDelete.size()); break;
271 default: throw new AssertionError();
272 }
273 }
274 return msg;
275 }
276 }
277
278 @Override
279 public Icon getDescriptionIcon() {
280 if (toDelete.size() == 1)
281 return ImageProvider.get(toDelete.iterator().next().getDisplayType());
282 Set<OsmPrimitiveType> typesToDelete = getTypesToDelete();
283 if (typesToDelete.size() > 1)
284 return ImageProvider.get("data", "object");
285 else
286 return ImageProvider.get(typesToDelete.iterator().next());
287 }
288
289 @Override public Collection<PseudoCommand> getChildren() {
290 if (toDelete.size() == 1)
291 return null;
292 else {
293 return toDelete.stream().map(DeleteChildCommand::new).collect(Collectors.toList());
294 }
295 }
296
297 @Override public Collection<? extends OsmPrimitive> getParticipatingPrimitives() {
298 return toDelete;
299 }
300
301 /**
302 * Delete the primitives and everything they reference.
303 *
304 * If a node is deleted, the node and all ways and relations the node is part of are deleted as well.
305 * If a way is deleted, all relations the way is member of are also deleted.
306 * If a way is deleted, only the way and no nodes are deleted.
307 *
308 * @param selection The list of all object to be deleted.
309 * @param silent Set to true if the user should not be bugged with additional dialogs
310 * @return command A command to perform the deletions, or null of there is nothing to delete.
311 * @throws IllegalArgumentException if layer is null
312 * @since 12718
313 */
314 public static Command deleteWithReferences(Collection<? extends OsmPrimitive> selection, boolean silent) {
315 if (Utils.isEmpty(selection)) return null;
316 Set<OsmPrimitive> parents = OsmPrimitive.getReferrer(selection);
317 parents.addAll(selection);
318
319 if (parents.isEmpty())
320 return null;
321 if (!silent && !callback.checkAndConfirmOutlyingDelete(parents, null))
322 return null;
323 return new DeleteCommand(parents);
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 selection The list of all object to be deleted.
334 * @return command A command to perform the deletions, or null of there is nothing to delete.
335 * @throws IllegalArgumentException if layer is null
336 * @since 12718
337 */
338 public static Command deleteWithReferences(Collection<? extends OsmPrimitive> selection) {
339 return deleteWithReferences(selection, false);
340 }
341
342 /**
343 * Try to delete all given primitives.
344 *
345 * If a node is used by a way, it's removed from that way. If a node or a way is used by a
346 * relation, inform the user and do not delete.
347 *
348 * If this would cause ways with less than 2 nodes to be created, delete these ways instead. If
349 * they are part of a relation, inform the user and do not delete.
350 *
351 * @param selection the objects to delete.
352 * @return command a command to perform the deletions, or null if there is nothing to delete.
353 * @since 12718
354 */
355 public static Command delete(Collection<? extends OsmPrimitive> selection) {
356 return delete(selection, true, false);
357 }
358
359 /**
360 * Replies the collection of nodes referred to by primitives in <code>primitivesToDelete</code> which
361 * can be deleted too. A node can be deleted if
362 * <ul>
363 * <li>it is untagged (see {@link Node#isTagged()}</li>
364 * <li>it is not referred to by other non-deleted primitives outside of <code>primitivesToDelete</code></li>
365 * </ul>
366 * @param primitivesToDelete the primitives to delete
367 * @return the collection of nodes referred to by primitives in <code>primitivesToDelete</code> which
368 * can be deleted too
369 */
370 protected static Collection<Node> computeNodesToDelete(Collection<OsmPrimitive> primitivesToDelete) {
371 Collection<Node> nodesToDelete = new HashSet<>();
372 for (Way way : Utils.filteredCollection(primitivesToDelete, Way.class)) {
373 for (Node n : way.getNodes()) {
374 if (n.isTagged()) {
375 continue;
376 }
377 Collection<OsmPrimitive> referringPrimitives = n.getReferrers();
378 referringPrimitives.removeAll(primitivesToDelete);
379 if (referringPrimitives.stream().allMatch(OsmPrimitive::isDeleted)) {
380 nodesToDelete.add(n);
381 }
382 }
383 }
384 return nodesToDelete;
385 }
386
387 /**
388 * Try to delete all given primitives.
389 *
390 * If a node is used by a way, it's removed from that way. If a node or a way is used by a
391 * relation, inform the user and do not delete.
392 *
393 * If this would cause ways with less than 2 nodes to be created, delete these ways instead. If
394 * they are part of a relation, inform the user and do not delete.
395 *
396 * @param selection the objects to delete.
397 * @param alsoDeleteNodesInWay <code>true</code> if nodes should be deleted as well
398 * @return command a command to perform the deletions, or null if there is nothing to delete.
399 * @since 12718
400 */
401 public static Command delete(Collection<? extends OsmPrimitive> selection, boolean alsoDeleteNodesInWay) {
402 return delete(selection, alsoDeleteNodesInWay, false /* not silent */);
403 }
404
405 /**
406 * Try to delete all given primitives.
407 *
408 * If a node is used by a way, it's removed from that way. If a node or a way is used by a
409 * relation, inform the user and do not delete.
410 *
411 * If this would cause ways with less than 2 nodes to be created, delete these ways instead. If
412 * they are part of a relation, inform the user and do not delete.
413 *
414 * @param selection the objects to delete.
415 * @param alsoDeleteNodesInWay <code>true</code> if nodes should be deleted as well
416 * @param silent set to true if the user should not be bugged with additional questions
417 * @return command a command to perform the deletions, or null if there is nothing to delete.
418 * @since 12718
419 */
420 public static Command delete(Collection<? extends OsmPrimitive> selection, boolean alsoDeleteNodesInWay, boolean silent) {
421 if (Utils.isEmpty(selection))
422 return null;
423
424 Set<OsmPrimitive> primitivesToDelete = new HashSet<>(selection);
425
426 Collection<Relation> relationsToDelete = Utils.filteredCollection(primitivesToDelete, Relation.class);
427 if (!relationsToDelete.isEmpty() && !silent && !callback.confirmRelationDeletion(relationsToDelete))
428 return null;
429
430 if (alsoDeleteNodesInWay) {
431 // delete untagged nodes only referenced by primitives in primitivesToDelete, too
432 Collection<Node> nodesToDelete = computeNodesToDelete(primitivesToDelete);
433 primitivesToDelete.addAll(nodesToDelete);
434 }
435
436 if (!silent && !callback.checkAndConfirmOutlyingDelete(
437 primitivesToDelete, Utils.filteredCollection(primitivesToDelete, Way.class)))
438 return null;
439
440 Collection<Way> waysToBeChanged = primitivesToDelete.stream()
441 .flatMap(p -> p.referrers(Way.class))
442 .collect(Collectors.toSet());
443
444 Collection<Command> cmds = new LinkedList<>();
445 Set<Node> nodesToRemove = new HashSet<>(Utils.filteredCollection(primitivesToDelete, Node.class));
446 for (Way w : waysToBeChanged) {
447 if (primitivesToDelete.contains(w))
448 continue;
449 List<Node> remainingNodes = w.calculateRemoveNodes(nodesToRemove);
450 if (remainingNodes.size() < 2) {
451 primitivesToDelete.add(w);
452 } else {
453 cmds.add(new ChangeNodesCommand(w, remainingNodes));
454 }
455 }
456
457 // remove the objects from their parent relations
458 //
459 final Set<Relation> relationsToBeChanged = primitivesToDelete.stream()
460 .flatMap(p -> p.referrers(Relation.class))
461 .collect(Collectors.toSet());
462 final Set<Relation> additionalRelationsToDelete = new HashSet<>();
463 for (Relation cur : relationsToBeChanged) {
464 List<RelationMember> newMembers = cur.getMembers();
465 cur.getMembersFor(primitivesToDelete).forEach(newMembers::remove);
466 cmds.add(new ChangeMembersCommand(cur, newMembers));
467 // If we don't have any members, we probably ought to delete the relation as well.
468 if (newMembers.isEmpty()) {
469 additionalRelationsToDelete.add(cur);
470 }
471 }
472
473
474 // get a confirmation that the objects to delete can be removed from their parent relations
475 //
476 if (!silent) {
477 Set<RelationToChildReference> references = RelationToChildReference.getRelationToChildReferences(primitivesToDelete);
478 references.removeIf(ref -> ref.getParent().isDeleted());
479 final Collection<Pair<Relation, Boolean>> pairedRelations = additionalRelationsToDelete.stream()
480 /*
481 * Default to true, so that users have to make a choice to not delete.
482 * Default implementation converts it to false, so this should be safe.
483 */
484 .map(relation -> new Pair<>(relation, true)).collect(Collectors.toSet());
485 if (!references.isEmpty() && !callback.confirmDeletionFromRelation(references, pairedRelations)) {
486 return null;
487 }
488 pairedRelations.stream().filter(pair -> Boolean.TRUE.equals(pair.b)).map(pair -> pair.a)
489 .forEach(primitivesToDelete::add);
490 }
491
492 // build the delete command
493 //
494 if (!primitivesToDelete.isEmpty()) {
495 cmds.add(new DeleteCommand(primitivesToDelete));
496 }
497
498 return SequenceCommand.wrapIfNeeded(tr("Delete"), cmds);
499 }
500
501 /**
502 * Create a command that deletes a single way segment. The way may be split by this.
503 * @param ws The way segment that should be deleted
504 * @return A matching command to safely delete that segment.
505 * @since 12718
506 */
507 public static Command deleteWaySegment(WaySegment ws) {
508 if (ws.getWay().getNodesCount() < 3)
509 return delete(Collections.singleton(ws.getWay()), false);
510
511 if (ws.getWay().isClosed()) {
512 // If the way is circular (first and last nodes are the same), the way shouldn't be split
513
514 List<Node> n = new ArrayList<>();
515
516 n.addAll(ws.getWay().getNodes().subList(ws.getUpperIndex(), ws.getWay().getNodesCount() - 1));
517 n.addAll(ws.getWay().getNodes().subList(0, ws.getUpperIndex()));
518
519 return new ChangeNodesCommand(ws.getWay(), n);
520 }
521
522 List<Node> n1 = new ArrayList<>();
523 List<Node> n2 = new ArrayList<>();
524
525 n1.addAll(ws.getWay().getNodes().subList(0, ws.getUpperIndex()));
526 n2.addAll(ws.getWay().getNodes().subList(ws.getUpperIndex(), ws.getWay().getNodesCount()));
527
528 if (n1.size() < 2) {
529 return new ChangeNodesCommand(ws.getWay(), n2);
530 } else if (n2.size() < 2) {
531 return new ChangeNodesCommand(ws.getWay(), n1);
532 } else {
533 return SplitWayCommand.splitWay(ws.getWay(), Arrays.asList(n1, n2), Collections.<OsmPrimitive>emptyList());
534 }
535 }
536
537 @Override
538 public int hashCode() {
539 return Objects.hash(super.hashCode(), toDelete, clonedPrimitives);
540 }
541
542 @Override
543 public boolean equals(Object obj) {
544 if (this == obj) return true;
545 if (obj == null || getClass() != obj.getClass()) return false;
546 if (!super.equals(obj)) return false;
547 DeleteCommand that = (DeleteCommand) obj;
548 return Objects.equals(toDelete, that.toDelete) &&
549 Objects.equals(clonedPrimitives, that.clonedPrimitives);
550 }
551}
Note: See TracBrowser for help on using the repository browser.