source: josm/trunk/src/org/openstreetmap/josm/command/AddCommand.java@ 12711

Last change on this file since 12711 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
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.Collections;
9import java.util.Objects;
10
11import javax.swing.Icon;
12
13import org.openstreetmap.josm.data.osm.DataSet;
14import org.openstreetmap.josm.data.osm.DefaultNameFormatter;
15import org.openstreetmap.josm.data.osm.OsmPrimitive;
16import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
17import org.openstreetmap.josm.data.osm.Way;
18import org.openstreetmap.josm.gui.layer.OsmDataLayer;
19import org.openstreetmap.josm.tools.ImageProvider;
20
21/**
22 * A command that adds an osm primitive to a dataset. Keys cannot be added this way.
23 *
24 * See {@link ChangeCommand} for comments on relation back references.
25 *
26 * @author imi
27 */
28public class AddCommand extends Command {
29
30 /**
31 * The primitive to add to the dataset.
32 */
33 private final OsmPrimitive osm;
34
35 /**
36 * Creates the command and specify the element to add in the context of the current edit layer, if any.
37 * @param osm The primitive to add
38 */
39 public AddCommand(OsmPrimitive osm) {
40 this.osm = Objects.requireNonNull(osm, "osm");
41 }
42
43 /**
44 * Creates the command and specify the element to add in the context of the given data layer.
45 * @param layer The data layer. Must not be {@code null}
46 * @param osm The primitive to add
47 */
48 public AddCommand(OsmDataLayer layer, OsmPrimitive osm) {
49 super(layer);
50 this.osm = Objects.requireNonNull(osm, "osm");
51 }
52
53 /**
54 * Creates the command and specify the element to add in the context of the given data set.
55 * @param data The data set. Must not be {@code null}
56 * @param osm The primitive to add
57 * @since 11240
58 */
59 public AddCommand(DataSet data, OsmPrimitive osm) {
60 super(data);
61 this.osm = Objects.requireNonNull(osm, "osm");
62 }
63
64 protected static final void checkNodeStyles(OsmPrimitive osm) {
65 if (osm instanceof Way) {
66 // Fix #10557 - node icon not updated after undoing/redoing addition of a way
67 ((Way) osm).clearCachedNodeStyles();
68 }
69 }
70
71 @Override
72 public boolean executeCommand() {
73 getAffectedDataSet().addPrimitive(osm);
74 osm.setModified(true);
75 checkNodeStyles(osm);
76 return true;
77 }
78
79 @Override
80 public void undoCommand() {
81 getAffectedDataSet().removePrimitive(osm);
82 checkNodeStyles(osm);
83 }
84
85 @Override
86 public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
87 added.add(osm);
88 }
89
90 @Override
91 public String getDescriptionText() {
92 String msg;
93 switch(OsmPrimitiveType.from(osm)) {
94 case NODE: msg = marktr("Add node {0}"); break;
95 case WAY: msg = marktr("Add way {0}"); break;
96 case RELATION: msg = marktr("Add relation {0}"); break;
97 default: /* should not happen */msg = ""; break;
98 }
99 return tr(msg, osm.getDisplayName(DefaultNameFormatter.getInstance()));
100 }
101
102 @Override
103 public Icon getDescriptionIcon() {
104 return ImageProvider.get(osm.getDisplayType());
105 }
106
107 @Override
108 public Collection<OsmPrimitive> getParticipatingPrimitives() {
109 return Collections.singleton(osm);
110 }
111
112 @Override
113 public int hashCode() {
114 return Objects.hash(super.hashCode(), osm);
115 }
116
117 @Override
118 public boolean equals(Object obj) {
119 if (this == obj) return true;
120 if (obj == null || getClass() != obj.getClass()) return false;
121 if (!super.equals(obj)) return false;
122 AddCommand that = (AddCommand) obj;
123 return Objects.equals(osm, that.osm);
124 }
125}
Note: See TracBrowser for help on using the repository browser.