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

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

see #15182 - move NameFormatter* from gui to data.osm

  • Property svn:eol-style set to native
File size: 3.8 KB
RevLine 
[6380]1// License: GPL. For details, see LICENSE file.
[343]2package org.openstreetmap.josm.command;
3
[1989]4import static org.openstreetmap.josm.tools.I18n.marktr;
[343]5import static org.openstreetmap.josm.tools.I18n.tr;
6
7import java.util.Collection;
[9371]8import java.util.Objects;
[5077]9
[4918]10import javax.swing.Icon;
[343]11
[11240]12import org.openstreetmap.josm.data.osm.DataSet;
[12663]13import org.openstreetmap.josm.data.osm.DefaultNameFormatter;
[343]14import org.openstreetmap.josm.data.osm.OsmPrimitive;
[1814]15import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
[6302]16import org.openstreetmap.josm.data.osm.Way;
[3152]17import org.openstreetmap.josm.gui.layer.OsmDataLayer;
[6302]18import org.openstreetmap.josm.tools.CheckParameterUtil;
[1814]19import org.openstreetmap.josm.tools.ImageProvider;
[343]20
21/**
[6365]22 * Command that basically replaces one OSM primitive by another of the same type.
[1169]23 *
[6365]24 * @since 93
[343]25 */
26public class ChangeCommand extends Command {
27
[1169]28 private final OsmPrimitive osm;
29 private final OsmPrimitive newOsm;
[343]30
[6881]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 */
[1169]36 public ChangeCommand(OsmPrimitive osm, OsmPrimitive newOsm) {
37 this.osm = osm;
38 this.newOsm = newOsm;
[6302]39 sanityChecks();
[343]40 }
41
[6881]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 */
[3152]48 public ChangeCommand(OsmDataLayer layer, OsmPrimitive osm, OsmPrimitive newOsm) {
49 super(layer);
50 this.osm = osm;
51 this.newOsm = newOsm;
[6302]52 sanityChecks();
[3152]53 }
[6881]54
[11240]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
[6302]69 private void sanityChecks() {
70 CheckParameterUtil.ensureParameterNotNull(osm, "osm");
71 CheckParameterUtil.ensureParameterNotNull(newOsm, "newOsm");
[8510]72 if (newOsm instanceof Way && ((Way) newOsm).getNodesCount() == 0) {
[6302]73 // Do not allow to create empty ways (see #7465)
[6365]74 throw new IllegalArgumentException(tr("New way {0} has 0 nodes", newOsm));
[6302]75 }
76 }
[3152]77
[6365]78 @Override
79 public boolean executeCommand() {
[1169]80 super.executeCommand();
81 osm.cloneFrom(newOsm);
[2025]82 osm.setModified(true);
[1169]83 return true;
[343]84 }
85
[6365]86 @Override
87 public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
[1169]88 modified.add(osm);
[343]89 }
90
[4918]91 @Override
92 public String getDescriptionText() {
[10216]93 String msg;
[1989]94 switch(OsmPrimitiveType.from(osm)) {
[3152]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;
[10216]98 default: throw new AssertionError();
[1989]99 }
[4918]100 return tr(msg, osm.getDisplayName(DefaultNameFormatter.getInstance()));
[343]101 }
[4918]102
103 @Override
104 public Icon getDescriptionIcon() {
[5077]105 return ImageProvider.get(osm.getDisplayType());
[4918]106 }
[8456]107
108 @Override
109 public int hashCode() {
[9371]110 return Objects.hash(super.hashCode(), osm, newOsm);
[8456]111 }
112
113 @Override
114 public boolean equals(Object obj) {
[9371]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);
[8456]121 }
[343]122}
Note: See TracBrowser for help on using the repository browser.