[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
|
---|
| 47 | */
|
---|
[3152] | 48 | public ChangeCommand(OsmDataLayer layer, OsmPrimitive osm, OsmPrimitive newOsm) {
|
---|
| 49 | super(layer);
|
---|
| 50 | this.osm = osm;
|
---|
| 51 | this.newOsm = newOsm;
|
---|
[6302] | 52 | sanityChecks();
|
---|
[3152] | 53 | }
|
---|
[6881] | 54 |
|
---|
[11240] | 55 | /**
|
---|
| 56 | * Constructs a new {@code ChangeCommand} in the context of a given data set.
|
---|
| 57 | * @param data The data set
|
---|
| 58 | * @param osm The existing primitive to modify
|
---|
| 59 | * @param newOsm The new primitive
|
---|
| 60 | * @since 11240
|
---|
| 61 | */
|
---|
| 62 | public ChangeCommand(DataSet data, OsmPrimitive osm, OsmPrimitive newOsm) {
|
---|
| 63 | super(data);
|
---|
| 64 | this.osm = osm;
|
---|
| 65 | this.newOsm = newOsm;
|
---|
| 66 | sanityChecks();
|
---|
| 67 | }
|
---|
| 68 |
|
---|
[6302] | 69 | private void sanityChecks() {
|
---|
| 70 | CheckParameterUtil.ensureParameterNotNull(osm, "osm");
|
---|
| 71 | CheckParameterUtil.ensureParameterNotNull(newOsm, "newOsm");
|
---|
[8510] | 72 | if (newOsm instanceof Way && ((Way) newOsm).getNodesCount() == 0) {
|
---|
[6302] | 73 | // Do not allow to create empty ways (see #7465)
|
---|
[6365] | 74 | throw new IllegalArgumentException(tr("New way {0} has 0 nodes", newOsm));
|
---|
[6302] | 75 | }
|
---|
| 76 | }
|
---|
[3152] | 77 |
|
---|
[6365] | 78 | @Override
|
---|
| 79 | public boolean executeCommand() {
|
---|
[1169] | 80 | super.executeCommand();
|
---|
| 81 | osm.cloneFrom(newOsm);
|
---|
[2025] | 82 | osm.setModified(true);
|
---|
[1169] | 83 | return true;
|
---|
[343] | 84 | }
|
---|
| 85 |
|
---|
[6365] | 86 | @Override
|
---|
| 87 | public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
|
---|
[1169] | 88 | modified.add(osm);
|
---|
[343] | 89 | }
|
---|
| 90 |
|
---|
[4918] | 91 | @Override
|
---|
| 92 | public String getDescriptionText() {
|
---|
[10216] | 93 | String msg;
|
---|
[1989] | 94 | switch(OsmPrimitiveType.from(osm)) {
|
---|
[3152] | 95 | case NODE: msg = marktr("Change node {0}"); break;
|
---|
| 96 | case WAY: msg = marktr("Change way {0}"); break;
|
---|
| 97 | case RELATION: msg = marktr("Change relation {0}"); break;
|
---|
[10216] | 98 | default: throw new AssertionError();
|
---|
[1989] | 99 | }
|
---|
[4918] | 100 | return tr(msg, osm.getDisplayName(DefaultNameFormatter.getInstance()));
|
---|
[343] | 101 | }
|
---|
[4918] | 102 |
|
---|
| 103 | @Override
|
---|
| 104 | public Icon getDescriptionIcon() {
|
---|
[5077] | 105 | return ImageProvider.get(osm.getDisplayType());
|
---|
[4918] | 106 | }
|
---|
[8456] | 107 |
|
---|
| 108 | @Override
|
---|
| 109 | public int hashCode() {
|
---|
[9371] | 110 | return Objects.hash(super.hashCode(), osm, newOsm);
|
---|
[8456] | 111 | }
|
---|
| 112 |
|
---|
| 113 | @Override
|
---|
| 114 | public boolean equals(Object obj) {
|
---|
[9371] | 115 | if (this == obj) return true;
|
---|
| 116 | if (obj == null || getClass() != obj.getClass()) return false;
|
---|
| 117 | if (!super.equals(obj)) return false;
|
---|
| 118 | ChangeCommand that = (ChangeCommand) obj;
|
---|
| 119 | return Objects.equals(osm, that.osm) &&
|
---|
| 120 | Objects.equals(newOsm, that.newOsm);
|
---|
[8456] | 121 | }
|
---|
[343] | 122 | }
|
---|