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