source: josm/trunk/src/org/openstreetmap/josm/command/SelectCommand.java@ 12225

Last change on this file since 12225 was 10663, checked in by Don-vip, 8 years ago

fix #13223 - Minor command class fixes (patch by michael2402, modified) - gsoc-core

  • Property svn:eol-style set to native
File size: 2.3 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.command;
3
4import static org.openstreetmap.josm.tools.I18n.trn;
5
6import java.util.Collection;
7import java.util.Collections;
8import java.util.HashSet;
9import java.util.Objects;
10
11import org.openstreetmap.josm.Main;
12import org.openstreetmap.josm.data.osm.OsmPrimitive;
13
14/**
15 * Command that selects OSM primitives
16 *
17 * @author Landwirt
18 */
19public class SelectCommand extends Command {
20
21 /** the primitives to select when executing the command */
22 private final Collection<OsmPrimitive> newSelection;
23
24 /** the selection before applying the new selection */
25 private Collection<OsmPrimitive> oldSelection;
26
27 /**
28 * Constructs a new select command.
29 * @param newSelection the primitives to select when executing the command.
30 */
31 public SelectCommand(Collection<OsmPrimitive> newSelection) {
32 if (newSelection == null || newSelection.isEmpty()) {
33 this.newSelection = Collections.emptySet();
34 } else {
35 this.newSelection = new HashSet<>(newSelection);
36 }
37 }
38
39 @Override
40 public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
41 // Do nothing
42 }
43
44 @Override
45 public void undoCommand() {
46 Main.getLayerManager().getEditLayer().data.setSelected(oldSelection);
47 }
48
49 @Override
50 public boolean executeCommand() {
51 oldSelection = Main.getLayerManager().getEditLayer().data.getSelected();
52 Main.getLayerManager().getEditLayer().data.setSelected(newSelection);
53 return true;
54 }
55
56 @Override
57 public String getDescriptionText() {
58 int size = newSelection != null ? newSelection.size() : 0;
59 return trn("Selected {0} object", "Selected {0} objects", size, size);
60 }
61
62 @Override
63 public int hashCode() {
64 return Objects.hash(super.hashCode(), newSelection, oldSelection);
65 }
66
67 @Override
68 public boolean equals(Object obj) {
69 if (this == obj) return true;
70 if (obj == null || getClass() != obj.getClass()) return false;
71 if (!super.equals(obj)) return false;
72 SelectCommand that = (SelectCommand) obj;
73 return Objects.equals(newSelection, that.newSelection) &&
74 Objects.equals(oldSelection, that.oldSelection);
75 }
76}
Note: See TracBrowser for help on using the repository browser.