[6380] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
[21] | 2 | package org.openstreetmap.josm.command;
|
---|
| 3 |
|
---|
[1989] | 4 | import static org.openstreetmap.josm.tools.I18n.marktr;
|
---|
[104] | 5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
| 6 |
|
---|
[22] | 7 | import java.util.Collection;
|
---|
[3262] | 8 | import java.util.Collections;
|
---|
[9371] | 9 | import java.util.Objects;
|
---|
[21] | 10 |
|
---|
[4918] | 11 | import javax.swing.Icon;
|
---|
[94] | 12 |
|
---|
[11240] | 13 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
[12663] | 14 | import org.openstreetmap.josm.data.osm.DefaultNameFormatter;
|
---|
[21] | 15 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
[1814] | 16 | import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
|
---|
[7587] | 17 | import org.openstreetmap.josm.data.osm.Way;
|
---|
[1857] | 18 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
[1814] | 19 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
[21] | 20 |
|
---|
| 21 | /**
|
---|
[6881] | 22 | * A command that adds an osm primitive to a dataset. Keys cannot be added this way.
|
---|
[1169] | 23 | *
|
---|
[5266] | 24 | * See {@link ChangeCommand} for comments on relation back references.
|
---|
[1169] | 25 | *
|
---|
[21] | 26 | * @author imi
|
---|
| 27 | */
|
---|
[86] | 28 | public class AddCommand extends Command {
|
---|
[21] | 29 |
|
---|
[1169] | 30 | /**
|
---|
| 31 | * The primitive to add to the dataset.
|
---|
| 32 | */
|
---|
| 33 | private final OsmPrimitive osm;
|
---|
[21] | 34 |
|
---|
[1169] | 35 | /**
|
---|
[6881] | 36 | * Creates the command and specify the element to add in the context of the current edit layer, if any.
|
---|
| 37 | * @param osm The primitive to add
|
---|
[1169] | 38 | */
|
---|
| 39 | public AddCommand(OsmPrimitive osm) {
|
---|
[11140] | 40 | this.osm = Objects.requireNonNull(osm, "osm");
|
---|
[1169] | 41 | }
|
---|
[30] | 42 |
|
---|
[1857] | 43 | /**
|
---|
[6881] | 44 | * Creates the command and specify the element to add in the context of the given data layer.
|
---|
| 45 | * @param layer The data layer. Must not be {@code null}
|
---|
| 46 | * @param osm The primitive to add
|
---|
[1857] | 47 | */
|
---|
| 48 | public AddCommand(OsmDataLayer layer, OsmPrimitive osm) {
|
---|
| 49 | super(layer);
|
---|
[11140] | 50 | this.osm = Objects.requireNonNull(osm, "osm");
|
---|
[1857] | 51 | }
|
---|
| 52 |
|
---|
[11240] | 53 | /**
|
---|
| 54 | * Creates the command and specify the element to add in the context of the given data set.
|
---|
| 55 | * @param data The data set. Must not be {@code null}
|
---|
| 56 | * @param osm The primitive to add
|
---|
| 57 | * @since 11240
|
---|
| 58 | */
|
---|
| 59 | public AddCommand(DataSet data, OsmPrimitive osm) {
|
---|
| 60 | super(data);
|
---|
| 61 | this.osm = Objects.requireNonNull(osm, "osm");
|
---|
| 62 | }
|
---|
| 63 |
|
---|
[7587] | 64 | protected static final void checkNodeStyles(OsmPrimitive osm) {
|
---|
| 65 | if (osm instanceof Way) {
|
---|
| 66 | // Fix #10557 - node icon not updated after undoing/redoing addition of a way
|
---|
[8510] | 67 | ((Way) osm).clearCachedNodeStyles();
|
---|
[7587] | 68 | }
|
---|
| 69 | }
|
---|
| 70 |
|
---|
[6881] | 71 | @Override
|
---|
| 72 | public boolean executeCommand() {
|
---|
[10467] | 73 | getAffectedDataSet().addPrimitive(osm);
|
---|
[5112] | 74 | osm.setModified(true);
|
---|
[7587] | 75 | checkNodeStyles(osm);
|
---|
[1169] | 76 | return true;
|
---|
| 77 | }
|
---|
[30] | 78 |
|
---|
[6881] | 79 | @Override
|
---|
| 80 | public void undoCommand() {
|
---|
[10467] | 81 | getAffectedDataSet().removePrimitive(osm);
|
---|
[7587] | 82 | checkNodeStyles(osm);
|
---|
[1169] | 83 | }
|
---|
[94] | 84 |
|
---|
[6881] | 85 | @Override
|
---|
| 86 | public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
|
---|
[1169] | 87 | added.add(osm);
|
---|
[304] | 88 | }
|
---|
| 89 |
|
---|
[4918] | 90 | @Override
|
---|
| 91 | public String getDescriptionText() {
|
---|
[1989] | 92 | String msg;
|
---|
| 93 | switch(OsmPrimitiveType.from(osm)) {
|
---|
| 94 | case NODE: msg = marktr("Add node {0}"); break;
|
---|
| 95 | case WAY: msg = marktr("Add way {0}"); break;
|
---|
| 96 | case RELATION: msg = marktr("Add relation {0}"); break;
|
---|
| 97 | default: /* should not happen */msg = ""; break;
|
---|
| 98 | }
|
---|
[4918] | 99 | return tr(msg, osm.getDisplayName(DefaultNameFormatter.getInstance()));
|
---|
| 100 | }
|
---|
[1989] | 101 |
|
---|
[4918] | 102 | @Override
|
---|
| 103 | public Icon getDescriptionIcon() {
|
---|
[5077] | 104 | return ImageProvider.get(osm.getDisplayType());
|
---|
[1169] | 105 | }
|
---|
[3262] | 106 |
|
---|
| 107 | @Override
|
---|
| 108 | public Collection<OsmPrimitive> getParticipatingPrimitives() {
|
---|
| 109 | return Collections.singleton(osm);
|
---|
| 110 | }
|
---|
[8456] | 111 |
|
---|
| 112 | @Override
|
---|
| 113 | public int hashCode() {
|
---|
[9371] | 114 | return Objects.hash(super.hashCode(), osm);
|
---|
[8456] | 115 | }
|
---|
| 116 |
|
---|
| 117 | @Override
|
---|
| 118 | public boolean equals(Object obj) {
|
---|
[9371] | 119 | if (this == obj) return true;
|
---|
| 120 | if (obj == null || getClass() != obj.getClass()) return false;
|
---|
| 121 | if (!super.equals(obj)) return false;
|
---|
| 122 | AddCommand that = (AddCommand) obj;
|
---|
| 123 | return Objects.equals(osm, that.osm);
|
---|
[8456] | 124 | }
|
---|
[21] | 125 | }
|
---|