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

Last change on this file since 12718 was 12718, checked in by Don-vip, 3 weeks ago

see #13036 - see #15229 - see #15182 - make Commands depends only on a DataSet, not a Layer. This removes a lot of GUI dependencies

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