1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.command;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.trn;
|
---|
5 |
|
---|
6 | import java.util.Collection;
|
---|
7 | import java.util.Collections;
|
---|
8 | import java.util.HashSet;
|
---|
9 | import java.util.Objects;
|
---|
10 |
|
---|
11 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
12 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
13 |
|
---|
14 | /**
|
---|
15 | * Command that selects OSM primitives
|
---|
16 | *
|
---|
17 | * @author Landwirt
|
---|
18 | */
|
---|
19 | public 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 | }
|
---|