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

Last change on this file since 14173 was 13173, checked in by Don-vip, 6 years ago

see #15310 - remove most of deprecated APIs

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