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.Set;
|
---|
10 |
|
---|
11 | import javax.swing.Icon;
|
---|
12 |
|
---|
13 | import org.openstreetmap.josm.data.osm.Node;
|
---|
14 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
15 | import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
|
---|
16 | import org.openstreetmap.josm.data.osm.Way;
|
---|
17 | import org.openstreetmap.josm.gui.DefaultNameFormatter;
|
---|
18 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * Command that removes a set of nodes from a way.
|
---|
22 | * The same can be done with ChangeNodesCommand, but this is more
|
---|
23 | * efficient. (Needed for the tool to disconnect nodes from ways.)
|
---|
24 | *
|
---|
25 | * @author Giuseppe Bilotta
|
---|
26 | */
|
---|
27 | public class RemoveNodesCommand extends Command {
|
---|
28 |
|
---|
29 | private final Way way;
|
---|
30 | private final Set<Node> rmNodes;
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * Constructs a new {@code RemoveNodesCommand}.
|
---|
34 | * @param way The way to modify
|
---|
35 | * @param rmNodes The list of nodes to remove
|
---|
36 | */
|
---|
37 | public RemoveNodesCommand(Way way, List<Node> rmNodes) {
|
---|
38 | this.way = way;
|
---|
39 | this.rmNodes = new HashSet<>(rmNodes);
|
---|
40 | }
|
---|
41 |
|
---|
42 | @Override
|
---|
43 | public boolean executeCommand() {
|
---|
44 | super.executeCommand();
|
---|
45 | way.removeNodes(rmNodes);
|
---|
46 | way.setModified(true);
|
---|
47 | return true;
|
---|
48 | }
|
---|
49 |
|
---|
50 | @Override
|
---|
51 | public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
|
---|
52 | modified.add(way);
|
---|
53 | }
|
---|
54 |
|
---|
55 | @Override
|
---|
56 | public String getDescriptionText() {
|
---|
57 | return tr("Removed nodes from {0}", way.getDisplayName(DefaultNameFormatter.getInstance()));
|
---|
58 | }
|
---|
59 |
|
---|
60 | @Override
|
---|
61 | public Icon getDescriptionIcon() {
|
---|
62 | return ImageProvider.get(OsmPrimitiveType.WAY);
|
---|
63 | }
|
---|
64 |
|
---|
65 | @Override
|
---|
66 | public int hashCode() {
|
---|
67 | final int prime = 31;
|
---|
68 | int result = super.hashCode();
|
---|
69 | result = prime * result + ((rmNodes == null) ? 0 : rmNodes.hashCode());
|
---|
70 | result = prime * result + ((way == null) ? 0 : way.hashCode());
|
---|
71 | return result;
|
---|
72 | }
|
---|
73 |
|
---|
74 | @Override
|
---|
75 | public boolean equals(Object obj) {
|
---|
76 | if (this == obj)
|
---|
77 | return true;
|
---|
78 | if (!super.equals(obj))
|
---|
79 | return false;
|
---|
80 | if (getClass() != obj.getClass())
|
---|
81 | return false;
|
---|
82 | RemoveNodesCommand other = (RemoveNodesCommand) obj;
|
---|
83 | if (rmNodes == null) {
|
---|
84 | if (other.rmNodes != null)
|
---|
85 | return false;
|
---|
86 | } else if (!rmNodes.equals(other.rmNodes))
|
---|
87 | return false;
|
---|
88 | if (way == null) {
|
---|
89 | if (other.way != null)
|
---|
90 | return false;
|
---|
91 | } else if (!way.equals(other.way))
|
---|
92 | return false;
|
---|
93 | return true;
|
---|
94 | }
|
---|
95 | }
|
---|