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; 007 008import org.openstreetmap.josm.Main; 009import org.openstreetmap.josm.data.osm.OsmPrimitive; 010 011/** 012 * Command that selects OSM primitives 013 * 014 * @author Landwirt 015 */ 016public class SelectCommand extends Command { 017 018 /** the primitives to select when executing the command */ 019 private final Collection<OsmPrimitive> newSelection; 020 021 /** the selection before applying the new selection */ 022 private Collection<OsmPrimitive> oldSelection; 023 024 /** 025 * Constructs a new select command. 026 * @param newSelection the primitives to select when executing the command. 027 */ 028 public SelectCommand(Collection<OsmPrimitive> newSelection) { 029 this.newSelection = newSelection; 030 } 031 032 @Override 033 public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) { 034 } 035 036 @Override 037 public void undoCommand() { 038 Main.map.mapView.getEditLayer().data.setSelected(oldSelection); 039 } 040 041 @Override 042 public boolean executeCommand() { 043 oldSelection = Main.map.mapView.getEditLayer().data.getSelected(); 044 Main.map.mapView.getEditLayer().data.setSelected(newSelection); 045 return true; 046 } 047 048 @Override 049 public String getDescriptionText() { 050 int size = newSelection != null ? newSelection.size() : 0; 051 return trn("Selected {0} object", "Selected {0} objects", size, size); 052 } 053 054 @Override 055 public int hashCode() { 056 final int prime = 31; 057 int result = super.hashCode(); 058 result = prime * result + ((newSelection == null) ? 0 : newSelection.hashCode()); 059 result = prime * result + ((oldSelection == null) ? 0 : oldSelection.hashCode()); 060 return result; 061 } 062 063 @Override 064 public boolean equals(Object obj) { 065 if (this == obj) 066 return true; 067 if (!super.equals(obj)) 068 return false; 069 if (getClass() != obj.getClass()) 070 return false; 071 SelectCommand other = (SelectCommand) obj; 072 if (newSelection == null) { 073 if (other.newSelection != null) 074 return false; 075 } else if (!newSelection.equals(other.newSelection)) 076 return false; 077 if (oldSelection == null) { 078 if (other.oldSelection != null) 079 return false; 080 } else if (!oldSelection.equals(other.oldSelection)) 081 return false; 082 return true; 083 } 084}