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