[7122] | 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;
|
---|
[10663] | 7 | import java.util.Collections;
|
---|
| 8 | import java.util.HashSet;
|
---|
[9371] | 9 | import java.util.Objects;
|
---|
[7122] | 10 |
|
---|
[12349] | 11 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
[7122] | 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 newSelection the primitives to select when executing the command.
|
---|
[12726] | 30 | * @deprecated to be removed end of 2017. Use {@link #SelectCommand(DataSet, Collection)} instead
|
---|
[7122] | 31 | */
|
---|
[12726] | 32 | @Deprecated
|
---|
[7122] | 33 | public SelectCommand(Collection<OsmPrimitive> newSelection) {
|
---|
[10663] | 34 | if (newSelection == null || newSelection.isEmpty()) {
|
---|
| 35 | this.newSelection = Collections.emptySet();
|
---|
| 36 | } else {
|
---|
| 37 | this.newSelection = new HashSet<>(newSelection);
|
---|
| 38 | }
|
---|
[7122] | 39 | }
|
---|
| 40 |
|
---|
[12349] | 41 | /**
|
---|
| 42 | * Constructs a new select command.
|
---|
| 43 | * @param dataset The dataset the selection belongs to
|
---|
| 44 | * @param newSelection the primitives to select when executing the command.
|
---|
| 45 | * @since 12349
|
---|
| 46 | */
|
---|
| 47 | public SelectCommand(DataSet dataset, Collection<OsmPrimitive> newSelection) {
|
---|
| 48 | super(dataset);
|
---|
| 49 | if (newSelection == null || newSelection.isEmpty()) {
|
---|
| 50 | this.newSelection = Collections.emptySet();
|
---|
| 51 | } else {
|
---|
| 52 | this.newSelection = new HashSet<>(newSelection);
|
---|
| 53 | }
|
---|
| 54 | }
|
---|
| 55 |
|
---|
[7122] | 56 | @Override
|
---|
| 57 | public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
|
---|
[9989] | 58 | // Do nothing
|
---|
[7122] | 59 | }
|
---|
| 60 |
|
---|
| 61 | @Override
|
---|
| 62 | public void undoCommand() {
|
---|
[12350] | 63 | ensurePrimitivesAreInDataset();
|
---|
| 64 |
|
---|
[12349] | 65 | getAffectedDataSet().setSelected(oldSelection);
|
---|
[7122] | 66 | }
|
---|
| 67 |
|
---|
| 68 | @Override
|
---|
| 69 | public boolean executeCommand() {
|
---|
[12350] | 70 | ensurePrimitivesAreInDataset();
|
---|
| 71 |
|
---|
[12349] | 72 | oldSelection = getAffectedDataSet().getSelected();
|
---|
| 73 | getAffectedDataSet().setSelected(newSelection);
|
---|
[7122] | 74 | return true;
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | @Override
|
---|
[12350] | 78 | public Collection<? extends OsmPrimitive> getParticipatingPrimitives() {
|
---|
| 79 | return Collections.unmodifiableCollection(newSelection);
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | @Override
|
---|
[7122] | 83 | public String getDescriptionText() {
|
---|
| 84 | int size = newSelection != null ? newSelection.size() : 0;
|
---|
| 85 | return trn("Selected {0} object", "Selected {0} objects", size, size);
|
---|
| 86 | }
|
---|
[8456] | 87 |
|
---|
| 88 | @Override
|
---|
| 89 | public int hashCode() {
|
---|
[9371] | 90 | return Objects.hash(super.hashCode(), newSelection, oldSelection);
|
---|
[8456] | 91 | }
|
---|
| 92 |
|
---|
| 93 | @Override
|
---|
| 94 | public boolean equals(Object obj) {
|
---|
[9371] | 95 | if (this == obj) return true;
|
---|
| 96 | if (obj == null || getClass() != obj.getClass()) return false;
|
---|
| 97 | if (!super.equals(obj)) return false;
|
---|
| 98 | SelectCommand that = (SelectCommand) obj;
|
---|
| 99 | return Objects.equals(newSelection, that.newSelection) &&
|
---|
| 100 | Objects.equals(oldSelection, that.oldSelection);
|
---|
[8456] | 101 | }
|
---|
[7122] | 102 | }
|
---|