[8378] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
[3142] | 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;
|
---|
[9371] | 8 | import java.util.Objects;
|
---|
[6881] | 9 |
|
---|
[4918] | 10 | import javax.swing.Icon;
|
---|
[3142] | 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;
|
---|
[6881] | 15 | import org.openstreetmap.josm.data.osm.Way;
|
---|
[3142] | 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 |
|
---|
[6881] | 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 | */
|
---|
[3142] | 37 | public ChangeNodesCommand(Way way, List<Node> newNodes) {
|
---|
| 38 | this.way = way;
|
---|
| 39 | this.newNodes = newNodes;
|
---|
[10663] | 40 | if (newNodes.isEmpty()) {
|
---|
| 41 | throw new IllegalArgumentException("Cannot set nodes to be an empty list.");
|
---|
| 42 | }
|
---|
[3142] | 43 | }
|
---|
| 44 |
|
---|
[6881] | 45 | @Override
|
---|
| 46 | public boolean executeCommand() {
|
---|
[3142] | 47 | super.executeCommand();
|
---|
| 48 | way.setNodes(newNodes);
|
---|
| 49 | way.setModified(true);
|
---|
| 50 | return true;
|
---|
| 51 | }
|
---|
| 52 |
|
---|
[6881] | 53 | @Override
|
---|
| 54 | public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
|
---|
[3142] | 55 | modified.add(way);
|
---|
| 56 | }
|
---|
| 57 |
|
---|
[4918] | 58 | @Override
|
---|
| 59 | public String getDescriptionText() {
|
---|
[10663] | 60 | return tr("Change nodes of {0}", way.getDisplayName(DefaultNameFormatter.getInstance()));
|
---|
[3142] | 61 | }
|
---|
[4918] | 62 |
|
---|
| 63 | @Override
|
---|
| 64 | public Icon getDescriptionIcon() {
|
---|
| 65 | return ImageProvider.get(OsmPrimitiveType.WAY);
|
---|
| 66 | }
|
---|
[8456] | 67 |
|
---|
| 68 | @Override
|
---|
| 69 | public int hashCode() {
|
---|
[9371] | 70 | return Objects.hash(super.hashCode(), way, newNodes);
|
---|
[8456] | 71 | }
|
---|
| 72 |
|
---|
| 73 | @Override
|
---|
| 74 | public boolean equals(Object obj) {
|
---|
[9371] | 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);
|
---|
[8456] | 81 | }
|
---|
[3142] | 82 | }
|
---|