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