[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.
|
---|
[12349] | 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 |
|
---|
[7122] | 42 | @Override
|
---|
| 43 | public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
|
---|
[9989] | 44 | // Do nothing
|
---|
[7122] | 45 | }
|
---|
| 46 |
|
---|
| 47 | @Override
|
---|
| 48 | public void undoCommand() {
|
---|
[12350] | 49 | ensurePrimitivesAreInDataset();
|
---|
| 50 |
|
---|
[12349] | 51 | getAffectedDataSet().setSelected(oldSelection);
|
---|
[7122] | 52 | }
|
---|
| 53 |
|
---|
| 54 | @Override
|
---|
| 55 | public boolean executeCommand() {
|
---|
[12350] | 56 | ensurePrimitivesAreInDataset();
|
---|
| 57 |
|
---|
[12349] | 58 | oldSelection = getAffectedDataSet().getSelected();
|
---|
| 59 | getAffectedDataSet().setSelected(newSelection);
|
---|
[7122] | 60 | return true;
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | @Override
|
---|
[12350] | 64 | public Collection<? extends OsmPrimitive> getParticipatingPrimitives() {
|
---|
| 65 | return Collections.unmodifiableCollection(newSelection);
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | @Override
|
---|
[7122] | 69 | public String getDescriptionText() {
|
---|
| 70 | int size = newSelection != null ? newSelection.size() : 0;
|
---|
| 71 | return trn("Selected {0} object", "Selected {0} objects", size, size);
|
---|
| 72 | }
|
---|
[8456] | 73 |
|
---|
| 74 | @Override
|
---|
| 75 | public int hashCode() {
|
---|
[9371] | 76 | return Objects.hash(super.hashCode(), newSelection, oldSelection);
|
---|
[8456] | 77 | }
|
---|
| 78 |
|
---|
| 79 | @Override
|
---|
| 80 | public boolean equals(Object obj) {
|
---|
[9371] | 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);
|
---|
[8456] | 87 | }
|
---|
[7122] | 88 | }
|
---|