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

Last change on this file since 12718 was 12350, checked in by michael2402, 7 years ago

See #13036: Add data set tests to SelectCommand

  • Property svn:eol-style set to native
File size: 3.0 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.data.osm.DataSet;
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 /**
40 * Constructs a new select command.
41 * @param dataset The dataset the selection belongs to
42 * @param newSelection the primitives to select when executing the command.
43 * @since 12349
44 */
45 public SelectCommand(DataSet dataset, Collection<OsmPrimitive> newSelection) {
46 super(dataset);
47 if (newSelection == null || newSelection.isEmpty()) {
48 this.newSelection = Collections.emptySet();
49 } else {
50 this.newSelection = new HashSet<>(newSelection);
51 }
52 }
53
54 @Override
55 public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
56 // Do nothing
57 }
58
59 @Override
60 public void undoCommand() {
61 ensurePrimitivesAreInDataset();
62
63 getAffectedDataSet().setSelected(oldSelection);
64 }
65
66 @Override
67 public boolean executeCommand() {
68 ensurePrimitivesAreInDataset();
69
70 oldSelection = getAffectedDataSet().getSelected();
71 getAffectedDataSet().setSelected(newSelection);
72 return true;
73 }
74
75 @Override
76 public Collection<? extends OsmPrimitive> getParticipatingPrimitives() {
77 return Collections.unmodifiableCollection(newSelection);
78 }
79
80 @Override
81 public String getDescriptionText() {
82 int size = newSelection != null ? newSelection.size() : 0;
83 return trn("Selected {0} object", "Selected {0} objects", size, size);
84 }
85
86 @Override
87 public int hashCode() {
88 return Objects.hash(super.hashCode(), newSelection, oldSelection);
89 }
90
91 @Override
92 public boolean equals(Object obj) {
93 if (this == obj) return true;
94 if (obj == null || getClass() != obj.getClass()) return false;
95 if (!super.equals(obj)) return false;
96 SelectCommand that = (SelectCommand) obj;
97 return Objects.equals(newSelection, that.newSelection) &&
98 Objects.equals(oldSelection, that.oldSelection);
99 }
100}
Note: See TracBrowser for help on using the repository browser.