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

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

see #13036 - deprecate Command() default constructor, fix unit tests and java warnings

  • Property svn:eol-style set to native
File size: 4.0 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 * @deprecated to be removed end of 2017. Use {@link #AddCommand(DataSet, OsmPrimitive)} instead
39 */
40 @Deprecated
41 public AddCommand(OsmPrimitive osm) {
42 this.osm = Objects.requireNonNull(osm, "osm");
43 }
44
45 /**
46 * Creates the command and specify the element to add in the context of the given data layer.
47 * @param layer The data layer. Must not be {@code null}
48 * @param osm The primitive to add
49 * @deprecated to be removed end of 2017. Use {@link #AddCommand(DataSet, OsmPrimitive)} instead
50 */
51 @Deprecated
52 public AddCommand(OsmDataLayer layer, OsmPrimitive osm) {
53 super(layer);
54 this.osm = Objects.requireNonNull(osm, "osm");
55 }
56
57 /**
58 * Creates the command and specify the element to add in the context of the given data set.
59 * @param data The data set. Must not be {@code null}
60 * @param osm The primitive to add
61 * @since 11240
62 */
63 public AddCommand(DataSet data, OsmPrimitive osm) {
64 super(data);
65 this.osm = Objects.requireNonNull(osm, "osm");
66 }
67
68 protected static final void checkNodeStyles(OsmPrimitive osm) {
69 if (osm instanceof Way) {
70 // Fix #10557 - node icon not updated after undoing/redoing addition of a way
71 ((Way) osm).clearCachedNodeStyles();
72 }
73 }
74
75 @Override
76 public boolean executeCommand() {
77 getAffectedDataSet().addPrimitive(osm);
78 osm.setModified(true);
79 checkNodeStyles(osm);
80 return true;
81 }
82
83 @Override
84 public void undoCommand() {
85 getAffectedDataSet().removePrimitive(osm);
86 checkNodeStyles(osm);
87 }
88
89 @Override
90 public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
91 added.add(osm);
92 }
93
94 @Override
95 public String getDescriptionText() {
96 String msg;
97 switch(OsmPrimitiveType.from(osm)) {
98 case NODE: msg = marktr("Add node {0}"); break;
99 case WAY: msg = marktr("Add way {0}"); break;
100 case RELATION: msg = marktr("Add relation {0}"); break;
101 default: /* should not happen */msg = ""; break;
102 }
103 return tr(msg, osm.getDisplayName(DefaultNameFormatter.getInstance()));
104 }
105
106 @Override
107 public Icon getDescriptionIcon() {
108 return ImageProvider.get(osm.getDisplayType());
109 }
110
111 @Override
112 public Collection<OsmPrimitive> getParticipatingPrimitives() {
113 return Collections.singleton(osm);
114 }
115
116 @Override
117 public int hashCode() {
118 return Objects.hash(super.hashCode(), osm);
119 }
120
121 @Override
122 public boolean equals(Object obj) {
123 if (this == obj) return true;
124 if (obj == null || getClass() != obj.getClass()) return false;
125 if (!super.equals(obj)) return false;
126 AddCommand that = (AddCommand) obj;
127 return Objects.equals(osm, that.osm);
128 }
129}
Note: See TracBrowser for help on using the repository browser.