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 | */
|
---|
35 | public ChangeCommand(OsmPrimitive osm, OsmPrimitive newOsm) {
|
---|
36 | this(osm.getDataSet(), osm, newOsm);
|
---|
37 | }
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * Constructs a new {@code ChangeCommand} in the context of a given data set.
|
---|
41 | * @param data The data set
|
---|
42 | * @param osm The existing primitive to modify
|
---|
43 | * @param newOsm The new primitive
|
---|
44 | * @since 11240
|
---|
45 | */
|
---|
46 | public ChangeCommand(DataSet data, OsmPrimitive osm, OsmPrimitive newOsm) {
|
---|
47 | super(data);
|
---|
48 | this.osm = osm;
|
---|
49 | this.newOsm = newOsm;
|
---|
50 | sanityChecks();
|
---|
51 | }
|
---|
52 |
|
---|
53 | private void sanityChecks() {
|
---|
54 | CheckParameterUtil.ensureParameterNotNull(osm, "osm");
|
---|
55 | CheckParameterUtil.ensureParameterNotNull(newOsm, "newOsm");
|
---|
56 | if (newOsm instanceof Way && ((Way) newOsm).getNodesCount() == 0) {
|
---|
57 | // Do not allow to create empty ways (see #7465)
|
---|
58 | throw new IllegalArgumentException(tr("New way {0} has 0 nodes", newOsm));
|
---|
59 | }
|
---|
60 | }
|
---|
61 |
|
---|
62 | @Override
|
---|
63 | public boolean executeCommand() {
|
---|
64 | super.executeCommand();
|
---|
65 | osm.cloneFrom(newOsm);
|
---|
66 | osm.setModified(true);
|
---|
67 | return true;
|
---|
68 | }
|
---|
69 |
|
---|
70 | @Override
|
---|
71 | public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
|
---|
72 | modified.add(osm);
|
---|
73 | }
|
---|
74 |
|
---|
75 | @Override
|
---|
76 | public String getDescriptionText() {
|
---|
77 | String msg;
|
---|
78 | switch(OsmPrimitiveType.from(osm)) {
|
---|
79 | case NODE: msg = marktr("Change node {0}"); break;
|
---|
80 | case WAY: msg = marktr("Change way {0}"); break;
|
---|
81 | case RELATION: msg = marktr("Change relation {0}"); break;
|
---|
82 | default: throw new AssertionError();
|
---|
83 | }
|
---|
84 | return tr(msg, osm.getDisplayName(DefaultNameFormatter.getInstance()));
|
---|
85 | }
|
---|
86 |
|
---|
87 | @Override
|
---|
88 | public Icon getDescriptionIcon() {
|
---|
89 | return ImageProvider.get(osm.getDisplayType());
|
---|
90 | }
|
---|
91 |
|
---|
92 | @Override
|
---|
93 | public int hashCode() {
|
---|
94 | return Objects.hash(super.hashCode(), osm, newOsm);
|
---|
95 | }
|
---|
96 |
|
---|
97 | @Override
|
---|
98 | public boolean equals(Object obj) {
|
---|
99 | if (this == obj) return true;
|
---|
100 | if (obj == null || getClass() != obj.getClass()) return false;
|
---|
101 | if (!super.equals(obj)) return false;
|
---|
102 | ChangeCommand that = (ChangeCommand) obj;
|
---|
103 | return Objects.equals(osm, that.osm) &&
|
---|
104 | Objects.equals(newOsm, that.newOsm);
|
---|
105 | }
|
---|
106 | }
|
---|