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 | public ChangeCommand(OsmPrimitive osm, OsmPrimitive newOsm) {
|
---|
30 | super();
|
---|
31 | this.osm = osm;
|
---|
32 | this.newOsm = newOsm;
|
---|
33 | sanityChecks();
|
---|
34 | }
|
---|
35 |
|
---|
36 | public ChangeCommand(OsmDataLayer layer, OsmPrimitive osm, OsmPrimitive newOsm) {
|
---|
37 | super(layer);
|
---|
38 | this.osm = osm;
|
---|
39 | this.newOsm = newOsm;
|
---|
40 | sanityChecks();
|
---|
41 | }
|
---|
42 |
|
---|
43 | private void sanityChecks() {
|
---|
44 | CheckParameterUtil.ensureParameterNotNull(osm, "osm");
|
---|
45 | CheckParameterUtil.ensureParameterNotNull(newOsm, "newOsm");
|
---|
46 | if (newOsm instanceof Way && ((Way)newOsm).getNodesCount() == 0) {
|
---|
47 | // Do not allow to create empty ways (see #7465)
|
---|
48 | throw new IllegalArgumentException(tr("New way {0} has 0 nodes", newOsm));
|
---|
49 | }
|
---|
50 | }
|
---|
51 |
|
---|
52 | @Override
|
---|
53 | public boolean executeCommand() {
|
---|
54 | super.executeCommand();
|
---|
55 | osm.cloneFrom(newOsm);
|
---|
56 | osm.setModified(true);
|
---|
57 | return true;
|
---|
58 | }
|
---|
59 |
|
---|
60 | @Override
|
---|
61 | public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
|
---|
62 | modified.add(osm);
|
---|
63 | }
|
---|
64 |
|
---|
65 | @Override
|
---|
66 | public String getDescriptionText() {
|
---|
67 | String msg = "";
|
---|
68 | switch(OsmPrimitiveType.from(osm)) {
|
---|
69 | case NODE: msg = marktr("Change node {0}"); break;
|
---|
70 | case WAY: msg = marktr("Change way {0}"); break;
|
---|
71 | case RELATION: msg = marktr("Change relation {0}"); break;
|
---|
72 | }
|
---|
73 | return tr(msg, osm.getDisplayName(DefaultNameFormatter.getInstance()));
|
---|
74 | }
|
---|
75 |
|
---|
76 | @Override
|
---|
77 | public Icon getDescriptionIcon() {
|
---|
78 | return ImageProvider.get(osm.getDisplayType());
|
---|
79 | }
|
---|
80 | }
|
---|