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