001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.command;
003
004import static org.openstreetmap.josm.tools.I18n.trn;
005
006import java.util.Collection;
007import java.util.Objects;
008
009import org.openstreetmap.josm.Main;
010import org.openstreetmap.josm.data.osm.OsmPrimitive;
011
012/**
013 * Command that selects OSM primitives
014 *
015 * @author Landwirt
016 */
017public class SelectCommand extends Command {
018
019    /** the primitives to select when executing the command */
020    private final Collection<OsmPrimitive> newSelection;
021
022    /** the selection before applying the new selection */
023    private Collection<OsmPrimitive> oldSelection;
024
025    /**
026     * Constructs a new select command.
027     * @param newSelection the primitives to select when executing the command.
028     */
029    public SelectCommand(Collection<OsmPrimitive> newSelection) {
030        this.newSelection = newSelection;
031    }
032
033    @Override
034    public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
035    }
036
037    @Override
038    public void undoCommand() {
039        Main.map.mapView.getEditLayer().data.setSelected(oldSelection);
040    }
041
042    @Override
043    public boolean executeCommand() {
044        oldSelection = Main.map.mapView.getEditLayer().data.getSelected();
045        Main.map.mapView.getEditLayer().data.setSelected(newSelection);
046        return true;
047    }
048
049    @Override
050    public String getDescriptionText() {
051        int size = newSelection != null ? newSelection.size() : 0;
052        return trn("Selected {0} object", "Selected {0} objects", size, size);
053    }
054
055    @Override
056    public int hashCode() {
057        return Objects.hash(super.hashCode(), newSelection, oldSelection);
058    }
059
060    @Override
061    public boolean equals(Object obj) {
062        if (this == obj) return true;
063        if (obj == null || getClass() != obj.getClass()) return false;
064        if (!super.equals(obj)) return false;
065        SelectCommand that = (SelectCommand) obj;
066        return Objects.equals(newSelection, that.newSelection) &&
067                Objects.equals(oldSelection, that.oldSelection);
068    }
069}