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.Node;
015import org.openstreetmap.josm.data.osm.OsmPrimitive;
016import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
017import org.openstreetmap.josm.data.osm.Way;
018import org.openstreetmap.josm.gui.DefaultNameFormatter;
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
036     * @param rmNodes The list of nodes to remove
037     */
038    public RemoveNodesCommand(Way way, List<Node> rmNodes) {
039        this.way = way;
040        this.rmNodes = new HashSet<>(rmNodes);
041    }
042
043    @Override
044    public boolean executeCommand() {
045        super.executeCommand();
046        way.removeNodes(rmNodes);
047        way.setModified(true);
048        return true;
049    }
050
051    @Override
052    public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
053        modified.add(way);
054    }
055
056    @Override
057    public String getDescriptionText() {
058        return tr("Removed nodes from {0}", way.getDisplayName(DefaultNameFormatter.getInstance()));
059    }
060
061    @Override
062    public Icon getDescriptionIcon() {
063        return ImageProvider.get(OsmPrimitiveType.WAY);
064    }
065
066    @Override
067    public int hashCode() {
068        return Objects.hash(super.hashCode(), way, rmNodes);
069    }
070
071    @Override
072    public boolean equals(Object obj) {
073        if (this == obj) return true;
074        if (obj == null || getClass() != obj.getClass()) return false;
075        if (!super.equals(obj)) return false;
076        RemoveNodesCommand that = (RemoveNodesCommand) obj;
077        return Objects.equals(way, that.way) &&
078                Objects.equals(rmNodes, that.rmNodes);
079    }
080}