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

Last change on this file since 12466 was 11240, checked in by Don-vip, 7 years ago

see #10387 - refactor various actions and commands so they can be used without data layer

  • Property svn:eol-style set to native
File size: 3.8 KB
Line 
1// License: GPL. For details, see LICENSE file.
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;
8import java.util.Objects;
9
10import javax.swing.Icon;
11
12import org.openstreetmap.josm.data.osm.DataSet;
13import org.openstreetmap.josm.data.osm.OsmPrimitive;
14import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
15import org.openstreetmap.josm.data.osm.Way;
16import org.openstreetmap.josm.gui.DefaultNameFormatter;
17import org.openstreetmap.josm.gui.layer.OsmDataLayer;
18import org.openstreetmap.josm.tools.CheckParameterUtil;
19import org.openstreetmap.josm.tools.ImageProvider;
20
21/**
22 * Command that basically replaces one OSM primitive by another of the same type.
23 *
24 * @since 93
25 */
26public class ChangeCommand extends Command {
27
28 private final OsmPrimitive osm;
29 private final OsmPrimitive newOsm;
30
31 /**
32 * Constructs a new {@code ChangeCommand} in the context of the current edit layer, if any.
33 * @param osm The existing primitive to modify
34 * @param newOsm The new primitive
35 */
36 public ChangeCommand(OsmPrimitive osm, OsmPrimitive newOsm) {
37 this.osm = osm;
38 this.newOsm = newOsm;
39 sanityChecks();
40 }
41
42 /**
43 * Constructs a new {@code ChangeCommand} in the context of a given data layer.
44 * @param layer The data layer
45 * @param osm The existing primitive to modify
46 * @param newOsm The new primitive
47 */
48 public ChangeCommand(OsmDataLayer layer, OsmPrimitive osm, OsmPrimitive newOsm) {
49 super(layer);
50 this.osm = osm;
51 this.newOsm = newOsm;
52 sanityChecks();
53 }
54
55 /**
56 * Constructs a new {@code ChangeCommand} in the context of a given data set.
57 * @param data The data set
58 * @param osm The existing primitive to modify
59 * @param newOsm The new primitive
60 * @since 11240
61 */
62 public ChangeCommand(DataSet data, OsmPrimitive osm, OsmPrimitive newOsm) {
63 super(data);
64 this.osm = osm;
65 this.newOsm = newOsm;
66 sanityChecks();
67 }
68
69 private void sanityChecks() {
70 CheckParameterUtil.ensureParameterNotNull(osm, "osm");
71 CheckParameterUtil.ensureParameterNotNull(newOsm, "newOsm");
72 if (newOsm instanceof Way && ((Way) newOsm).getNodesCount() == 0) {
73 // Do not allow to create empty ways (see #7465)
74 throw new IllegalArgumentException(tr("New way {0} has 0 nodes", newOsm));
75 }
76 }
77
78 @Override
79 public boolean executeCommand() {
80 super.executeCommand();
81 osm.cloneFrom(newOsm);
82 osm.setModified(true);
83 return true;
84 }
85
86 @Override
87 public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
88 modified.add(osm);
89 }
90
91 @Override
92 public String getDescriptionText() {
93 String msg;
94 switch(OsmPrimitiveType.from(osm)) {
95 case NODE: msg = marktr("Change node {0}"); break;
96 case WAY: msg = marktr("Change way {0}"); break;
97 case RELATION: msg = marktr("Change relation {0}"); break;
98 default: throw new AssertionError();
99 }
100 return tr(msg, osm.getDisplayName(DefaultNameFormatter.getInstance()));
101 }
102
103 @Override
104 public Icon getDescriptionIcon() {
105 return ImageProvider.get(osm.getDisplayType());
106 }
107
108 @Override
109 public int hashCode() {
110 return Objects.hash(super.hashCode(), osm, newOsm);
111 }
112
113 @Override
114 public boolean equals(Object obj) {
115 if (this == obj) return true;
116 if (obj == null || getClass() != obj.getClass()) return false;
117 if (!super.equals(obj)) return false;
118 ChangeCommand that = (ChangeCommand) obj;
119 return Objects.equals(osm, that.osm) &&
120 Objects.equals(newOsm, that.newOsm);
121 }
122}
Note: See TracBrowser for help on using the repository browser.