[6380] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
[343] | 2 | package org.openstreetmap.josm.command;
|
---|
| 3 |
|
---|
[1989] | 4 | import static org.openstreetmap.josm.tools.I18n.marktr;
|
---|
[343] | 5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
| 6 |
|
---|
| 7 | import java.util.Collection;
|
---|
[9371] | 8 | import java.util.Objects;
|
---|
[5077] | 9 |
|
---|
[4918] | 10 | import javax.swing.Icon;
|
---|
[343] | 11 |
|
---|
[11240] | 12 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
[12663] | 13 | import org.openstreetmap.josm.data.osm.DefaultNameFormatter;
|
---|
[343] | 14 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
[1814] | 15 | import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
|
---|
[6302] | 16 | import org.openstreetmap.josm.data.osm.Way;
|
---|
[3152] | 17 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
[6302] | 18 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
[1814] | 19 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
[343] | 20 |
|
---|
| 21 | /**
|
---|
[6365] | 22 | * Command that basically replaces one OSM primitive by another of the same type.
|
---|
[1169] | 23 | *
|
---|
[6365] | 24 | * @since 93
|
---|
[343] | 25 | */
|
---|
| 26 | public class ChangeCommand extends Command {
|
---|
| 27 |
|
---|
[1169] | 28 | private final OsmPrimitive osm;
|
---|
| 29 | private final OsmPrimitive newOsm;
|
---|
[343] | 30 |
|
---|
[6881] | 31 | /**
|
---|
| 32 | * Constructs a new {@code ChangeCommand} in the context of the current edit layer, if any.
|
---|
| 33 | * @param osm The existing primitive to modify
|
---|
| 34 | * @param newOsm The new primitive
|
---|
| 35 | */
|
---|
[1169] | 36 | public ChangeCommand(OsmPrimitive osm, OsmPrimitive newOsm) {
|
---|
| 37 | this.osm = osm;
|
---|
| 38 | this.newOsm = newOsm;
|
---|
[6302] | 39 | sanityChecks();
|
---|
[343] | 40 | }
|
---|
| 41 |
|
---|
[6881] | 42 | /**
|
---|
| 43 | * Constructs a new {@code ChangeCommand} in the context of a given data layer.
|
---|
| 44 | * @param layer The data layer
|
---|
| 45 | * @param osm The existing primitive to modify
|
---|
| 46 | * @param newOsm The new primitive
|
---|
[12718] | 47 | * @deprecated to be removed end of 2017. Use {@link #ChangeCommand(DataSet, OsmPrimitive, OsmPrimitive)} instead
|
---|
[6881] | 48 | */
|
---|
[12718] | 49 | @Deprecated
|
---|
[3152] | 50 | public ChangeCommand(OsmDataLayer layer, OsmPrimitive osm, OsmPrimitive newOsm) {
|
---|
| 51 | super(layer);
|
---|
| 52 | this.osm = osm;
|
---|
| 53 | this.newOsm = newOsm;
|
---|
[6302] | 54 | sanityChecks();
|
---|
[3152] | 55 | }
|
---|
[6881] | 56 |
|
---|
[11240] | 57 | /**
|
---|
| 58 | * Constructs a new {@code ChangeCommand} in the context of a given data set.
|
---|
| 59 | * @param data The data set
|
---|
| 60 | * @param osm The existing primitive to modify
|
---|
| 61 | * @param newOsm The new primitive
|
---|
| 62 | * @since 11240
|
---|
| 63 | */
|
---|
| 64 | public ChangeCommand(DataSet data, OsmPrimitive osm, OsmPrimitive newOsm) {
|
---|
| 65 | super(data);
|
---|
| 66 | this.osm = osm;
|
---|
| 67 | this.newOsm = newOsm;
|
---|
| 68 | sanityChecks();
|
---|
| 69 | }
|
---|
| 70 |
|
---|
[6302] | 71 | private void sanityChecks() {
|
---|
| 72 | CheckParameterUtil.ensureParameterNotNull(osm, "osm");
|
---|
| 73 | CheckParameterUtil.ensureParameterNotNull(newOsm, "newOsm");
|
---|
[8510] | 74 | if (newOsm instanceof Way && ((Way) newOsm).getNodesCount() == 0) {
|
---|
[6302] | 75 | // Do not allow to create empty ways (see #7465)
|
---|
[6365] | 76 | throw new IllegalArgumentException(tr("New way {0} has 0 nodes", newOsm));
|
---|
[6302] | 77 | }
|
---|
| 78 | }
|
---|
[3152] | 79 |
|
---|
[6365] | 80 | @Override
|
---|
| 81 | public boolean executeCommand() {
|
---|
[1169] | 82 | super.executeCommand();
|
---|
| 83 | osm.cloneFrom(newOsm);
|
---|
[2025] | 84 | osm.setModified(true);
|
---|
[1169] | 85 | return true;
|
---|
[343] | 86 | }
|
---|
| 87 |
|
---|
[6365] | 88 | @Override
|
---|
| 89 | public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
|
---|
[1169] | 90 | modified.add(osm);
|
---|
[343] | 91 | }
|
---|
| 92 |
|
---|
[4918] | 93 | @Override
|
---|
| 94 | public String getDescriptionText() {
|
---|
[10216] | 95 | String msg;
|
---|
[1989] | 96 | switch(OsmPrimitiveType.from(osm)) {
|
---|
[3152] | 97 | case NODE: msg = marktr("Change node {0}"); break;
|
---|
| 98 | case WAY: msg = marktr("Change way {0}"); break;
|
---|
| 99 | case RELATION: msg = marktr("Change relation {0}"); break;
|
---|
[10216] | 100 | default: throw new AssertionError();
|
---|
[1989] | 101 | }
|
---|
[4918] | 102 | return tr(msg, osm.getDisplayName(DefaultNameFormatter.getInstance()));
|
---|
[343] | 103 | }
|
---|
[4918] | 104 |
|
---|
| 105 | @Override
|
---|
| 106 | public Icon getDescriptionIcon() {
|
---|
[5077] | 107 | return ImageProvider.get(osm.getDisplayType());
|
---|
[4918] | 108 | }
|
---|
[8456] | 109 |
|
---|
| 110 | @Override
|
---|
| 111 | public int hashCode() {
|
---|
[9371] | 112 | return Objects.hash(super.hashCode(), osm, newOsm);
|
---|
[8456] | 113 | }
|
---|
| 114 |
|
---|
| 115 | @Override
|
---|
| 116 | public boolean equals(Object obj) {
|
---|
[9371] | 117 | if (this == obj) return true;
|
---|
| 118 | if (obj == null || getClass() != obj.getClass()) return false;
|
---|
| 119 | if (!super.equals(obj)) return false;
|
---|
| 120 | ChangeCommand that = (ChangeCommand) obj;
|
---|
| 121 | return Objects.equals(osm, that.osm) &&
|
---|
| 122 | Objects.equals(newOsm, that.newOsm);
|
---|
[8456] | 123 | }
|
---|
[343] | 124 | }
|
---|