source: josm/trunk/src/org/openstreetmap/josm/command/ChangeCommand.java@ 6324

Last change on this file since 6324 was 6302, checked in by Don-vip, 11 years ago

see #7465 - Raise an error when a plugin replaces a way with a zero-node way (such as reverter before v30000)

  • Property svn:eol-style set to native
File size: 2.5 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.command;
3
4import static org.openstreetmap.josm.tools.I18n.marktr;
5import static org.openstreetmap.josm.tools.I18n.tr;
6
7import java.util.Collection;
8
9import javax.swing.Icon;
10
11import org.openstreetmap.josm.data.osm.OsmPrimitive;
12import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
13import org.openstreetmap.josm.data.osm.Way;
14import org.openstreetmap.josm.gui.DefaultNameFormatter;
15import org.openstreetmap.josm.gui.layer.OsmDataLayer;
16import org.openstreetmap.josm.tools.CheckParameterUtil;
17import org.openstreetmap.josm.tools.ImageProvider;
18
19/**
20 * Command that basically replaces one OSM primitive by another of the
21 * same type.
22 *
23 * @author Imi
24 */
25public class ChangeCommand extends Command {
26
27 private final OsmPrimitive osm;
28 private final OsmPrimitive newOsm;
29
30 public ChangeCommand(OsmPrimitive osm, OsmPrimitive newOsm) {
31 super();
32 this.osm = osm;
33 this.newOsm = newOsm;
34 sanityChecks();
35 }
36
37 public ChangeCommand(OsmDataLayer layer, OsmPrimitive osm, OsmPrimitive newOsm) {
38 super(layer);
39 this.osm = osm;
40 this.newOsm = newOsm;
41 sanityChecks();
42 }
43
44 private void sanityChecks() {
45 CheckParameterUtil.ensureParameterNotNull(osm, "osm");
46 CheckParameterUtil.ensureParameterNotNull(newOsm, "newOsm");
47 if (newOsm instanceof Way) {
48 // Do not allow to create empty ways (see #7465)
49 if (((Way)newOsm).getNodesCount() == 0) {
50 throw new IllegalArgumentException(tr("New way {0} has 0 nodes", newOsm));
51 }
52 }
53 }
54
55 @Override public boolean executeCommand() {
56 super.executeCommand();
57 osm.cloneFrom(newOsm);
58 osm.setModified(true);
59 return true;
60 }
61
62 @Override public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
63 modified.add(osm);
64 }
65
66 @Override
67 public String getDescriptionText() {
68 String msg = "";
69 switch(OsmPrimitiveType.from(osm)) {
70 case NODE: msg = marktr("Change node {0}"); break;
71 case WAY: msg = marktr("Change way {0}"); break;
72 case RELATION: msg = marktr("Change relation {0}"); break;
73 }
74 return tr(msg, osm.getDisplayName(DefaultNameFormatter.getInstance()));
75 }
76
77 @Override
78 public Icon getDescriptionIcon() {
79 return ImageProvider.get(osm.getDisplayType());
80 }
81}
Note: See TracBrowser for help on using the repository browser.