[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 |
|
---|
| 11 | import org.openstreetmap.josm.Main;
|
---|
| 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.
|
---|
| 30 | */
|
---|
| 31 | public SelectCommand(Collection<OsmPrimitive> newSelection) {
|
---|
[10663] | 32 | if (newSelection == null || newSelection.isEmpty()) {
|
---|
| 33 | this.newSelection = Collections.emptySet();
|
---|
| 34 | } else {
|
---|
| 35 | this.newSelection = new HashSet<>(newSelection);
|
---|
| 36 | }
|
---|
[7122] | 37 | }
|
---|
| 38 |
|
---|
| 39 | @Override
|
---|
| 40 | public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
|
---|
[9989] | 41 | // Do nothing
|
---|
[7122] | 42 | }
|
---|
| 43 |
|
---|
| 44 | @Override
|
---|
| 45 | public void undoCommand() {
|
---|
[10364] | 46 | Main.getLayerManager().getEditLayer().data.setSelected(oldSelection);
|
---|
[7122] | 47 | }
|
---|
| 48 |
|
---|
| 49 | @Override
|
---|
| 50 | public boolean executeCommand() {
|
---|
[10364] | 51 | oldSelection = Main.getLayerManager().getEditLayer().data.getSelected();
|
---|
| 52 | Main.getLayerManager().getEditLayer().data.setSelected(newSelection);
|
---|
[7122] | 53 | return true;
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | @Override
|
---|
| 57 | public String getDescriptionText() {
|
---|
| 58 | int size = newSelection != null ? newSelection.size() : 0;
|
---|
| 59 | return trn("Selected {0} object", "Selected {0} objects", size, size);
|
---|
| 60 | }
|
---|
[8456] | 61 |
|
---|
| 62 | @Override
|
---|
| 63 | public int hashCode() {
|
---|
[9371] | 64 | return Objects.hash(super.hashCode(), newSelection, oldSelection);
|
---|
[8456] | 65 | }
|
---|
| 66 |
|
---|
| 67 | @Override
|
---|
| 68 | public boolean equals(Object obj) {
|
---|
[9371] | 69 | if (this == obj) return true;
|
---|
| 70 | if (obj == null || getClass() != obj.getClass()) return false;
|
---|
| 71 | if (!super.equals(obj)) return false;
|
---|
| 72 | SelectCommand that = (SelectCommand) obj;
|
---|
| 73 | return Objects.equals(newSelection, that.newSelection) &&
|
---|
| 74 | Objects.equals(oldSelection, that.oldSelection);
|
---|
[8456] | 75 | }
|
---|
[7122] | 76 | }
|
---|