1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.data.osm;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.geom.Area;
|
---|
7 | import java.util.ArrayList;
|
---|
8 | import java.util.Arrays;
|
---|
9 | import java.util.Collection;
|
---|
10 | import java.util.Collections;
|
---|
11 | import java.util.Comparator;
|
---|
12 | import java.util.HashMap;
|
---|
13 | import java.util.HashSet;
|
---|
14 | import java.util.Iterator;
|
---|
15 | import java.util.LinkedHashSet;
|
---|
16 | import java.util.LinkedList;
|
---|
17 | import java.util.List;
|
---|
18 | import java.util.Map;
|
---|
19 | import java.util.Set;
|
---|
20 |
|
---|
21 | import org.openstreetmap.josm.data.SelectionChangedListener;
|
---|
22 | import org.openstreetmap.josm.data.osm.event.AbstractDatasetChangedEvent;
|
---|
23 | import org.openstreetmap.josm.data.osm.event.ChangesetIdChangedEvent;
|
---|
24 | import org.openstreetmap.josm.data.osm.event.DataChangedEvent;
|
---|
25 | import org.openstreetmap.josm.data.osm.event.DataSetListener;
|
---|
26 | import org.openstreetmap.josm.data.osm.event.NodeMovedEvent;
|
---|
27 | import org.openstreetmap.josm.data.osm.event.PrimitivesAddedEvent;
|
---|
28 | import org.openstreetmap.josm.data.osm.event.PrimitivesRemovedEvent;
|
---|
29 | import org.openstreetmap.josm.data.osm.event.RelationMembersChangedEvent;
|
---|
30 | import org.openstreetmap.josm.data.osm.event.TagsChangedEvent;
|
---|
31 | import org.openstreetmap.josm.data.osm.event.WayNodesChangedEvent;
|
---|
32 |
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * DataSet is the data behind the application. It can consists of only a few points up to the whole
|
---|
36 | * osm database. DataSet's can be merged together, saved, (up/down/disk)loaded etc.
|
---|
37 | *
|
---|
38 | * Note that DataSet is not an osm-primitive and so has no key association but a few members to
|
---|
39 | * store some information.
|
---|
40 | *
|
---|
41 | * @author imi
|
---|
42 | */
|
---|
43 | public class DataSet implements Cloneable {
|
---|
44 |
|
---|
45 | private static class IdHash implements Hash<PrimitiveId,OsmPrimitive> {
|
---|
46 |
|
---|
47 | public int getHashCode(PrimitiveId k) {
|
---|
48 | return (int)k.getUniqueId() ^ k.getType().hashCode();
|
---|
49 | }
|
---|
50 |
|
---|
51 | public boolean equals(PrimitiveId key, OsmPrimitive value) {
|
---|
52 | if (key == null || value == null) return false;
|
---|
53 | return key.getUniqueId() == value.getUniqueId() && key.getType() == value.getType();
|
---|
54 | }
|
---|
55 | }
|
---|
56 |
|
---|
57 | private Storage<OsmPrimitive> allPrimitives = new Storage<OsmPrimitive>(new IdHash());
|
---|
58 | private Map<PrimitiveId, OsmPrimitive> primitivesMap = allPrimitives.foreignKey(new IdHash());
|
---|
59 | private List<DataSetListener> listeners = new ArrayList<DataSetListener>();
|
---|
60 | // Number of open calls to beginUpdate
|
---|
61 | private int updateCount;
|
---|
62 |
|
---|
63 | /**
|
---|
64 | * The API version that created this data set, if any.
|
---|
65 | */
|
---|
66 | private String version;
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * Replies the API version this dataset was created from. May be null.
|
---|
70 | *
|
---|
71 | * @return the API version this dataset was created from. May be null.
|
---|
72 | */
|
---|
73 | public String getVersion() {
|
---|
74 | return version;
|
---|
75 | }
|
---|
76 |
|
---|
77 | /**
|
---|
78 | * Sets the API version this dataset was created from.
|
---|
79 | *
|
---|
80 | * @param version the API version, i.e. "0.5" or "0.6"
|
---|
81 | */
|
---|
82 | public void setVersion(String version) {
|
---|
83 | this.version = version;
|
---|
84 | }
|
---|
85 |
|
---|
86 | /**
|
---|
87 | * All nodes goes here, even when included in other data (ways etc). This enables the instant
|
---|
88 | * conversion of the whole DataSet by iterating over this data structure.
|
---|
89 | */
|
---|
90 | private QuadBuckets<Node> nodes = new QuadBuckets<Node>();
|
---|
91 |
|
---|
92 | /**
|
---|
93 | * Replies an unmodifiable collection of nodes in this dataset
|
---|
94 | *
|
---|
95 | * @return an unmodifiable collection of nodes in this dataset
|
---|
96 | */
|
---|
97 | public Collection<Node> getNodes() {
|
---|
98 | return Collections.unmodifiableCollection(nodes);
|
---|
99 | }
|
---|
100 |
|
---|
101 | public List<Node> searchNodes(BBox bbox) {
|
---|
102 | return nodes.search(bbox);
|
---|
103 | }
|
---|
104 |
|
---|
105 | /**
|
---|
106 | * All ways (Streets etc.) in the DataSet.
|
---|
107 | *
|
---|
108 | * The way nodes are stored only in the way list.
|
---|
109 | */
|
---|
110 | private QuadBuckets<Way> ways = new QuadBuckets<Way>();
|
---|
111 |
|
---|
112 | /**
|
---|
113 | * Replies an unmodifiable collection of ways in this dataset
|
---|
114 | *
|
---|
115 | * @return an unmodifiable collection of ways in this dataset
|
---|
116 | */
|
---|
117 | public Collection<Way> getWays() {
|
---|
118 | return Collections.unmodifiableCollection(ways);
|
---|
119 | }
|
---|
120 |
|
---|
121 | public List<Way> searchWays(BBox bbox) {
|
---|
122 | return ways.search(bbox);
|
---|
123 | }
|
---|
124 |
|
---|
125 | /**
|
---|
126 | * All relations/relationships
|
---|
127 | */
|
---|
128 | private Collection<Relation> relations = new LinkedList<Relation>();
|
---|
129 |
|
---|
130 | /**
|
---|
131 | * Replies an unmodifiable collection of relations in this dataset
|
---|
132 | *
|
---|
133 | * @return an unmodifiable collection of relations in this dataset
|
---|
134 | */
|
---|
135 | public Collection<Relation> getRelations() {
|
---|
136 | return Collections.unmodifiableCollection(relations);
|
---|
137 | }
|
---|
138 |
|
---|
139 | /**
|
---|
140 | * All data sources of this DataSet.
|
---|
141 | */
|
---|
142 | public Collection<DataSource> dataSources = new LinkedList<DataSource>();
|
---|
143 |
|
---|
144 | /**
|
---|
145 | * @return A collection containing all primitives of the dataset. The data is ordered after:
|
---|
146 | * first come nodes, then ways, then relations. Ordering in between the categories is not
|
---|
147 | * guaranteed.
|
---|
148 | */
|
---|
149 | public List<OsmPrimitive> allPrimitives() {
|
---|
150 | List<OsmPrimitive> o = new LinkedList<OsmPrimitive>();
|
---|
151 | o.addAll(nodes);
|
---|
152 | o.addAll(ways);
|
---|
153 | o.addAll(relations);
|
---|
154 | return o;
|
---|
155 | }
|
---|
156 |
|
---|
157 | /**
|
---|
158 | * @return A collection containing all not-deleted primitives (except keys).
|
---|
159 | */
|
---|
160 | public Collection<OsmPrimitive> allNonDeletedPrimitives() {
|
---|
161 | Collection<OsmPrimitive> o = new LinkedList<OsmPrimitive>();
|
---|
162 | for (OsmPrimitive osm : allPrimitives())
|
---|
163 | if (osm.isVisible() && !osm.isDeleted()) {
|
---|
164 | o.add(osm);
|
---|
165 | }
|
---|
166 | return o;
|
---|
167 | }
|
---|
168 |
|
---|
169 | public Collection<OsmPrimitive> allNonDeletedCompletePrimitives() {
|
---|
170 | Collection<OsmPrimitive> o = new LinkedList<OsmPrimitive>();
|
---|
171 | for (OsmPrimitive osm : allPrimitives())
|
---|
172 | if (osm.isVisible() && !osm.isDeleted() && !osm.isIncomplete()) {
|
---|
173 | o.add(osm);
|
---|
174 | }
|
---|
175 | return o;
|
---|
176 | }
|
---|
177 |
|
---|
178 | public Collection<OsmPrimitive> allNonDeletedPhysicalPrimitives() {
|
---|
179 | Collection<OsmPrimitive> o = new LinkedList<OsmPrimitive>();
|
---|
180 | for (OsmPrimitive osm : allPrimitives())
|
---|
181 | if (osm.isVisible() && !osm.isDeleted() && !osm.isIncomplete() && !(osm instanceof Relation)) {
|
---|
182 | o.add(osm);
|
---|
183 | }
|
---|
184 | return o;
|
---|
185 | }
|
---|
186 |
|
---|
187 | /**
|
---|
188 | * Adds a primitive to the dataset
|
---|
189 | *
|
---|
190 | * @param primitive the primitive. Ignored if null.
|
---|
191 | */
|
---|
192 | public void addPrimitive(OsmPrimitive primitive) {
|
---|
193 | if (getPrimitiveById(primitive) != null)
|
---|
194 | throw new DataIntegrityProblemException(
|
---|
195 | tr("Unable to add primitive {0} to the dataset because it's already included", primitive.toString()));
|
---|
196 |
|
---|
197 | if (primitive instanceof Node) {
|
---|
198 | nodes.add((Node) primitive);
|
---|
199 | } else if (primitive instanceof Way) {
|
---|
200 | ways.add((Way) primitive);
|
---|
201 | } else if (primitive instanceof Relation) {
|
---|
202 | relations.add((Relation) primitive);
|
---|
203 | }
|
---|
204 | allPrimitives.add(primitive);
|
---|
205 | primitive.setDataset(this);
|
---|
206 | firePrimitivesAdded(Collections.singletonList(primitive), false);
|
---|
207 | }
|
---|
208 |
|
---|
209 | public OsmPrimitive addPrimitive(PrimitiveData data) {
|
---|
210 | OsmPrimitive result;
|
---|
211 | if (data instanceof NodeData) {
|
---|
212 | result = new Node();
|
---|
213 | } else if (data instanceof WayData) {
|
---|
214 | result = new Way();
|
---|
215 | } else if (data instanceof RelationData) {
|
---|
216 | result = new Relation();
|
---|
217 | } else
|
---|
218 | throw new AssertionError();
|
---|
219 | result.setDataset(this);
|
---|
220 | result.load(data);
|
---|
221 | addPrimitive(result);
|
---|
222 | return result;
|
---|
223 | }
|
---|
224 |
|
---|
225 | /**
|
---|
226 | * Removes a primitive from the dataset. This method only removes the
|
---|
227 | * primitive form the respective collection of primitives managed
|
---|
228 | * by this dataset, i.e. from {@see #nodes}, {@see #ways}, or
|
---|
229 | * {@see #relations}. References from other primitives to this
|
---|
230 | * primitive are left unchanged.
|
---|
231 | *
|
---|
232 | * @param primitive the primitive
|
---|
233 | */
|
---|
234 | public void removePrimitive(PrimitiveId primitiveId) {
|
---|
235 | OsmPrimitive primitive = getPrimitiveByIdChecked(primitiveId);
|
---|
236 | if (primitive == null)
|
---|
237 | return;
|
---|
238 | if (primitive instanceof Node) {
|
---|
239 | nodes.remove(primitive);
|
---|
240 | } else if (primitive instanceof Way) {
|
---|
241 | ways.remove(primitive);
|
---|
242 | } else if (primitive instanceof Relation) {
|
---|
243 | relations.remove(primitive);
|
---|
244 | }
|
---|
245 | selectedPrimitives.remove(primitive);
|
---|
246 | allPrimitives.remove(primitive);
|
---|
247 | primitive.setDataset(null);
|
---|
248 | errors.remove(primitive);
|
---|
249 | firePrimitivesRemoved(Collections.singletonList(primitive), false);
|
---|
250 | }
|
---|
251 |
|
---|
252 | /*---------------------------------------------------
|
---|
253 | * SELECTION HANDLING
|
---|
254 | *---------------------------------------------------*/
|
---|
255 |
|
---|
256 | /**
|
---|
257 | * A list of listeners to selection changed events. The list is static, as listeners register
|
---|
258 | * themselves for any dataset selection changes that occur, regardless of the current active
|
---|
259 | * dataset. (However, the selection does only change in the active layer)
|
---|
260 | */
|
---|
261 | public static Collection<SelectionChangedListener> selListeners =
|
---|
262 | Collections.synchronizedList(new LinkedList<SelectionChangedListener>());
|
---|
263 |
|
---|
264 | /**
|
---|
265 | * Notifies all registered {@see SelectionChangedListener} about the current selection in
|
---|
266 | * this dataset.
|
---|
267 | *
|
---|
268 | */
|
---|
269 | public void fireSelectionChanged(){
|
---|
270 | synchronized (selListeners) {
|
---|
271 | List<? extends OsmPrimitive> currentSelection = Collections.unmodifiableList(new ArrayList<OsmPrimitive>(selectedPrimitives));
|
---|
272 | for (SelectionChangedListener l : selListeners) {
|
---|
273 | l.selectionChanged(currentSelection);
|
---|
274 | }
|
---|
275 | }
|
---|
276 | }
|
---|
277 |
|
---|
278 | LinkedHashSet<OsmPrimitive> selectedPrimitives = new LinkedHashSet<OsmPrimitive>();
|
---|
279 |
|
---|
280 | public Collection<OsmPrimitive> getSelectedNodesAndWays() {
|
---|
281 | Collection<OsmPrimitive> sel = new LinkedList<OsmPrimitive>();
|
---|
282 | for (OsmPrimitive osm : selectedPrimitives) {
|
---|
283 | if (osm instanceof Way ||
|
---|
284 | osm instanceof Node) {
|
---|
285 | sel.add(osm);
|
---|
286 | }
|
---|
287 | }
|
---|
288 | return sel;
|
---|
289 | }
|
---|
290 |
|
---|
291 | /**
|
---|
292 | * Return a list of all selected objects. Even keys are returned.
|
---|
293 | * @return List of all selected objects.
|
---|
294 | */
|
---|
295 | public Collection<OsmPrimitive> getSelected() {
|
---|
296 | // It would be nice to have this be a copy-on-write list
|
---|
297 | // or an Collections.unmodifiableList(). It would be
|
---|
298 | // much faster for large selections. May users just
|
---|
299 | // call this, and only check the .size().
|
---|
300 | return new ArrayList<OsmPrimitive>(selectedPrimitives);
|
---|
301 | }
|
---|
302 |
|
---|
303 | /**
|
---|
304 | * Return selected nodes.
|
---|
305 | */
|
---|
306 | public Collection<Node> getSelectedNodes() {
|
---|
307 | List<Node> result = new ArrayList<Node>(selectedPrimitives.size());
|
---|
308 | for (OsmPrimitive primitive:selectedPrimitives) {
|
---|
309 | if (primitive instanceof Node) {
|
---|
310 | result.add((Node)primitive);
|
---|
311 | }
|
---|
312 | }
|
---|
313 | return result;
|
---|
314 | }
|
---|
315 |
|
---|
316 | /**
|
---|
317 | * Return selected ways.
|
---|
318 | */
|
---|
319 | public Collection<Way> getSelectedWays() {
|
---|
320 | List<Way> result = new ArrayList<Way>(selectedPrimitives.size());
|
---|
321 | for (OsmPrimitive primitive:selectedPrimitives) {
|
---|
322 | if (primitive instanceof Way) {
|
---|
323 | result.add((Way)primitive);
|
---|
324 | }
|
---|
325 | }
|
---|
326 | return result;
|
---|
327 | }
|
---|
328 |
|
---|
329 | /**
|
---|
330 | * Return selected relations.
|
---|
331 | */
|
---|
332 | public Collection<Relation> getSelectedRelations() {
|
---|
333 | List<Relation> result = new ArrayList<Relation>(selectedPrimitives.size() / 10);
|
---|
334 | for (OsmPrimitive primitive:selectedPrimitives) {
|
---|
335 | if (primitive instanceof Relation) {
|
---|
336 | result.add((Relation)primitive);
|
---|
337 | }
|
---|
338 | }
|
---|
339 | return result;
|
---|
340 | }
|
---|
341 |
|
---|
342 | public boolean isSelected(OsmPrimitive osm) {
|
---|
343 | return selectedPrimitives.contains(osm);
|
---|
344 | }
|
---|
345 |
|
---|
346 | public void toggleSelected(Collection<? extends PrimitiveId> osm) {
|
---|
347 | boolean changed = false;
|
---|
348 | for (PrimitiveId o : osm) {
|
---|
349 | changed = changed | this.__toggleSelected(o);
|
---|
350 | }
|
---|
351 | if (changed) {
|
---|
352 | fireSelectionChanged();
|
---|
353 | }
|
---|
354 | }
|
---|
355 | public void toggleSelected(PrimitiveId... osm) {
|
---|
356 | toggleSelected(Arrays.asList(osm));
|
---|
357 | }
|
---|
358 | private boolean __toggleSelected(PrimitiveId primitiveId) {
|
---|
359 | OsmPrimitive primitive = getPrimitiveByIdChecked(primitiveId);
|
---|
360 | if (primitive == null)
|
---|
361 | return false;
|
---|
362 | if (!selectedPrimitives.remove(primitive)) {
|
---|
363 | selectedPrimitives.add(primitive);
|
---|
364 | }
|
---|
365 | return true;
|
---|
366 | }
|
---|
367 |
|
---|
368 | /**
|
---|
369 | * Sets the current selection to the primitives in <code>selection</code>.
|
---|
370 | * Notifies all {@see SelectionChangedListener} if <code>fireSelectionChangeEvent</code> is true.
|
---|
371 | *
|
---|
372 | * @param selection the selection
|
---|
373 | * @param fireSelectionChangeEvent true, if the selection change listeners are to be notified; false, otherwise
|
---|
374 | */
|
---|
375 | public void setSelected(Collection<? extends PrimitiveId> selection, boolean fireSelectionChangeEvent) {
|
---|
376 | boolean wasEmpty = selectedPrimitives.isEmpty();
|
---|
377 | selectedPrimitives = new LinkedHashSet<OsmPrimitive>();
|
---|
378 | addSelected(selection, fireSelectionChangeEvent);
|
---|
379 | if (!wasEmpty && selectedPrimitives.isEmpty() && fireSelectionChangeEvent) {
|
---|
380 | fireSelectionChanged();
|
---|
381 | }
|
---|
382 | }
|
---|
383 |
|
---|
384 | /**
|
---|
385 | * Sets the current selection to the primitives in <code>selection</code>
|
---|
386 | * and notifies all {@see SelectionChangedListener}.
|
---|
387 | *
|
---|
388 | * @param selection the selection
|
---|
389 | */
|
---|
390 | public void setSelected(Collection<? extends PrimitiveId> selection) {
|
---|
391 | setSelected(selection, true /* fire selection change event */);
|
---|
392 | }
|
---|
393 |
|
---|
394 | public void setSelected(PrimitiveId... osm) {
|
---|
395 | if (osm.length == 1 && osm[0] == null) {
|
---|
396 | setSelected();
|
---|
397 | return;
|
---|
398 | }
|
---|
399 | List<PrimitiveId> list = Arrays.asList(osm);
|
---|
400 | setSelected(list);
|
---|
401 | }
|
---|
402 |
|
---|
403 | /**
|
---|
404 | * Adds the primitives in <code>selection</code> to the current selection
|
---|
405 | * and notifies all {@see SelectionChangedListener}.
|
---|
406 | *
|
---|
407 | * @param selection the selection
|
---|
408 | */
|
---|
409 | public void addSelected(Collection<? extends PrimitiveId> selection) {
|
---|
410 | addSelected(selection, true /* fire selection change event */);
|
---|
411 | }
|
---|
412 |
|
---|
413 | public void addSelected(PrimitiveId... osm) {
|
---|
414 | addSelected(Arrays.asList(osm));
|
---|
415 | }
|
---|
416 |
|
---|
417 | /**
|
---|
418 | * Adds the primitives in <code>selection</code> to the current selection.
|
---|
419 | * Notifies all {@see SelectionChangedListener} if <code>fireSelectionChangeEvent</code> is true.
|
---|
420 | *
|
---|
421 | * @param selection the selection
|
---|
422 | * @param fireSelectionChangeEvent true, if the selection change listeners are to be notified; false, otherwise
|
---|
423 | */
|
---|
424 | public void addSelected(Collection<? extends PrimitiveId> selection, boolean fireSelectionChangeEvent) {
|
---|
425 | boolean changed = false;
|
---|
426 | for (PrimitiveId id: selection) {
|
---|
427 | OsmPrimitive primitive = getPrimitiveByIdChecked(id);
|
---|
428 | if (primitive != null) {
|
---|
429 | changed = changed | selectedPrimitives.add(primitive);
|
---|
430 | }
|
---|
431 | }
|
---|
432 | if (fireSelectionChangeEvent && changed) {
|
---|
433 | fireSelectionChanged();
|
---|
434 | }
|
---|
435 | }
|
---|
436 |
|
---|
437 | /**
|
---|
438 | * Remove the selection from every value in the collection.
|
---|
439 | * @param list The collection to remove the selection from.
|
---|
440 | */
|
---|
441 | public void clearSelection(PrimitiveId... osm) {
|
---|
442 | clearSelection(Arrays.asList(osm));
|
---|
443 | }
|
---|
444 | public void clearSelection(Collection<? extends PrimitiveId> list) {
|
---|
445 | boolean changed = false;
|
---|
446 | for (PrimitiveId id:list) {
|
---|
447 | OsmPrimitive primitive = getPrimitiveById(id);
|
---|
448 | if (primitive != null) {
|
---|
449 | changed = changed | selectedPrimitives.remove(primitive);
|
---|
450 | }
|
---|
451 | }
|
---|
452 | if (changed) {
|
---|
453 | fireSelectionChanged();
|
---|
454 | }
|
---|
455 | }
|
---|
456 | public void clearSelection() {
|
---|
457 | if (!selectedPrimitives.isEmpty()) {
|
---|
458 | selectedPrimitives.clear();
|
---|
459 | fireSelectionChanged();
|
---|
460 | }
|
---|
461 | }
|
---|
462 |
|
---|
463 | /*------------------------------------------------------
|
---|
464 | * FILTERED / DISABLED HANDLING
|
---|
465 | *-----------------------------------------------------*/
|
---|
466 |
|
---|
467 | public void setDisabled(OsmPrimitive... osm) {
|
---|
468 | if (osm.length == 1 && osm[0] == null) {
|
---|
469 | setDisabled();
|
---|
470 | return;
|
---|
471 | }
|
---|
472 | clearDisabled(nodes);
|
---|
473 | clearDisabled(ways);
|
---|
474 | clearDisabled(relations);
|
---|
475 | for (OsmPrimitive o : osm)
|
---|
476 | if (o != null) {
|
---|
477 | o.setDisabled(true);
|
---|
478 | }
|
---|
479 | }
|
---|
480 |
|
---|
481 | public void setFiltered(Collection<? extends OsmPrimitive> selection) {
|
---|
482 | clearFiltered(nodes);
|
---|
483 | clearFiltered(ways);
|
---|
484 | clearFiltered(relations);
|
---|
485 | for (OsmPrimitive osm : selection) {
|
---|
486 | osm.setFiltered(true);
|
---|
487 | }
|
---|
488 | }
|
---|
489 |
|
---|
490 | public void setFiltered(OsmPrimitive... osm) {
|
---|
491 | if (osm.length == 1 && osm[0] == null) {
|
---|
492 | setFiltered();
|
---|
493 | return;
|
---|
494 | }
|
---|
495 | clearFiltered(nodes);
|
---|
496 | clearFiltered(ways);
|
---|
497 | clearFiltered(relations);
|
---|
498 | for (OsmPrimitive o : osm)
|
---|
499 | if (o != null) {
|
---|
500 | o.setFiltered(true);
|
---|
501 | }
|
---|
502 | }
|
---|
503 |
|
---|
504 | public void setDisabled(Collection<? extends OsmPrimitive> selection) {
|
---|
505 | clearDisabled(nodes);
|
---|
506 | clearDisabled(ways);
|
---|
507 | clearDisabled(relations);
|
---|
508 | for (OsmPrimitive osm : selection) {
|
---|
509 | osm.setDisabled(true);
|
---|
510 | }
|
---|
511 | }
|
---|
512 |
|
---|
513 | /**
|
---|
514 | * Remove the filtered parameter from every value in the collection.
|
---|
515 | * @param list The collection to remove the filtered parameter from.
|
---|
516 | */
|
---|
517 | private void clearFiltered(Collection<? extends OsmPrimitive> list) {
|
---|
518 | if (list == null)
|
---|
519 | return;
|
---|
520 | for (OsmPrimitive osm : list) {
|
---|
521 | osm.setFiltered(false);
|
---|
522 | }
|
---|
523 | }
|
---|
524 | /**
|
---|
525 | * Remove the disabled parameter from every value in the collection.
|
---|
526 | * @param list The collection to remove the disabled parameter from.
|
---|
527 | */
|
---|
528 | private void clearDisabled(Collection<? extends OsmPrimitive> list) {
|
---|
529 | if (list == null)
|
---|
530 | return;
|
---|
531 | for (OsmPrimitive osm : list) {
|
---|
532 | osm.setDisabled(false);
|
---|
533 | }
|
---|
534 | }
|
---|
535 |
|
---|
536 | @Override public DataSet clone() {
|
---|
537 | DataSet ds = new DataSet();
|
---|
538 | for (Node n : nodes) {
|
---|
539 | ds.addPrimitive(new Node(n));
|
---|
540 | }
|
---|
541 | for (Way w : ways) {
|
---|
542 | ds.addPrimitive(new Way(w));
|
---|
543 | }
|
---|
544 | for (Relation e : relations) {
|
---|
545 | ds.addPrimitive(new Relation(e));
|
---|
546 | }
|
---|
547 | for (DataSource source : dataSources) {
|
---|
548 | ds.dataSources.add(new DataSource(source.bounds, source.origin));
|
---|
549 | }
|
---|
550 | ds.version = version;
|
---|
551 | return ds;
|
---|
552 | }
|
---|
553 |
|
---|
554 | /**
|
---|
555 | * Returns the total area of downloaded data (the "yellow rectangles").
|
---|
556 | * @return Area object encompassing downloaded data.
|
---|
557 | */
|
---|
558 | public Area getDataSourceArea() {
|
---|
559 | if (dataSources.isEmpty()) return null;
|
---|
560 | Area a = new Area();
|
---|
561 | for (DataSource source : dataSources) {
|
---|
562 | // create area from data bounds
|
---|
563 | a.add(new Area(source.bounds.asRect()));
|
---|
564 | }
|
---|
565 | return a;
|
---|
566 | }
|
---|
567 |
|
---|
568 | // Provide well-defined sorting for collections of OsmPrimitives.
|
---|
569 | // FIXME: probably not a good place to put this code.
|
---|
570 | public static OsmPrimitive[] sort(Collection<? extends OsmPrimitive> list) {
|
---|
571 | OsmPrimitive[] selArr = new OsmPrimitive[list.size()];
|
---|
572 | final HashMap<Object, String> h = new HashMap<Object, String>();
|
---|
573 | selArr = list.toArray(selArr);
|
---|
574 | Arrays.sort(selArr, new Comparator<OsmPrimitive>() {
|
---|
575 | public int compare(OsmPrimitive a, OsmPrimitive b) {
|
---|
576 | if (a.getClass() == b.getClass()) {
|
---|
577 | String as = h.get(a);
|
---|
578 | if (as == null) {
|
---|
579 | as = a.getName() != null ? a.getName() : Long.toString(a.getId());
|
---|
580 | h.put(a, as);
|
---|
581 | }
|
---|
582 | String bs = h.get(b);
|
---|
583 | if (bs == null) {
|
---|
584 | bs = b.getName() != null ? b.getName() : Long.toString(b.getId());
|
---|
585 | h.put(b, bs);
|
---|
586 | }
|
---|
587 | int res = as.compareTo(bs);
|
---|
588 | if (res != 0)
|
---|
589 | return res;
|
---|
590 | }
|
---|
591 | return a.compareTo(b);
|
---|
592 | }
|
---|
593 | });
|
---|
594 | return selArr;
|
---|
595 | }
|
---|
596 |
|
---|
597 | /**
|
---|
598 | * returns a primitive with a given id from the data set. null, if no such primitive
|
---|
599 | * exists
|
---|
600 | *
|
---|
601 | * @param id uniqueId of the primitive. Might be < 0 for newly created primitives
|
---|
602 | * @param type the type of the primitive. Must not be null.
|
---|
603 | * @return the primitive
|
---|
604 | * @exception NullPointerException thrown, if type is null
|
---|
605 | */
|
---|
606 | public OsmPrimitive getPrimitiveById(long id, OsmPrimitiveType type) {
|
---|
607 | return getPrimitiveById(new SimplePrimitiveId(id, type), false);
|
---|
608 | }
|
---|
609 |
|
---|
610 | public OsmPrimitive getPrimitiveById(PrimitiveId primitiveId) {
|
---|
611 | return getPrimitiveById(primitiveId, false);
|
---|
612 | }
|
---|
613 |
|
---|
614 | public OsmPrimitive getPrimitiveById(PrimitiveId primitiveId, boolean createNew) {
|
---|
615 | OsmPrimitive result = primitivesMap.get(primitiveId);
|
---|
616 |
|
---|
617 | if (result == null && createNew) {
|
---|
618 | switch (primitiveId.getType()) {
|
---|
619 | case NODE: result = new Node(primitiveId.getUniqueId(), true); break;
|
---|
620 | case WAY: result = new Way(primitiveId.getUniqueId(), true); break;
|
---|
621 | case RELATION: result = new Relation(primitiveId.getUniqueId(), true); break;
|
---|
622 | }
|
---|
623 | addPrimitive(result);
|
---|
624 | }
|
---|
625 |
|
---|
626 | return result;
|
---|
627 | }
|
---|
628 |
|
---|
629 | /**
|
---|
630 | * Show message and stack trace in log in case primitive is not found
|
---|
631 | * @param primitiveId
|
---|
632 | * @return Primitive by id.
|
---|
633 | */
|
---|
634 | private OsmPrimitive getPrimitiveByIdChecked(PrimitiveId primitiveId) {
|
---|
635 | OsmPrimitive result = getPrimitiveById(primitiveId);
|
---|
636 | if (result == null) {
|
---|
637 | System.out.println(tr("JOSM expected to find primitive [{0} {1}] in dataset but it's not there. Please report this "
|
---|
638 | + " at http://josm.openstreetmap.de . This is not a critical error, it should be safe to continue in your work.",
|
---|
639 | primitiveId.getType(), Long.toString(primitiveId.getUniqueId())));
|
---|
640 | new Exception().printStackTrace();
|
---|
641 | }
|
---|
642 |
|
---|
643 | return result;
|
---|
644 | }
|
---|
645 |
|
---|
646 | public Set<Long> getPrimitiveIds() {
|
---|
647 | HashSet<Long> ret = new HashSet<Long>();
|
---|
648 | for (OsmPrimitive primitive : nodes) {
|
---|
649 | ret.add(primitive.getId());
|
---|
650 | }
|
---|
651 | for (OsmPrimitive primitive : ways) {
|
---|
652 | ret.add(primitive.getId());
|
---|
653 | }
|
---|
654 | for (OsmPrimitive primitive : relations) {
|
---|
655 | ret.add(primitive.getId());
|
---|
656 | }
|
---|
657 | return ret;
|
---|
658 | }
|
---|
659 |
|
---|
660 | protected void deleteWay(Way way) {
|
---|
661 | way.setNodes(null);
|
---|
662 | way.setDeleted(true);
|
---|
663 | }
|
---|
664 |
|
---|
665 | /**
|
---|
666 | * removes all references from ways in this dataset to a particular node
|
---|
667 | *
|
---|
668 | * @param node the node
|
---|
669 | */
|
---|
670 | public void unlinkNodeFromWays(Node node) {
|
---|
671 | for (Way way: ways) {
|
---|
672 | List<Node> nodes = way.getNodes();
|
---|
673 | if (nodes.remove(node)) {
|
---|
674 | if (nodes.size() < 2) {
|
---|
675 | deleteWay(way);
|
---|
676 | } else {
|
---|
677 | way.setNodes(nodes);
|
---|
678 | }
|
---|
679 | }
|
---|
680 | }
|
---|
681 | }
|
---|
682 |
|
---|
683 | /**
|
---|
684 | * removes all references from relations in this dataset to this primitive
|
---|
685 | *
|
---|
686 | * @param primitive the primitive
|
---|
687 | */
|
---|
688 | public void unlinkPrimitiveFromRelations(OsmPrimitive primitive) {
|
---|
689 | for (Relation relation : relations) {
|
---|
690 | Iterator<RelationMember> it = relation.getMembers().iterator();
|
---|
691 | while(it.hasNext()) {
|
---|
692 | RelationMember member = it.next();
|
---|
693 | if (member.getMember().equals(primitive)) {
|
---|
694 | it.remove();
|
---|
695 | }
|
---|
696 | }
|
---|
697 | }
|
---|
698 | }
|
---|
699 |
|
---|
700 | /**
|
---|
701 | * removes all references from from other primitives to the
|
---|
702 | * referenced primitive
|
---|
703 | *
|
---|
704 | * @param referencedPrimitive the referenced primitive
|
---|
705 | */
|
---|
706 | public void unlinkReferencesToPrimitive(OsmPrimitive referencedPrimitive) {
|
---|
707 | if (referencedPrimitive instanceof Node) {
|
---|
708 | unlinkNodeFromWays((Node)referencedPrimitive);
|
---|
709 | unlinkPrimitiveFromRelations(referencedPrimitive);
|
---|
710 | } else {
|
---|
711 | unlinkPrimitiveFromRelations(referencedPrimitive);
|
---|
712 | }
|
---|
713 | }
|
---|
714 |
|
---|
715 | /**
|
---|
716 | * Replies a list of parent relations which refer to the relation
|
---|
717 | * <code>child</code>. Replies an empty list if child is null.
|
---|
718 | *
|
---|
719 | * @param child the child relation
|
---|
720 | * @return a list of parent relations which refer to the relation
|
---|
721 | * <code>child</code>
|
---|
722 | */
|
---|
723 | public List<Relation> getParentRelations(Relation child) {
|
---|
724 | ArrayList<Relation> parents = new ArrayList<Relation>();
|
---|
725 | if (child == null)
|
---|
726 | return parents;
|
---|
727 | for (Relation parent : relations) {
|
---|
728 | if (parent == child) {
|
---|
729 | continue;
|
---|
730 | }
|
---|
731 | for (RelationMember member: parent.getMembers()) {
|
---|
732 | if (member.refersTo(child)) {
|
---|
733 | parents.add(parent);
|
---|
734 | break;
|
---|
735 | }
|
---|
736 | }
|
---|
737 | }
|
---|
738 | return parents;
|
---|
739 | }
|
---|
740 |
|
---|
741 | /**
|
---|
742 | * Replies true if there is at least one primitive in this dataset with
|
---|
743 | * {@see OsmPrimitive#isModified()} == <code>true</code>.
|
---|
744 | *
|
---|
745 | * @return true if there is at least one primitive in this dataset with
|
---|
746 | * {@see OsmPrimitive#isModified()} == <code>true</code>.
|
---|
747 | */
|
---|
748 | public boolean isModified() {
|
---|
749 | for (Node n: nodes) {
|
---|
750 | if (n.isModified()) return true;
|
---|
751 | }
|
---|
752 | for (Way w: ways) {
|
---|
753 | if (w.isModified()) return true;
|
---|
754 | }
|
---|
755 | for (Relation r: relations) {
|
---|
756 | if (r.isModified()) return true;
|
---|
757 | }
|
---|
758 | return false;
|
---|
759 | }
|
---|
760 |
|
---|
761 | /**
|
---|
762 | * Reindex all nodes and ways after their coordinates were changed. This is a temporary solution, reindexing should
|
---|
763 | * be automatic in the future
|
---|
764 | * @deprecated Reindexing should be automatic
|
---|
765 | */
|
---|
766 | @Deprecated
|
---|
767 | public void reindexAll() {
|
---|
768 | List<Node> ntmp = new ArrayList<Node>(nodes);
|
---|
769 | nodes.clear();
|
---|
770 | nodes.addAll(ntmp);
|
---|
771 | List<Way> wtmp = new ArrayList<Way>(ways);
|
---|
772 | ways.clear();
|
---|
773 | ways.addAll(wtmp);
|
---|
774 | }
|
---|
775 |
|
---|
776 | private void reindexNode(Node node) {
|
---|
777 | nodes.remove(node);
|
---|
778 | nodes.add(node);
|
---|
779 | for (Way way:OsmPrimitive.getFilteredList(node.getReferrers(), Way.class)) {
|
---|
780 | ways.remove(way);
|
---|
781 | way.updatePosition();
|
---|
782 | ways.add(way);
|
---|
783 | }
|
---|
784 | }
|
---|
785 |
|
---|
786 | private void reindexWay(Way way) {
|
---|
787 | ways.remove(way);
|
---|
788 | way.updatePosition();
|
---|
789 | ways.add(way);
|
---|
790 | }
|
---|
791 |
|
---|
792 | public void addDataSetListener(DataSetListener dsl) {
|
---|
793 | listeners.add(dsl);
|
---|
794 | }
|
---|
795 |
|
---|
796 | public void removeDataSetListener(DataSetListener dsl) {
|
---|
797 | listeners.remove(dsl);
|
---|
798 | }
|
---|
799 |
|
---|
800 | /**
|
---|
801 | * Can be called before bigger changes on dataset. Events are disabled until {@link #endUpdate()}.
|
---|
802 | * {@link DataSetListener#dataChanged()} event is triggered after end of changes
|
---|
803 | * <br>
|
---|
804 | * Typical usecase should look like this:
|
---|
805 | * <pre>
|
---|
806 | * ds.beginUpdate();
|
---|
807 | * try {
|
---|
808 | * ...
|
---|
809 | * } finally {
|
---|
810 | * ds.endUpdate();
|
---|
811 | * }
|
---|
812 | * </pre>
|
---|
813 | */
|
---|
814 | public void beginUpdate() {
|
---|
815 | updateCount++;
|
---|
816 | }
|
---|
817 |
|
---|
818 | /**
|
---|
819 | * @see DataSet#beginUpdate()
|
---|
820 | */
|
---|
821 | public void endUpdate() {
|
---|
822 | if (updateCount > 0) {
|
---|
823 | updateCount--;
|
---|
824 | if (updateCount == 0) {
|
---|
825 | fireDataChanged();
|
---|
826 | }
|
---|
827 | } else
|
---|
828 | throw new AssertionError("endUpdate called without beginUpdate");
|
---|
829 | }
|
---|
830 |
|
---|
831 | private void fireEvent(AbstractDatasetChangedEvent event) {
|
---|
832 | if (updateCount == 0) {
|
---|
833 | for (DataSetListener dsl : listeners) {
|
---|
834 | event.fire(dsl);
|
---|
835 | }
|
---|
836 | }
|
---|
837 | }
|
---|
838 |
|
---|
839 | private void fireDataChanged() {
|
---|
840 | fireEvent(new DataChangedEvent(this));
|
---|
841 | }
|
---|
842 |
|
---|
843 | void firePrimitivesAdded(Collection<? extends OsmPrimitive> added, boolean wasIncomplete) {
|
---|
844 | fireEvent(new PrimitivesAddedEvent(this, added, wasIncomplete));
|
---|
845 | }
|
---|
846 |
|
---|
847 | void firePrimitivesRemoved(Collection<? extends OsmPrimitive> removed, boolean wasComplete) {
|
---|
848 | fireEvent(new PrimitivesRemovedEvent(this, removed, wasComplete));
|
---|
849 | }
|
---|
850 |
|
---|
851 | void fireTagsChanged(OsmPrimitive prim, Map<String, String> originalKeys) {
|
---|
852 | fireEvent(new TagsChangedEvent(this, prim, originalKeys));
|
---|
853 | }
|
---|
854 |
|
---|
855 | void fireRelationMembersChanged(Relation r) {
|
---|
856 | fireEvent(new RelationMembersChangedEvent(this, r));
|
---|
857 | }
|
---|
858 |
|
---|
859 | void fireNodeMoved(Node node) {
|
---|
860 | reindexNode(node);
|
---|
861 | fireEvent(new NodeMovedEvent(this, node));
|
---|
862 | }
|
---|
863 |
|
---|
864 | void fireWayNodesChanged(Way way) {
|
---|
865 | reindexWay(way);
|
---|
866 | fireEvent(new WayNodesChangedEvent(this, way));
|
---|
867 | }
|
---|
868 |
|
---|
869 | void fireChangesetIdChanged(OsmPrimitive primitive, int oldChangesetId, int newChangesetId) {
|
---|
870 | fireEvent(new ChangesetIdChangedEvent(this, Collections.singletonList(primitive), oldChangesetId, newChangesetId));
|
---|
871 | }
|
---|
872 |
|
---|
873 | public void clenupDeletedPrimitives() {
|
---|
874 | if (cleanupDeleted(nodes.iterator())
|
---|
875 | | cleanupDeleted(ways.iterator())
|
---|
876 | | cleanupDeleted(relations.iterator())) {
|
---|
877 | fireSelectionChanged();
|
---|
878 | }
|
---|
879 | }
|
---|
880 |
|
---|
881 | private boolean cleanupDeleted(Iterator<? extends OsmPrimitive> it) {
|
---|
882 | boolean changed = false;
|
---|
883 | while (it.hasNext()) {
|
---|
884 | OsmPrimitive primitive = it.next();
|
---|
885 | if (primitive.isDeleted()) {
|
---|
886 | selectedPrimitives.remove(primitive);
|
---|
887 | allPrimitives.remove(primitive);
|
---|
888 | primitive.setDataset(null);
|
---|
889 | changed = true;
|
---|
890 | it.remove();
|
---|
891 | }
|
---|
892 | }
|
---|
893 | return changed;
|
---|
894 | }
|
---|
895 |
|
---|
896 | /**
|
---|
897 | * Removes all primitives from the dataset and resets the currently selected primitives
|
---|
898 | * to the empty collection. Also notifies selection change listeners if necessary.
|
---|
899 | *
|
---|
900 | */
|
---|
901 | public void clear() {
|
---|
902 | clearSelection();
|
---|
903 | for (OsmPrimitive primitive:allPrimitives) {
|
---|
904 | primitive.setDataset(null);
|
---|
905 | }
|
---|
906 | nodes.clear();
|
---|
907 | ways.clear();
|
---|
908 | relations.clear();
|
---|
909 | allPrimitives.clear();
|
---|
910 | }
|
---|
911 |
|
---|
912 | // TODO Should be completely part of validator
|
---|
913 | private Map<OsmPrimitive, List<String>> errors = new HashMap<OsmPrimitive, List<String>>();
|
---|
914 |
|
---|
915 | public void addError(OsmPrimitive primitive, String error) {
|
---|
916 | List<String> perrors = errors.get(primitive);
|
---|
917 | if (perrors == null) {
|
---|
918 | perrors = new ArrayList<String>();
|
---|
919 | }
|
---|
920 | perrors.add(error);
|
---|
921 | errors.put(primitive, perrors);
|
---|
922 | }
|
---|
923 |
|
---|
924 | /**
|
---|
925 | * Replies the list of errors registered for this primitive.
|
---|
926 | *
|
---|
927 | * @param primitive the primitive for which errors are queried
|
---|
928 | * @return the list of errors. Never null.
|
---|
929 | * @deprecated should be moved to the validator plugin
|
---|
930 | */
|
---|
931 | @Deprecated
|
---|
932 | public List<String> getErrors(OsmPrimitive primitive) {
|
---|
933 | List<String> ret = errors.get(primitive);
|
---|
934 | if (ret == null) {
|
---|
935 | ret = Collections.emptyList();
|
---|
936 | }
|
---|
937 | return ret;
|
---|
938 | }
|
---|
939 |
|
---|
940 | public void clearErrors()
|
---|
941 | {
|
---|
942 | errors.clear();
|
---|
943 | }
|
---|
944 | }
|
---|