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.HashSet;
008import java.util.List;
009import java.util.Objects;
010import java.util.Set;
011
012import javax.swing.Icon;
013
014import org.openstreetmap.josm.data.osm.DefaultNameFormatter;
015import org.openstreetmap.josm.data.osm.Node;
016import org.openstreetmap.josm.data.osm.OsmPrimitive;
017import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
018import org.openstreetmap.josm.data.osm.Way;
019import org.openstreetmap.josm.tools.ImageProvider;
020
021/**
022 * Command that removes a set of nodes from a way.
023 * The same can be done with ChangeNodesCommand, but this is more
024 * efficient. (Needed for the tool to disconnect nodes from ways.)
025 *
026 * @author Giuseppe Bilotta
027 */
028public class RemoveNodesCommand extends Command {
029
030    private final Way way;
031    private final Set<Node> rmNodes;
032
033    /**
034     * Constructs a new {@code RemoveNodesCommand}.
035     * @param way The way to modify. Must not be null, and belong to a data set
036     * @param rmNodes The list of nodes to remove
037     */
038    public RemoveNodesCommand(Way way, List<Node> rmNodes) {
039        super(way.getDataSet());
040        this.way = way;
041        this.rmNodes = new HashSet<>(rmNodes);
042    }
043
044    @Override
045    public boolean executeCommand() {
046        super.executeCommand();
047        way.removeNodes(rmNodes);
048        way.setModified(true);
049        return true;
050    }
051
052    @Override
053    public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
054        modified.add(way);
055    }
056
057    @Override
058    public String getDescriptionText() {
059        return tr("Removed nodes from {0}", way.getDisplayName(DefaultNameFormatter.getInstance()));
060    }
061
062    @Override
063    public Icon getDescriptionIcon() {
064        return ImageProvider.get(OsmPrimitiveType.WAY);
065    }
066
067    @Override
068    public int hashCode() {
069        return Objects.hash(super.hashCode(), way, rmNodes);
070    }
071
072    @Override
073    public boolean equals(Object obj) {
074        if (this == obj) return true;
075        if (obj == null || getClass() != obj.getClass()) return false;
076        if (!super.equals(obj)) return false;
077        RemoveNodesCommand that = (RemoveNodesCommand) obj;
078        return Objects.equals(way, that.way) &&
079                Objects.equals(rmNodes, that.rmNodes);
080    }
081}