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.List;
|
---|
8 | import java.util.Objects;
|
---|
9 |
|
---|
10 | import javax.swing.Icon;
|
---|
11 |
|
---|
12 | import org.openstreetmap.josm.data.osm.Node;
|
---|
13 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
14 | import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
|
---|
15 | import org.openstreetmap.josm.data.osm.Way;
|
---|
16 | import org.openstreetmap.josm.gui.DefaultNameFormatter;
|
---|
17 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
18 |
|
---|
19 | /**
|
---|
20 | * Command that changes the nodes list of a way.
|
---|
21 | * The same can be done with ChangeCommand, but this is more
|
---|
22 | * efficient. (Needed for the duplicate node fixing
|
---|
23 | * tool of the validator plugin, when processing large data sets.)
|
---|
24 | *
|
---|
25 | * @author Imi
|
---|
26 | */
|
---|
27 | public class ChangeNodesCommand extends Command {
|
---|
28 |
|
---|
29 | private final Way way;
|
---|
30 | private final List<Node> newNodes;
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * Constructs a new {@code ChangeNodesCommand}.
|
---|
34 | * @param way The way to modify
|
---|
35 | * @param newNodes The new list of nodes for the given way
|
---|
36 | */
|
---|
37 | public ChangeNodesCommand(Way way, List<Node> newNodes) {
|
---|
38 | this.way = way;
|
---|
39 | this.newNodes = newNodes;
|
---|
40 | if (newNodes.isEmpty()) {
|
---|
41 | throw new IllegalArgumentException("Cannot set nodes to be an empty list.");
|
---|
42 | }
|
---|
43 | }
|
---|
44 |
|
---|
45 | @Override
|
---|
46 | public boolean executeCommand() {
|
---|
47 | super.executeCommand();
|
---|
48 | way.setNodes(newNodes);
|
---|
49 | way.setModified(true);
|
---|
50 | return true;
|
---|
51 | }
|
---|
52 |
|
---|
53 | @Override
|
---|
54 | public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
|
---|
55 | modified.add(way);
|
---|
56 | }
|
---|
57 |
|
---|
58 | @Override
|
---|
59 | public String getDescriptionText() {
|
---|
60 | return tr("Change nodes of {0}", way.getDisplayName(DefaultNameFormatter.getInstance()));
|
---|
61 | }
|
---|
62 |
|
---|
63 | @Override
|
---|
64 | public Icon getDescriptionIcon() {
|
---|
65 | return ImageProvider.get(OsmPrimitiveType.WAY);
|
---|
66 | }
|
---|
67 |
|
---|
68 | @Override
|
---|
69 | public int hashCode() {
|
---|
70 | return Objects.hash(super.hashCode(), way, newNodes);
|
---|
71 | }
|
---|
72 |
|
---|
73 | @Override
|
---|
74 | public boolean equals(Object obj) {
|
---|
75 | if (this == obj) return true;
|
---|
76 | if (obj == null || getClass() != obj.getClass()) return false;
|
---|
77 | if (!super.equals(obj)) return false;
|
---|
78 | ChangeNodesCommand that = (ChangeNodesCommand) obj;
|
---|
79 | return Objects.equals(way, that.way) &&
|
---|
80 | Objects.equals(newNodes, that.newNodes);
|
---|
81 | }
|
---|
82 | }
|
---|