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.Set;
010
011import javax.swing.Icon;
012
013import org.openstreetmap.josm.data.osm.Node;
014import org.openstreetmap.josm.data.osm.OsmPrimitive;
015import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
016import org.openstreetmap.josm.data.osm.Way;
017import org.openstreetmap.josm.gui.DefaultNameFormatter;
018import org.openstreetmap.josm.tools.ImageProvider;
019
020/**
021 * Command that removes a set of nodes from a way.
022 * The same can be done with ChangeNodesCommand, but this is more
023 * efficient. (Needed for the tool to disconnect nodes from ways.)
024 *
025 * @author Giuseppe Bilotta
026 */
027public class RemoveNodesCommand extends Command {
028
029    private final Way way;
030    private final Set<Node> rmNodes;
031
032    /**
033     * Constructs a new {@code RemoveNodesCommand}.
034     * @param way The way to modify
035     * @param rmNodes The list of nodes to remove
036     */
037    public RemoveNodesCommand(Way way, List<Node> rmNodes) {
038        this.way = way;
039        this.rmNodes = new HashSet<>(rmNodes);
040    }
041
042    @Override
043    public boolean executeCommand() {
044        super.executeCommand();
045        way.removeNodes(rmNodes);
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("Removed nodes from {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        final int prime = 31;
068        int result = super.hashCode();
069        result = prime * result + ((rmNodes == null) ? 0 : rmNodes.hashCode());
070        result = prime * result + ((way == null) ? 0 : way.hashCode());
071        return result;
072    }
073
074    @Override
075    public boolean equals(Object obj) {
076        if (this == obj)
077            return true;
078        if (!super.equals(obj))
079            return false;
080        if (getClass() != obj.getClass())
081            return false;
082        RemoveNodesCommand other = (RemoveNodesCommand) obj;
083        if (rmNodes == null) {
084            if (other.rmNodes != null)
085                return false;
086        } else if (!rmNodes.equals(other.rmNodes))
087            return false;
088        if (way == null) {
089            if (other.way != null)
090                return false;
091        } else if (!way.equals(other.way))
092            return false;
093        return true;
094    }
095}