001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.command;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005
006import java.util.Collection;
007import java.util.List;
008import java.util.Objects;
009
010import javax.swing.Icon;
011
012import org.openstreetmap.josm.data.osm.Node;
013import org.openstreetmap.josm.data.osm.OsmPrimitive;
014import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
015import org.openstreetmap.josm.data.osm.Way;
016import org.openstreetmap.josm.gui.DefaultNameFormatter;
017import org.openstreetmap.josm.tools.ImageProvider;
018
019/**
020 * Command that changes the nodes list of a way.
021 * The same can be done with ChangeCommand, but this is more
022 * efficient. (Needed for the duplicate node fixing
023 * tool of the validator plugin, when processing large data sets.)
024 *
025 * @author Imi
026 */
027public class ChangeNodesCommand extends Command {
028
029    private final Way way;
030    private final List<Node> newNodes;
031
032    /**
033     * Constructs a new {@code ChangeNodesCommand}.
034     * @param way The way to modify
035     * @param newNodes The new list of nodes for the given way
036     */
037    public ChangeNodesCommand(Way way, List<Node> newNodes) {
038        this.way = way;
039        this.newNodes = newNodes;
040    }
041
042    @Override
043    public boolean executeCommand() {
044        super.executeCommand();
045        way.setNodes(newNodes);
046        way.setModified(true);
047        return true;
048    }
049
050    @Override
051    public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
052        modified.add(way);
053    }
054
055    @Override
056    public String getDescriptionText() {
057        return tr("Changed nodes of {0}", way.getDisplayName(DefaultNameFormatter.getInstance()));
058    }
059
060    @Override
061    public Icon getDescriptionIcon() {
062        return ImageProvider.get(OsmPrimitiveType.WAY);
063    }
064
065    @Override
066    public int hashCode() {
067        return Objects.hash(super.hashCode(), way, newNodes);
068    }
069
070    @Override
071    public boolean equals(Object obj) {
072        if (this == obj) return true;
073        if (obj == null || getClass() != obj.getClass()) return false;
074        if (!super.equals(obj)) return false;
075        ChangeNodesCommand that = (ChangeNodesCommand) obj;
076        return Objects.equals(way, that.way) &&
077                Objects.equals(newNodes, that.newNodes);
078    }
079}