source: josm/trunk/src/org/openstreetmap/josm/command/AddPrimitivesCommand.java@ 8456

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

see #11508 - override hashCode() and equals() in other commands as well

  • Property svn:eol-style set to native
File size: 7.7 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.command;
3
4import static org.openstreetmap.josm.tools.I18n.trn;
5
6import java.util.ArrayList;
7import java.util.Collection;
8import java.util.HashSet;
9import java.util.List;
10
11import javax.swing.Icon;
12
13import org.openstreetmap.josm.data.osm.DataSet;
14import org.openstreetmap.josm.data.osm.Node;
15import org.openstreetmap.josm.data.osm.NodeData;
16import org.openstreetmap.josm.data.osm.OsmPrimitive;
17import org.openstreetmap.josm.data.osm.PrimitiveData;
18import org.openstreetmap.josm.gui.layer.OsmDataLayer;
19import org.openstreetmap.josm.tools.CheckParameterUtil;
20
21/**
22 * Add primitives to a data layer.
23 * @since 2305
24 */
25public class AddPrimitivesCommand extends Command {
26
27 private List<PrimitiveData> data = new ArrayList<>();
28 private Collection<PrimitiveData> toSelect = new ArrayList<>();
29
30 // only filled on undo
31 private List<OsmPrimitive> createdPrimitives = null;
32 private Collection<OsmPrimitive> createdPrimitivesToSelect = null;
33
34 /**
35 * Constructs a new {@code AddPrimitivesCommand} to add data to the current edit layer.
36 * @param data The OSM primitives data to add. Must not be {@code null}
37 */
38 public AddPrimitivesCommand(List<PrimitiveData> data) {
39 this(data, data);
40 }
41
42 /**
43 * Constructs a new {@code AddPrimitivesCommand} to add data to the current edit layer.
44 * @param data The OSM primitives to add. Must not be {@code null}
45 * @param toSelect The OSM primitives to select at the end. Can be {@code null}
46 * @since 5953
47 */
48 public AddPrimitivesCommand(List<PrimitiveData> data, List<PrimitiveData> toSelect) {
49 init(data, toSelect);
50 }
51
52 /**
53 * Constructs a new {@code AddPrimitivesCommand} to add data to the given layer.
54 * @param data The OSM primitives data to add. Must not be {@code null}
55 * @param toSelect The OSM primitives to select at the end. Can be {@code null}
56 * @param layer The target data layer. Must not be {@code null}
57 */
58 public AddPrimitivesCommand(List<PrimitiveData> data, List<PrimitiveData> toSelect, OsmDataLayer layer) {
59 super(layer);
60 init(data, toSelect);
61 }
62
63 private final void init(List<PrimitiveData> data, List<PrimitiveData> toSelect) {
64 CheckParameterUtil.ensureParameterNotNull(data, "data");
65 this.data.addAll(data);
66 if (toSelect != null) {
67 this.toSelect.addAll(toSelect);
68 }
69 }
70
71 @Override public boolean executeCommand() {
72 Collection<OsmPrimitive> primitivesToSelect;
73 if (createdPrimitives == null) { // first time execution
74 List<OsmPrimitive> newPrimitives = new ArrayList<>(data.size());
75 primitivesToSelect = new ArrayList<>(toSelect.size());
76
77 for (PrimitiveData pd : data) {
78 OsmPrimitive primitive = getLayer().data.getPrimitiveById(pd);
79 boolean created = primitive == null;
80 if (created) {
81 primitive = pd.getType().newInstance(pd.getUniqueId(), true);
82 }
83 if (pd instanceof NodeData) { // Load nodes immediately because they can't be added to dataset without coordinates
84 primitive.load(pd);
85 }
86 if (created) {
87 getLayer().data.addPrimitive(primitive);
88 }
89 newPrimitives.add(primitive);
90 if (toSelect.contains(pd)) {
91 primitivesToSelect.add(primitive);
92 }
93 }
94
95 // Then load ways and relations
96 for (int i=0; i<newPrimitives.size(); i++) {
97 if (!(newPrimitives.get(i) instanceof Node)) {
98 newPrimitives.get(i).load(data.get(i));
99 }
100 }
101 } else { // redo
102 // When redoing this command, we have to add the same objects, otherwise
103 // a subsequent command (e.g. MoveCommand) cannot be redone.
104 for (OsmPrimitive osm : createdPrimitives) {
105 getLayer().data.addPrimitive(osm);
106 }
107 primitivesToSelect = createdPrimitivesToSelect;
108 }
109
110 getLayer().data.setSelected(primitivesToSelect);
111 return true;
112 }
113
114 @Override public void undoCommand() {
115 DataSet ds = getLayer().data;
116
117 if (createdPrimitives == null) {
118 createdPrimitives = new ArrayList<>(data.size());
119 createdPrimitivesToSelect = new ArrayList<>(toSelect.size());
120
121 for (PrimitiveData pd : data) {
122 OsmPrimitive p = ds.getPrimitiveById(pd);
123 createdPrimitives.add(p);
124 if (toSelect.contains(pd)) {
125 createdPrimitivesToSelect.add(p);
126 }
127 }
128 createdPrimitives = PurgeCommand.topoSort(createdPrimitives);
129
130 for (PrimitiveData p : data) {
131 ds.removePrimitive(p);
132 }
133 data = null;
134 toSelect = null;
135
136 } else {
137 for (OsmPrimitive osm : createdPrimitives) {
138 ds.removePrimitive(osm);
139 }
140 }
141 }
142
143 @Override
144 public String getDescriptionText() {
145 int size = data != null ? data.size() : createdPrimitives.size();
146 return trn("Added {0} object", "Added {0} objects", size, size);
147 }
148
149 @Override
150 public Icon getDescriptionIcon() {
151 return null;
152 }
153
154 @Override
155 public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted,
156 Collection<OsmPrimitive> added) {
157 // Does nothing because we don't want to create OsmPrimitives.
158 }
159
160 @Override
161 public Collection<? extends OsmPrimitive> getParticipatingPrimitives() {
162 if (createdPrimitives != null)
163 return createdPrimitives;
164
165 Collection<OsmPrimitive> prims = new HashSet<>();
166 for (PrimitiveData d : data) {
167 OsmPrimitive osm = getLayer().data.getPrimitiveById(d);
168 if (osm == null)
169 throw new RuntimeException();
170 prims.add(osm);
171 }
172 return prims;
173 }
174
175 @Override
176 public int hashCode() {
177 final int prime = 31;
178 int result = super.hashCode();
179 result = prime * result + ((createdPrimitives == null) ? 0 : createdPrimitives.hashCode());
180 result = prime * result + ((createdPrimitivesToSelect == null) ? 0 : createdPrimitivesToSelect.hashCode());
181 result = prime * result + ((data == null) ? 0 : data.hashCode());
182 result = prime * result + ((toSelect == null) ? 0 : toSelect.hashCode());
183 return result;
184 }
185
186 @Override
187 public boolean equals(Object obj) {
188 if (this == obj)
189 return true;
190 if (!super.equals(obj))
191 return false;
192 if (getClass() != obj.getClass())
193 return false;
194 AddPrimitivesCommand other = (AddPrimitivesCommand) obj;
195 if (createdPrimitives == null) {
196 if (other.createdPrimitives != null)
197 return false;
198 } else if (!createdPrimitives.equals(other.createdPrimitives))
199 return false;
200 if (createdPrimitivesToSelect == null) {
201 if (other.createdPrimitivesToSelect != null)
202 return false;
203 } else if (!createdPrimitivesToSelect.equals(other.createdPrimitivesToSelect))
204 return false;
205 if (data == null) {
206 if (other.data != null)
207 return false;
208 } else if (!data.equals(other.data))
209 return false;
210 if (toSelect == null) {
211 if (other.toSelect != null)
212 return false;
213 } else if (!toSelect.equals(other.toSelect))
214 return false;
215 return true;
216 }
217}
Note: See TracBrowser for help on using the repository browser.