1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.command;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.util.Collection;
|
---|
7 | import java.util.HashSet;
|
---|
8 | import java.util.List;
|
---|
9 | import java.util.Objects;
|
---|
10 | import java.util.Set;
|
---|
11 |
|
---|
12 | import javax.swing.Icon;
|
---|
13 |
|
---|
14 | import org.openstreetmap.josm.data.osm.Node;
|
---|
15 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
16 | import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
|
---|
17 | import org.openstreetmap.josm.data.osm.Way;
|
---|
18 | import org.openstreetmap.josm.gui.DefaultNameFormatter;
|
---|
19 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
20 |
|
---|
21 | /**
|
---|
22 | * Command that removes a set of nodes from a way.
|
---|
23 | * The same can be done with ChangeNodesCommand, but this is more
|
---|
24 | * efficient. (Needed for the tool to disconnect nodes from ways.)
|
---|
25 | *
|
---|
26 | * @author Giuseppe Bilotta
|
---|
27 | */
|
---|
28 | public class RemoveNodesCommand extends Command {
|
---|
29 |
|
---|
30 | private final Way way;
|
---|
31 | private final Set<Node> rmNodes;
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * Constructs a new {@code RemoveNodesCommand}.
|
---|
35 | * @param way The way to modify
|
---|
36 | * @param rmNodes The list of nodes to remove
|
---|
37 | */
|
---|
38 | public RemoveNodesCommand(Way way, List<Node> rmNodes) {
|
---|
39 | this.way = way;
|
---|
40 | this.rmNodes = new HashSet<>(rmNodes);
|
---|
41 | }
|
---|
42 |
|
---|
43 | @Override
|
---|
44 | public boolean executeCommand() {
|
---|
45 | super.executeCommand();
|
---|
46 | way.removeNodes(rmNodes);
|
---|
47 | way.setModified(true);
|
---|
48 | return true;
|
---|
49 | }
|
---|
50 |
|
---|
51 | @Override
|
---|
52 | public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
|
---|
53 | modified.add(way);
|
---|
54 | }
|
---|
55 |
|
---|
56 | @Override
|
---|
57 | public String getDescriptionText() {
|
---|
58 | return tr("Removed nodes from {0}", way.getDisplayName(DefaultNameFormatter.getInstance()));
|
---|
59 | }
|
---|
60 |
|
---|
61 | @Override
|
---|
62 | public Icon getDescriptionIcon() {
|
---|
63 | return ImageProvider.get(OsmPrimitiveType.WAY);
|
---|
64 | }
|
---|
65 |
|
---|
66 | @Override
|
---|
67 | public int hashCode() {
|
---|
68 | return Objects.hash(super.hashCode(), way, rmNodes);
|
---|
69 | }
|
---|
70 |
|
---|
71 | @Override
|
---|
72 | public boolean equals(Object obj) {
|
---|
73 | if (this == obj) return true;
|
---|
74 | if (obj == null || getClass() != obj.getClass()) return false;
|
---|
75 | if (!super.equals(obj)) return false;
|
---|
76 | RemoveNodesCommand that = (RemoveNodesCommand) obj;
|
---|
77 | return Objects.equals(way, that.way) &&
|
---|
78 | Objects.equals(rmNodes, that.rmNodes);
|
---|
79 | }
|
---|
80 | }
|
---|