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