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

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

Fix SelectCommand triggering a critical error when it is undone after the edit layer was changed.

  • Property svn:eol-style set to native
File size: 2.8 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 getAffectedDataSet().setSelected(oldSelection);
62 }
63
64 @Override
65 public boolean executeCommand() {
66 oldSelection = getAffectedDataSet().getSelected();
67 getAffectedDataSet().setSelected(newSelection);
68 return true;
69 }
70
71 @Override
72 public String getDescriptionText() {
73 int size = newSelection != null ? newSelection.size() : 0;
74 return trn("Selected {0} object", "Selected {0} objects", size, size);
75 }
76
77 @Override
78 public int hashCode() {
79 return Objects.hash(super.hashCode(), newSelection, oldSelection);
80 }
81
82 @Override
83 public boolean equals(Object obj) {
84 if (this == obj) return true;
85 if (obj == null || getClass() != obj.getClass()) return false;
86 if (!super.equals(obj)) return false;
87 SelectCommand that = (SelectCommand) obj;
88 return Objects.equals(newSelection, that.newSelection) &&
89 Objects.equals(oldSelection, that.oldSelection);
90 }
91}
Note: See TracBrowser for help on using the repository browser.