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 // Do nothing 036 } 037 038 @Override 039 public void undoCommand() { 040 Main.map.mapView.getEditLayer().data.setSelected(oldSelection); 041 } 042 043 @Override 044 public boolean executeCommand() { 045 oldSelection = Main.map.mapView.getEditLayer().data.getSelected(); 046 Main.map.mapView.getEditLayer().data.setSelected(newSelection); 047 return true; 048 } 049 050 @Override 051 public String getDescriptionText() { 052 int size = newSelection != null ? newSelection.size() : 0; 053 return trn("Selected {0} object", "Selected {0} objects", size, size); 054 } 055 056 @Override 057 public int hashCode() { 058 return Objects.hash(super.hashCode(), newSelection, oldSelection); 059 } 060 061 @Override 062 public boolean equals(Object obj) { 063 if (this == obj) return true; 064 if (obj == null || getClass() != obj.getClass()) return false; 065 if (!super.equals(obj)) return false; 066 SelectCommand that = (SelectCommand) obj; 067 return Objects.equals(newSelection, that.newSelection) && 068 Objects.equals(oldSelection, that.oldSelection); 069 } 070}