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