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 |
|
---|
9 | import javax.swing.Icon;
|
---|
10 |
|
---|
11 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
12 | import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
|
---|
13 | import org.openstreetmap.josm.data.osm.Way;
|
---|
14 | import org.openstreetmap.josm.gui.DefaultNameFormatter;
|
---|
15 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
16 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
17 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
18 |
|
---|
19 | /**
|
---|
20 | * Command that basically replaces one OSM primitive by another of the same type.
|
---|
21 | *
|
---|
22 | * @since 93
|
---|
23 | */
|
---|
24 | public class ChangeCommand extends Command {
|
---|
25 |
|
---|
26 | private final OsmPrimitive osm;
|
---|
27 | private final OsmPrimitive newOsm;
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * Constructs a new {@code ChangeCommand} in the context of the current edit layer, if any.
|
---|
31 | * @param osm The existing primitive to modify
|
---|
32 | * @param newOsm The new primitive
|
---|
33 | */
|
---|
34 | public ChangeCommand(OsmPrimitive osm, OsmPrimitive newOsm) {
|
---|
35 | this.osm = osm;
|
---|
36 | this.newOsm = newOsm;
|
---|
37 | sanityChecks();
|
---|
38 | }
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * Constructs a new {@code ChangeCommand} in the context of a given data layer.
|
---|
42 | * @param layer The data layer
|
---|
43 | * @param osm The existing primitive to modify
|
---|
44 | * @param newOsm The new primitive
|
---|
45 | */
|
---|
46 | public ChangeCommand(OsmDataLayer layer, OsmPrimitive osm, OsmPrimitive newOsm) {
|
---|
47 | super(layer);
|
---|
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 | }
|
---|
83 | return tr(msg, osm.getDisplayName(DefaultNameFormatter.getInstance()));
|
---|
84 | }
|
---|
85 |
|
---|
86 | @Override
|
---|
87 | public Icon getDescriptionIcon() {
|
---|
88 | return ImageProvider.get(osm.getDisplayType());
|
---|
89 | }
|
---|
90 | }
|
---|