1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.osm;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.util.ArrayList;
|
---|
7 | import java.util.Arrays;
|
---|
8 | import java.util.Collection;
|
---|
9 | import java.util.Collections;
|
---|
10 | import java.util.HashMap;
|
---|
11 | import java.util.HashSet;
|
---|
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.Set;
|
---|
18 | import java.util.concurrent.CopyOnWriteArrayList;
|
---|
19 | import java.util.concurrent.locks.Lock;
|
---|
20 | import java.util.concurrent.locks.ReadWriteLock;
|
---|
21 | import java.util.concurrent.locks.ReentrantReadWriteLock;
|
---|
22 | import java.util.function.Predicate;
|
---|
23 | import java.util.stream.Collectors;
|
---|
24 |
|
---|
25 | import org.openstreetmap.josm.Main;
|
---|
26 | import org.openstreetmap.josm.data.Data;
|
---|
27 | import org.openstreetmap.josm.data.DataSource;
|
---|
28 | import org.openstreetmap.josm.data.ProjectionBounds;
|
---|
29 | import org.openstreetmap.josm.data.SelectionChangedListener;
|
---|
30 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
31 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
32 | import org.openstreetmap.josm.data.osm.event.AbstractDatasetChangedEvent;
|
---|
33 | import org.openstreetmap.josm.data.osm.event.ChangesetIdChangedEvent;
|
---|
34 | import org.openstreetmap.josm.data.osm.event.DataChangedEvent;
|
---|
35 | import org.openstreetmap.josm.data.osm.event.DataSetListener;
|
---|
36 | import org.openstreetmap.josm.data.osm.event.NodeMovedEvent;
|
---|
37 | import org.openstreetmap.josm.data.osm.event.PrimitiveFlagsChangedEvent;
|
---|
38 | import org.openstreetmap.josm.data.osm.event.PrimitivesAddedEvent;
|
---|
39 | import org.openstreetmap.josm.data.osm.event.PrimitivesRemovedEvent;
|
---|
40 | import org.openstreetmap.josm.data.osm.event.RelationMembersChangedEvent;
|
---|
41 | import org.openstreetmap.josm.data.osm.event.TagsChangedEvent;
|
---|
42 | import org.openstreetmap.josm.data.osm.event.WayNodesChangedEvent;
|
---|
43 | import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor;
|
---|
44 | import org.openstreetmap.josm.data.projection.Projection;
|
---|
45 | import org.openstreetmap.josm.data.projection.ProjectionChangeListener;
|
---|
46 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
47 | import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionManager;
|
---|
48 | import org.openstreetmap.josm.tools.JosmRuntimeException;
|
---|
49 | import org.openstreetmap.josm.tools.SubclassFilteredCollection;
|
---|
50 | import org.openstreetmap.josm.tools.Utils;
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * DataSet is the data behind the application. It can consists of only a few points up to the whole
|
---|
54 | * osm database. DataSet's can be merged together, saved, (up/down/disk)loaded etc.
|
---|
55 | *
|
---|
56 | * Note that DataSet is not an osm-primitive and so has no key association but a few members to
|
---|
57 | * store some information.
|
---|
58 | *
|
---|
59 | * Dataset is threadsafe - accessing Dataset simultaneously from different threads should never
|
---|
60 | * lead to data corruption or ConccurentModificationException. However when for example one thread
|
---|
61 | * removes primitive and other thread try to add another primitive referring to the removed primitive,
|
---|
62 | * DataIntegrityException will occur.
|
---|
63 | *
|
---|
64 | * To prevent such situations, read/write lock is provided. While read lock is used, it's guaranteed that
|
---|
65 | * Dataset will not change. Sample usage:
|
---|
66 | * <code>
|
---|
67 | * ds.getReadLock().lock();
|
---|
68 | * try {
|
---|
69 | * // .. do something with dataset
|
---|
70 | * } finally {
|
---|
71 | * ds.getReadLock().unlock();
|
---|
72 | * }
|
---|
73 | * </code>
|
---|
74 | *
|
---|
75 | * Write lock should be used in case of bulk operations. In addition to ensuring that other threads can't
|
---|
76 | * use dataset in the middle of modifications it also stops sending of dataset events. That's good for performance
|
---|
77 | * reasons - GUI can be updated after all changes are done.
|
---|
78 | * Sample usage:
|
---|
79 | * <code>
|
---|
80 | * ds.beginUpdate()
|
---|
81 | * try {
|
---|
82 | * // .. do modifications
|
---|
83 | * } finally {
|
---|
84 | * ds.endUpdate();
|
---|
85 | * }
|
---|
86 | * </code>
|
---|
87 | *
|
---|
88 | * Note that it is not necessary to call beginUpdate/endUpdate for every dataset modification - dataset will get locked
|
---|
89 | * automatically.
|
---|
90 | *
|
---|
91 | * Note that locks cannot be upgraded - if one threads use read lock and and then write lock, dead lock will occur - see #5814 for
|
---|
92 | * sample ticket
|
---|
93 | *
|
---|
94 | * @author imi
|
---|
95 | */
|
---|
96 | public final class DataSet implements Data, ProjectionChangeListener {
|
---|
97 |
|
---|
98 | /**
|
---|
99 | * Maximum number of events that can be fired between beginUpdate/endUpdate to be send as single events (ie without DatasetChangedEvent)
|
---|
100 | */
|
---|
101 | private static final int MAX_SINGLE_EVENTS = 30;
|
---|
102 |
|
---|
103 | /**
|
---|
104 | * Maximum number of events to kept between beginUpdate/endUpdate. When more events are created, that simple DatasetChangedEvent is sent)
|
---|
105 | */
|
---|
106 | private static final int MAX_EVENTS = 1000;
|
---|
107 |
|
---|
108 | private final Storage<OsmPrimitive> allPrimitives = new Storage<>(new Storage.PrimitiveIdHash(), true);
|
---|
109 | private final Map<PrimitiveId, OsmPrimitive> primitivesMap = allPrimitives.foreignKey(new Storage.PrimitiveIdHash());
|
---|
110 | private final CopyOnWriteArrayList<DataSetListener> listeners = new CopyOnWriteArrayList<>();
|
---|
111 |
|
---|
112 | // provide means to highlight map elements that are not osm primitives
|
---|
113 | private Collection<WaySegment> highlightedVirtualNodes = new LinkedList<>();
|
---|
114 | private Collection<WaySegment> highlightedWaySegments = new LinkedList<>();
|
---|
115 |
|
---|
116 | // Number of open calls to beginUpdate
|
---|
117 | private int updateCount;
|
---|
118 | // Events that occurred while dataset was locked but should be fired after write lock is released
|
---|
119 | private final List<AbstractDatasetChangedEvent> cachedEvents = new ArrayList<>();
|
---|
120 |
|
---|
121 | private int highlightUpdateCount;
|
---|
122 |
|
---|
123 | private boolean uploadDiscouraged;
|
---|
124 |
|
---|
125 | private final ReadWriteLock lock = new ReentrantReadWriteLock();
|
---|
126 | private final Object selectionLock = new Object();
|
---|
127 |
|
---|
128 | /**
|
---|
129 | * Constructs a new {@code DataSet}.
|
---|
130 | */
|
---|
131 | public DataSet() {
|
---|
132 | // Transparently register as projection change listener. No need to explicitly remove
|
---|
133 | // the listener, projection change listeners are managed as WeakReferences.
|
---|
134 | Main.addProjectionChangeListener(this);
|
---|
135 | }
|
---|
136 |
|
---|
137 | /**
|
---|
138 | * Creates a new {@link DataSet}.
|
---|
139 | * @param copyFrom An other {@link DataSet} to copy the contents of this dataset from.
|
---|
140 | * @since 10346
|
---|
141 | */
|
---|
142 | public DataSet(DataSet copyFrom) {
|
---|
143 | this();
|
---|
144 | copyFrom.getReadLock().lock();
|
---|
145 | try {
|
---|
146 | Map<OsmPrimitive, OsmPrimitive> primMap = new HashMap<>();
|
---|
147 | for (Node n : copyFrom.nodes) {
|
---|
148 | Node newNode = new Node(n);
|
---|
149 | primMap.put(n, newNode);
|
---|
150 | addPrimitive(newNode);
|
---|
151 | }
|
---|
152 | for (Way w : copyFrom.ways) {
|
---|
153 | Way newWay = new Way(w);
|
---|
154 | primMap.put(w, newWay);
|
---|
155 | List<Node> newNodes = new ArrayList<>();
|
---|
156 | for (Node n: w.getNodes()) {
|
---|
157 | newNodes.add((Node) primMap.get(n));
|
---|
158 | }
|
---|
159 | newWay.setNodes(newNodes);
|
---|
160 | addPrimitive(newWay);
|
---|
161 | }
|
---|
162 | // Because relations can have other relations as members we first clone all relations
|
---|
163 | // and then get the cloned members
|
---|
164 | for (Relation r : copyFrom.relations) {
|
---|
165 | Relation newRelation = new Relation(r, r.isNew());
|
---|
166 | newRelation.setMembers(null);
|
---|
167 | primMap.put(r, newRelation);
|
---|
168 | addPrimitive(newRelation);
|
---|
169 | }
|
---|
170 | for (Relation r : copyFrom.relations) {
|
---|
171 | Relation newRelation = (Relation) primMap.get(r);
|
---|
172 | List<RelationMember> newMembers = new ArrayList<>();
|
---|
173 | for (RelationMember rm: r.getMembers()) {
|
---|
174 | newMembers.add(new RelationMember(rm.getRole(), primMap.get(rm.getMember())));
|
---|
175 | }
|
---|
176 | newRelation.setMembers(newMembers);
|
---|
177 | }
|
---|
178 | for (DataSource source : copyFrom.dataSources) {
|
---|
179 | dataSources.add(new DataSource(source));
|
---|
180 | }
|
---|
181 | version = copyFrom.version;
|
---|
182 | } finally {
|
---|
183 | copyFrom.getReadLock().unlock();
|
---|
184 | }
|
---|
185 | }
|
---|
186 |
|
---|
187 | /**
|
---|
188 | * Returns the lock used for reading.
|
---|
189 | * @return the lock used for reading
|
---|
190 | */
|
---|
191 | public Lock getReadLock() {
|
---|
192 | return lock.readLock();
|
---|
193 | }
|
---|
194 |
|
---|
195 | /**
|
---|
196 | * This method can be used to detect changes in highlight state of primitives. If highlighting was changed
|
---|
197 | * then the method will return different number.
|
---|
198 | * @return the current highlight counter
|
---|
199 | */
|
---|
200 | public int getHighlightUpdateCount() {
|
---|
201 | return highlightUpdateCount;
|
---|
202 | }
|
---|
203 |
|
---|
204 | /**
|
---|
205 | * History of selections - shared by plugins and SelectionListDialog
|
---|
206 | */
|
---|
207 | private final LinkedList<Collection<? extends OsmPrimitive>> selectionHistory = new LinkedList<>();
|
---|
208 |
|
---|
209 | /**
|
---|
210 | * Replies the history of JOSM selections
|
---|
211 | *
|
---|
212 | * @return list of history entries
|
---|
213 | */
|
---|
214 | public LinkedList<Collection<? extends OsmPrimitive>> getSelectionHistory() {
|
---|
215 | return selectionHistory;
|
---|
216 | }
|
---|
217 |
|
---|
218 | /**
|
---|
219 | * Clears selection history list
|
---|
220 | */
|
---|
221 | public void clearSelectionHistory() {
|
---|
222 | selectionHistory.clear();
|
---|
223 | }
|
---|
224 |
|
---|
225 | /**
|
---|
226 | * Maintains a list of used tags for autocompletion.
|
---|
227 | */
|
---|
228 | private AutoCompletionManager autocomplete;
|
---|
229 |
|
---|
230 | /**
|
---|
231 | * Returns the autocompletion manager, which maintains a list of used tags for autocompletion.
|
---|
232 | * @return the autocompletion manager
|
---|
233 | */
|
---|
234 | public AutoCompletionManager getAutoCompletionManager() {
|
---|
235 | if (autocomplete == null) {
|
---|
236 | autocomplete = new AutoCompletionManager(this);
|
---|
237 | addDataSetListener(autocomplete);
|
---|
238 | }
|
---|
239 | return autocomplete;
|
---|
240 | }
|
---|
241 |
|
---|
242 | /**
|
---|
243 | * The API version that created this data set, if any.
|
---|
244 | */
|
---|
245 | private String version;
|
---|
246 |
|
---|
247 | /**
|
---|
248 | * Replies the API version this dataset was created from. May be null.
|
---|
249 | *
|
---|
250 | * @return the API version this dataset was created from. May be null.
|
---|
251 | */
|
---|
252 | public String getVersion() {
|
---|
253 | return version;
|
---|
254 | }
|
---|
255 |
|
---|
256 | /**
|
---|
257 | * Sets the API version this dataset was created from.
|
---|
258 | *
|
---|
259 | * @param version the API version, i.e. "0.6"
|
---|
260 | */
|
---|
261 | public void setVersion(String version) {
|
---|
262 | this.version = version;
|
---|
263 | }
|
---|
264 |
|
---|
265 | /**
|
---|
266 | * Determines if upload is being discouraged (i.e. this dataset contains private data which should not be uploaded)
|
---|
267 | * @return {@code true} if upload is being discouraged, {@code false} otherwise
|
---|
268 | * @see #setUploadDiscouraged
|
---|
269 | */
|
---|
270 | public boolean isUploadDiscouraged() {
|
---|
271 | return uploadDiscouraged;
|
---|
272 | }
|
---|
273 |
|
---|
274 | /**
|
---|
275 | * Sets the "upload discouraged" flag.
|
---|
276 | * @param uploadDiscouraged {@code true} if this dataset contains private data which should not be uploaded
|
---|
277 | * @see #isUploadDiscouraged
|
---|
278 | */
|
---|
279 | public void setUploadDiscouraged(boolean uploadDiscouraged) {
|
---|
280 | this.uploadDiscouraged = uploadDiscouraged;
|
---|
281 | }
|
---|
282 |
|
---|
283 | /**
|
---|
284 | * Holding bin for changeset tag information, to be applied when or if this is ever uploaded.
|
---|
285 | */
|
---|
286 | private final Map<String, String> changeSetTags = new HashMap<>();
|
---|
287 |
|
---|
288 | /**
|
---|
289 | * Replies the set of changeset tags to be applied when or if this is ever uploaded.
|
---|
290 | * @return the set of changeset tags
|
---|
291 | * @see #addChangeSetTag
|
---|
292 | */
|
---|
293 | public Map<String, String> getChangeSetTags() {
|
---|
294 | return changeSetTags;
|
---|
295 | }
|
---|
296 |
|
---|
297 | /**
|
---|
298 | * Adds a new changeset tag.
|
---|
299 | * @param k Key
|
---|
300 | * @param v Value
|
---|
301 | * @see #getChangeSetTags
|
---|
302 | */
|
---|
303 | public void addChangeSetTag(String k, String v) {
|
---|
304 | this.changeSetTags.put(k, v);
|
---|
305 | }
|
---|
306 |
|
---|
307 | /**
|
---|
308 | * All nodes goes here, even when included in other data (ways etc). This enables the instant
|
---|
309 | * conversion of the whole DataSet by iterating over this data structure.
|
---|
310 | */
|
---|
311 | private final QuadBuckets<Node> nodes = new QuadBuckets<>();
|
---|
312 |
|
---|
313 | /**
|
---|
314 | * Gets a filtered collection of primitives matching the given predicate.
|
---|
315 | * @param <T> The primitive type.
|
---|
316 | * @param predicate The predicate to match
|
---|
317 | * @return The list of primtives.
|
---|
318 | * @since 10590
|
---|
319 | */
|
---|
320 | public <T extends OsmPrimitive> Collection<T> getPrimitives(Predicate<? super OsmPrimitive> predicate) {
|
---|
321 | return new SubclassFilteredCollection<>(allPrimitives, predicate);
|
---|
322 | }
|
---|
323 |
|
---|
324 | /**
|
---|
325 | * Replies an unmodifiable collection of nodes in this dataset
|
---|
326 | *
|
---|
327 | * @return an unmodifiable collection of nodes in this dataset
|
---|
328 | */
|
---|
329 | public Collection<Node> getNodes() {
|
---|
330 | return getPrimitives(Node.class::isInstance);
|
---|
331 | }
|
---|
332 |
|
---|
333 | /**
|
---|
334 | * Searches for nodes in the given bounding box.
|
---|
335 | * @param bbox the bounding box
|
---|
336 | * @return List of nodes in the given bbox. Can be empty but not null
|
---|
337 | */
|
---|
338 | public List<Node> searchNodes(BBox bbox) {
|
---|
339 | lock.readLock().lock();
|
---|
340 | try {
|
---|
341 | return nodes.search(bbox);
|
---|
342 | } finally {
|
---|
343 | lock.readLock().unlock();
|
---|
344 | }
|
---|
345 | }
|
---|
346 |
|
---|
347 | /**
|
---|
348 | * Determines if the given node can be retrieved in the data set through its bounding box. Useful for dataset consistency test.
|
---|
349 | * For efficiency reasons this method does not lock the dataset, you have to lock it manually.
|
---|
350 | *
|
---|
351 | * @param n The node to search
|
---|
352 | * @return {@code true} if {@code n} ban be retrieved in this data set, {@code false} otherwise
|
---|
353 | * @since 7501
|
---|
354 | */
|
---|
355 | public boolean containsNode(Node n) {
|
---|
356 | return nodes.contains(n);
|
---|
357 | }
|
---|
358 |
|
---|
359 | /**
|
---|
360 | * All ways (Streets etc.) in the DataSet.
|
---|
361 | *
|
---|
362 | * The way nodes are stored only in the way list.
|
---|
363 | */
|
---|
364 | private final QuadBuckets<Way> ways = new QuadBuckets<>();
|
---|
365 |
|
---|
366 | /**
|
---|
367 | * Replies an unmodifiable collection of ways in this dataset
|
---|
368 | *
|
---|
369 | * @return an unmodifiable collection of ways in this dataset
|
---|
370 | */
|
---|
371 | public Collection<Way> getWays() {
|
---|
372 | return getPrimitives(Way.class::isInstance);
|
---|
373 | }
|
---|
374 |
|
---|
375 | /**
|
---|
376 | * Searches for ways in the given bounding box.
|
---|
377 | * @param bbox the bounding box
|
---|
378 | * @return List of ways in the given bbox. Can be empty but not null
|
---|
379 | */
|
---|
380 | public List<Way> searchWays(BBox bbox) {
|
---|
381 | lock.readLock().lock();
|
---|
382 | try {
|
---|
383 | return ways.search(bbox);
|
---|
384 | } finally {
|
---|
385 | lock.readLock().unlock();
|
---|
386 | }
|
---|
387 | }
|
---|
388 |
|
---|
389 | /**
|
---|
390 | * Determines if the given way can be retrieved in the data set through its bounding box. Useful for dataset consistency test.
|
---|
391 | * For efficiency reasons this method does not lock the dataset, you have to lock it manually.
|
---|
392 | *
|
---|
393 | * @param w The way to search
|
---|
394 | * @return {@code true} if {@code w} ban be retrieved in this data set, {@code false} otherwise
|
---|
395 | * @since 7501
|
---|
396 | */
|
---|
397 | public boolean containsWay(Way w) {
|
---|
398 | return ways.contains(w);
|
---|
399 | }
|
---|
400 |
|
---|
401 | /**
|
---|
402 | * All relations/relationships
|
---|
403 | */
|
---|
404 | private final Collection<Relation> relations = new ArrayList<>();
|
---|
405 |
|
---|
406 | /**
|
---|
407 | * Replies an unmodifiable collection of relations in this dataset
|
---|
408 | *
|
---|
409 | * @return an unmodifiable collection of relations in this dataset
|
---|
410 | */
|
---|
411 | public Collection<Relation> getRelations() {
|
---|
412 | return getPrimitives(Relation.class::isInstance);
|
---|
413 | }
|
---|
414 |
|
---|
415 | /**
|
---|
416 | * Searches for relations in the given bounding box.
|
---|
417 | * @param bbox the bounding box
|
---|
418 | * @return List of relations in the given bbox. Can be empty but not null
|
---|
419 | */
|
---|
420 | public List<Relation> searchRelations(BBox bbox) {
|
---|
421 | lock.readLock().lock();
|
---|
422 | try {
|
---|
423 | // QuadBuckets might be useful here (don't forget to do reindexing after some of rm is changed)
|
---|
424 | return relations.stream()
|
---|
425 | .filter(r -> r.getBBox().intersects(bbox))
|
---|
426 | .collect(Collectors.toList());
|
---|
427 | } finally {
|
---|
428 | lock.readLock().unlock();
|
---|
429 | }
|
---|
430 | }
|
---|
431 |
|
---|
432 | /**
|
---|
433 | * Determines if the given relation can be retrieved in the data set through its bounding box. Useful for dataset consistency test.
|
---|
434 | * For efficiency reasons this method does not lock the dataset, you have to lock it manually.
|
---|
435 | *
|
---|
436 | * @param r The relation to search
|
---|
437 | * @return {@code true} if {@code r} ban be retrieved in this data set, {@code false} otherwise
|
---|
438 | * @since 7501
|
---|
439 | */
|
---|
440 | public boolean containsRelation(Relation r) {
|
---|
441 | return relations.contains(r);
|
---|
442 | }
|
---|
443 |
|
---|
444 | /**
|
---|
445 | * All data sources of this DataSet.
|
---|
446 | */
|
---|
447 | public final Collection<DataSource> dataSources = new LinkedList<>();
|
---|
448 |
|
---|
449 | /**
|
---|
450 | * Returns a collection containing all primitives of the dataset.
|
---|
451 | * @return A collection containing all primitives of the dataset. Data is not ordered
|
---|
452 | */
|
---|
453 | public Collection<OsmPrimitive> allPrimitives() {
|
---|
454 | return getPrimitives(o -> true);
|
---|
455 | }
|
---|
456 |
|
---|
457 | /**
|
---|
458 | * Returns a collection containing all not-deleted primitives.
|
---|
459 | * @return A collection containing all not-deleted primitives.
|
---|
460 | * @see OsmPrimitive#isDeleted
|
---|
461 | */
|
---|
462 | public Collection<OsmPrimitive> allNonDeletedPrimitives() {
|
---|
463 | return getPrimitives(p -> !p.isDeleted());
|
---|
464 | }
|
---|
465 |
|
---|
466 | /**
|
---|
467 | * Returns a collection containing all not-deleted complete primitives.
|
---|
468 | * @return A collection containing all not-deleted complete primitives.
|
---|
469 | * @see OsmPrimitive#isDeleted
|
---|
470 | * @see OsmPrimitive#isIncomplete
|
---|
471 | */
|
---|
472 | public Collection<OsmPrimitive> allNonDeletedCompletePrimitives() {
|
---|
473 | return getPrimitives(primitive -> !primitive.isDeleted() && !primitive.isIncomplete());
|
---|
474 | }
|
---|
475 |
|
---|
476 | /**
|
---|
477 | * Returns a collection containing all not-deleted complete physical primitives.
|
---|
478 | * @return A collection containing all not-deleted complete physical primitives (nodes and ways).
|
---|
479 | * @see OsmPrimitive#isDeleted
|
---|
480 | * @see OsmPrimitive#isIncomplete
|
---|
481 | */
|
---|
482 | public Collection<OsmPrimitive> allNonDeletedPhysicalPrimitives() {
|
---|
483 | return getPrimitives(primitive -> !primitive.isDeleted() && !primitive.isIncomplete() && !(primitive instanceof Relation));
|
---|
484 | }
|
---|
485 |
|
---|
486 | /**
|
---|
487 | * Returns a collection containing all modified primitives.
|
---|
488 | * @return A collection containing all modified primitives.
|
---|
489 | * @see OsmPrimitive#isModified
|
---|
490 | */
|
---|
491 | public Collection<OsmPrimitive> allModifiedPrimitives() {
|
---|
492 | return getPrimitives(OsmPrimitive::isModified);
|
---|
493 | }
|
---|
494 |
|
---|
495 | /**
|
---|
496 | * Adds a primitive to the dataset.
|
---|
497 | *
|
---|
498 | * @param primitive the primitive.
|
---|
499 | */
|
---|
500 | public void addPrimitive(OsmPrimitive primitive) {
|
---|
501 | beginUpdate();
|
---|
502 | try {
|
---|
503 | if (getPrimitiveById(primitive) != null)
|
---|
504 | throw new DataIntegrityProblemException(
|
---|
505 | tr("Unable to add primitive {0} to the dataset because it is already included", primitive.toString()));
|
---|
506 |
|
---|
507 | allPrimitives.add(primitive);
|
---|
508 | primitive.setDataset(this);
|
---|
509 | primitive.updatePosition(); // Set cached bbox for way and relation (required for reindexWay and reindexRelation to work properly)
|
---|
510 | boolean success = false;
|
---|
511 | if (primitive instanceof Node) {
|
---|
512 | success = nodes.add((Node) primitive);
|
---|
513 | } else if (primitive instanceof Way) {
|
---|
514 | success = ways.add((Way) primitive);
|
---|
515 | } else if (primitive instanceof Relation) {
|
---|
516 | success = relations.add((Relation) primitive);
|
---|
517 | }
|
---|
518 | if (!success)
|
---|
519 | throw new JosmRuntimeException("failed to add primitive: "+primitive);
|
---|
520 | firePrimitivesAdded(Collections.singletonList(primitive), false);
|
---|
521 | } finally {
|
---|
522 | endUpdate();
|
---|
523 | }
|
---|
524 | }
|
---|
525 |
|
---|
526 | /**
|
---|
527 | * Removes a primitive from the dataset. This method only removes the
|
---|
528 | * primitive form the respective collection of primitives managed
|
---|
529 | * by this dataset, i.e. from {@link #nodes}, {@link #ways}, or
|
---|
530 | * {@link #relations}. References from other primitives to this
|
---|
531 | * primitive are left unchanged.
|
---|
532 | *
|
---|
533 | * @param primitiveId the id of the primitive
|
---|
534 | */
|
---|
535 | public void removePrimitive(PrimitiveId primitiveId) {
|
---|
536 | beginUpdate();
|
---|
537 | try {
|
---|
538 | OsmPrimitive primitive = getPrimitiveByIdChecked(primitiveId);
|
---|
539 | if (primitive == null)
|
---|
540 | return;
|
---|
541 | boolean success = false;
|
---|
542 | if (primitive instanceof Node) {
|
---|
543 | success = nodes.remove(primitive);
|
---|
544 | } else if (primitive instanceof Way) {
|
---|
545 | success = ways.remove(primitive);
|
---|
546 | } else if (primitive instanceof Relation) {
|
---|
547 | success = relations.remove(primitive);
|
---|
548 | }
|
---|
549 | if (!success)
|
---|
550 | throw new JosmRuntimeException("failed to remove primitive: "+primitive);
|
---|
551 | synchronized (selectionLock) {
|
---|
552 | selectedPrimitives.remove(primitive);
|
---|
553 | selectionSnapshot = null;
|
---|
554 | }
|
---|
555 | allPrimitives.remove(primitive);
|
---|
556 | primitive.setDataset(null);
|
---|
557 | firePrimitivesRemoved(Collections.singletonList(primitive), false);
|
---|
558 | } finally {
|
---|
559 | endUpdate();
|
---|
560 | }
|
---|
561 | }
|
---|
562 |
|
---|
563 | /*---------------------------------------------------
|
---|
564 | * SELECTION HANDLING
|
---|
565 | *---------------------------------------------------*/
|
---|
566 |
|
---|
567 | /**
|
---|
568 | * A list of listeners to selection changed events. The list is static, as listeners register
|
---|
569 | * themselves for any dataset selection changes that occur, regardless of the current active
|
---|
570 | * dataset. (However, the selection does only change in the active layer)
|
---|
571 | */
|
---|
572 | private static final Collection<SelectionChangedListener> selListeners = new CopyOnWriteArrayList<>();
|
---|
573 |
|
---|
574 | /**
|
---|
575 | * Adds a new selection listener.
|
---|
576 | * @param listener The selection listener to add
|
---|
577 | */
|
---|
578 | public static void addSelectionListener(SelectionChangedListener listener) {
|
---|
579 | ((CopyOnWriteArrayList<SelectionChangedListener>) selListeners).addIfAbsent(listener);
|
---|
580 | }
|
---|
581 |
|
---|
582 | /**
|
---|
583 | * Removes a selection listener.
|
---|
584 | * @param listener The selection listener to remove
|
---|
585 | */
|
---|
586 | public static void removeSelectionListener(SelectionChangedListener listener) {
|
---|
587 | selListeners.remove(listener);
|
---|
588 | }
|
---|
589 |
|
---|
590 | /**
|
---|
591 | * Notifies all registered {@link SelectionChangedListener} about the current selection in
|
---|
592 | * this dataset.
|
---|
593 | *
|
---|
594 | */
|
---|
595 | public void fireSelectionChanged() {
|
---|
596 | Collection<? extends OsmPrimitive> currentSelection = getAllSelected();
|
---|
597 | for (SelectionChangedListener l : selListeners) {
|
---|
598 | l.selectionChanged(currentSelection);
|
---|
599 | }
|
---|
600 | }
|
---|
601 |
|
---|
602 | private Set<OsmPrimitive> selectedPrimitives = new LinkedHashSet<>();
|
---|
603 | private Collection<OsmPrimitive> selectionSnapshot;
|
---|
604 |
|
---|
605 | /**
|
---|
606 | * Returns selected nodes and ways.
|
---|
607 | * @return selected nodes and ways
|
---|
608 | */
|
---|
609 | public Collection<OsmPrimitive> getSelectedNodesAndWays() {
|
---|
610 | return new SubclassFilteredCollection<>(getSelected(), primitive -> primitive instanceof Node || primitive instanceof Way);
|
---|
611 | }
|
---|
612 |
|
---|
613 | /**
|
---|
614 | * Returns an unmodifiable collection of *WaySegments* whose virtual
|
---|
615 | * nodes should be highlighted. WaySegments are used to avoid having
|
---|
616 | * to create a VirtualNode class that wouldn't have much purpose otherwise.
|
---|
617 | *
|
---|
618 | * @return unmodifiable collection of WaySegments
|
---|
619 | */
|
---|
620 | public Collection<WaySegment> getHighlightedVirtualNodes() {
|
---|
621 | return Collections.unmodifiableCollection(highlightedVirtualNodes);
|
---|
622 | }
|
---|
623 |
|
---|
624 | /**
|
---|
625 | * Returns an unmodifiable collection of WaySegments that should be highlighted.
|
---|
626 | *
|
---|
627 | * @return unmodifiable collection of WaySegments
|
---|
628 | */
|
---|
629 | public Collection<WaySegment> getHighlightedWaySegments() {
|
---|
630 | return Collections.unmodifiableCollection(highlightedWaySegments);
|
---|
631 | }
|
---|
632 |
|
---|
633 | /**
|
---|
634 | * Replies an unmodifiable collection of primitives currently selected
|
---|
635 | * in this dataset, except deleted ones. May be empty, but not null.
|
---|
636 | *
|
---|
637 | * @return unmodifiable collection of primitives
|
---|
638 | */
|
---|
639 | public Collection<OsmPrimitive> getSelected() {
|
---|
640 | return new SubclassFilteredCollection<>(getAllSelected(), p -> !p.isDeleted());
|
---|
641 | }
|
---|
642 |
|
---|
643 | /**
|
---|
644 | * Replies an unmodifiable collection of primitives currently selected
|
---|
645 | * in this dataset, including deleted ones. May be empty, but not null.
|
---|
646 | *
|
---|
647 | * @return unmodifiable collection of primitives
|
---|
648 | */
|
---|
649 | public Collection<OsmPrimitive> getAllSelected() {
|
---|
650 | Collection<OsmPrimitive> currentList;
|
---|
651 | synchronized (selectionLock) {
|
---|
652 | if (selectionSnapshot == null) {
|
---|
653 | selectionSnapshot = Collections.unmodifiableList(new ArrayList<>(selectedPrimitives));
|
---|
654 | }
|
---|
655 | currentList = selectionSnapshot;
|
---|
656 | }
|
---|
657 | return currentList;
|
---|
658 | }
|
---|
659 |
|
---|
660 | /**
|
---|
661 | * Returns selected nodes.
|
---|
662 | * @return selected nodes
|
---|
663 | */
|
---|
664 | public Collection<Node> getSelectedNodes() {
|
---|
665 | return new SubclassFilteredCollection<>(getSelected(), Node.class::isInstance);
|
---|
666 | }
|
---|
667 |
|
---|
668 | /**
|
---|
669 | * Returns selected ways.
|
---|
670 | * @return selected ways
|
---|
671 | */
|
---|
672 | public Collection<Way> getSelectedWays() {
|
---|
673 | return new SubclassFilteredCollection<>(getSelected(), Way.class::isInstance);
|
---|
674 | }
|
---|
675 |
|
---|
676 | /**
|
---|
677 | * Returns selected relations.
|
---|
678 | * @return selected relations
|
---|
679 | */
|
---|
680 | public Collection<Relation> getSelectedRelations() {
|
---|
681 | return new SubclassFilteredCollection<>(getSelected(), Relation.class::isInstance);
|
---|
682 | }
|
---|
683 |
|
---|
684 | /**
|
---|
685 | * Determines whether the selection is empty or not
|
---|
686 | * @return whether the selection is empty or not
|
---|
687 | */
|
---|
688 | public boolean selectionEmpty() {
|
---|
689 | return selectedPrimitives.isEmpty();
|
---|
690 | }
|
---|
691 |
|
---|
692 | /**
|
---|
693 | * Determines whether the given primitive is selected or not
|
---|
694 | * @param osm the primitive
|
---|
695 | * @return whether {@code osm} is selected or not
|
---|
696 | */
|
---|
697 | public boolean isSelected(OsmPrimitive osm) {
|
---|
698 | return selectedPrimitives.contains(osm);
|
---|
699 | }
|
---|
700 |
|
---|
701 | /**
|
---|
702 | * Toggles the selected state of the given collection of primitives.
|
---|
703 | * @param osm The primitives to toggle
|
---|
704 | */
|
---|
705 | public void toggleSelected(Collection<? extends PrimitiveId> osm) {
|
---|
706 | boolean changed = false;
|
---|
707 | synchronized (selectionLock) {
|
---|
708 | for (PrimitiveId o : osm) {
|
---|
709 | changed = changed | this.dotoggleSelected(o);
|
---|
710 | }
|
---|
711 | if (changed) {
|
---|
712 | selectionSnapshot = null;
|
---|
713 | }
|
---|
714 | }
|
---|
715 | if (changed) {
|
---|
716 | fireSelectionChanged();
|
---|
717 | }
|
---|
718 | }
|
---|
719 |
|
---|
720 | /**
|
---|
721 | * Toggles the selected state of the given collection of primitives.
|
---|
722 | * @param osm The primitives to toggle
|
---|
723 | */
|
---|
724 | public void toggleSelected(PrimitiveId... osm) {
|
---|
725 | toggleSelected(Arrays.asList(osm));
|
---|
726 | }
|
---|
727 |
|
---|
728 | private boolean dotoggleSelected(PrimitiveId primitiveId) {
|
---|
729 | OsmPrimitive primitive = getPrimitiveByIdChecked(primitiveId);
|
---|
730 | if (primitive == null)
|
---|
731 | return false;
|
---|
732 | if (!selectedPrimitives.remove(primitive)) {
|
---|
733 | selectedPrimitives.add(primitive);
|
---|
734 | }
|
---|
735 | selectionSnapshot = null;
|
---|
736 | return true;
|
---|
737 | }
|
---|
738 |
|
---|
739 | /**
|
---|
740 | * set what virtual nodes should be highlighted. Requires a Collection of
|
---|
741 | * *WaySegments* to avoid a VirtualNode class that wouldn't have much use
|
---|
742 | * otherwise.
|
---|
743 | * @param waySegments Collection of way segments
|
---|
744 | */
|
---|
745 | public void setHighlightedVirtualNodes(Collection<WaySegment> waySegments) {
|
---|
746 | if (highlightedVirtualNodes.isEmpty() && waySegments.isEmpty())
|
---|
747 | return;
|
---|
748 |
|
---|
749 | highlightedVirtualNodes = waySegments;
|
---|
750 | fireHighlightingChanged();
|
---|
751 | }
|
---|
752 |
|
---|
753 | /**
|
---|
754 | * set what virtual ways should be highlighted.
|
---|
755 | * @param waySegments Collection of way segments
|
---|
756 | */
|
---|
757 | public void setHighlightedWaySegments(Collection<WaySegment> waySegments) {
|
---|
758 | if (highlightedWaySegments.isEmpty() && waySegments.isEmpty())
|
---|
759 | return;
|
---|
760 |
|
---|
761 | highlightedWaySegments = waySegments;
|
---|
762 | fireHighlightingChanged();
|
---|
763 | }
|
---|
764 |
|
---|
765 | /**
|
---|
766 | * Sets the current selection to the primitives in <code>selection</code>.
|
---|
767 | * Notifies all {@link SelectionChangedListener} if <code>fireSelectionChangeEvent</code> is true.
|
---|
768 | *
|
---|
769 | * @param selection the selection
|
---|
770 | * @param fireSelectionChangeEvent true, if the selection change listeners are to be notified; false, otherwise
|
---|
771 | */
|
---|
772 | public void setSelected(Collection<? extends PrimitiveId> selection, boolean fireSelectionChangeEvent) {
|
---|
773 | boolean changed;
|
---|
774 | synchronized (selectionLock) {
|
---|
775 | Set<OsmPrimitive> oldSelection = new LinkedHashSet<>(selectedPrimitives);
|
---|
776 | selectedPrimitives = new LinkedHashSet<>();
|
---|
777 | addSelected(selection, false);
|
---|
778 | changed = !oldSelection.equals(selectedPrimitives);
|
---|
779 | if (changed) {
|
---|
780 | selectionSnapshot = null;
|
---|
781 | }
|
---|
782 | }
|
---|
783 |
|
---|
784 | if (changed && fireSelectionChangeEvent) {
|
---|
785 | // If selection is not empty then event was already fired in addSelecteds
|
---|
786 | fireSelectionChanged();
|
---|
787 | }
|
---|
788 | }
|
---|
789 |
|
---|
790 | /**
|
---|
791 | * Sets the current selection to the primitives in <code>selection</code>
|
---|
792 | * and notifies all {@link SelectionChangedListener}.
|
---|
793 | *
|
---|
794 | * @param selection the selection
|
---|
795 | */
|
---|
796 | public void setSelected(Collection<? extends PrimitiveId> selection) {
|
---|
797 | setSelected(selection, true /* fire selection change event */);
|
---|
798 | }
|
---|
799 |
|
---|
800 | /**
|
---|
801 | * Sets the current selection to the primitives in <code>osm</code>
|
---|
802 | * and notifies all {@link SelectionChangedListener}.
|
---|
803 | *
|
---|
804 | * @param osm the primitives to set
|
---|
805 | */
|
---|
806 | public void setSelected(PrimitiveId... osm) {
|
---|
807 | if (osm.length == 1 && osm[0] == null) {
|
---|
808 | setSelected();
|
---|
809 | return;
|
---|
810 | }
|
---|
811 | List<PrimitiveId> list = Arrays.asList(osm);
|
---|
812 | setSelected(list);
|
---|
813 | }
|
---|
814 |
|
---|
815 | /**
|
---|
816 | * Adds the primitives in <code>selection</code> to the current selection
|
---|
817 | * and notifies all {@link SelectionChangedListener}.
|
---|
818 | *
|
---|
819 | * @param selection the selection
|
---|
820 | */
|
---|
821 | public void addSelected(Collection<? extends PrimitiveId> selection) {
|
---|
822 | addSelected(selection, true /* fire selection change event */);
|
---|
823 | }
|
---|
824 |
|
---|
825 | /**
|
---|
826 | * Adds the primitives in <code>osm</code> to the current selection
|
---|
827 | * and notifies all {@link SelectionChangedListener}.
|
---|
828 | *
|
---|
829 | * @param osm the primitives to add
|
---|
830 | */
|
---|
831 | public void addSelected(PrimitiveId... osm) {
|
---|
832 | addSelected(Arrays.asList(osm));
|
---|
833 | }
|
---|
834 |
|
---|
835 | /**
|
---|
836 | * Adds the primitives in <code>selection</code> to the current selection.
|
---|
837 | * Notifies all {@link SelectionChangedListener} if <code>fireSelectionChangeEvent</code> is true.
|
---|
838 | *
|
---|
839 | * @param selection the selection
|
---|
840 | * @param fireSelectionChangeEvent true, if the selection change listeners are to be notified; false, otherwise
|
---|
841 | * @return if the selection was changed in the process
|
---|
842 | */
|
---|
843 | private boolean addSelected(Collection<? extends PrimitiveId> selection, boolean fireSelectionChangeEvent) {
|
---|
844 | boolean changed = false;
|
---|
845 | synchronized (selectionLock) {
|
---|
846 | for (PrimitiveId id: selection) {
|
---|
847 | OsmPrimitive primitive = getPrimitiveByIdChecked(id);
|
---|
848 | if (primitive != null) {
|
---|
849 | changed = changed | selectedPrimitives.add(primitive);
|
---|
850 | }
|
---|
851 | }
|
---|
852 | if (changed) {
|
---|
853 | selectionSnapshot = null;
|
---|
854 | }
|
---|
855 | }
|
---|
856 | if (fireSelectionChangeEvent && changed) {
|
---|
857 | fireSelectionChanged();
|
---|
858 | }
|
---|
859 | return changed;
|
---|
860 | }
|
---|
861 |
|
---|
862 | /**
|
---|
863 | * clear all highlights of virtual nodes
|
---|
864 | */
|
---|
865 | public void clearHighlightedVirtualNodes() {
|
---|
866 | setHighlightedVirtualNodes(new ArrayList<WaySegment>());
|
---|
867 | }
|
---|
868 |
|
---|
869 | /**
|
---|
870 | * clear all highlights of way segments
|
---|
871 | */
|
---|
872 | public void clearHighlightedWaySegments() {
|
---|
873 | setHighlightedWaySegments(new ArrayList<WaySegment>());
|
---|
874 | }
|
---|
875 |
|
---|
876 | /**
|
---|
877 | * Removes the selection from every value in the collection.
|
---|
878 | * @param osm The collection of ids to remove the selection from.
|
---|
879 | */
|
---|
880 | public void clearSelection(PrimitiveId... osm) {
|
---|
881 | clearSelection(Arrays.asList(osm));
|
---|
882 | }
|
---|
883 |
|
---|
884 | /**
|
---|
885 | * Removes the selection from every value in the collection.
|
---|
886 | * @param list The collection of ids to remove the selection from.
|
---|
887 | */
|
---|
888 | public void clearSelection(Collection<? extends PrimitiveId> list) {
|
---|
889 | boolean changed = false;
|
---|
890 | synchronized (selectionLock) {
|
---|
891 | for (PrimitiveId id:list) {
|
---|
892 | OsmPrimitive primitive = getPrimitiveById(id);
|
---|
893 | if (primitive != null) {
|
---|
894 | changed = changed | selectedPrimitives.remove(primitive);
|
---|
895 | }
|
---|
896 | }
|
---|
897 | if (changed) {
|
---|
898 | selectionSnapshot = null;
|
---|
899 | }
|
---|
900 | }
|
---|
901 | if (changed) {
|
---|
902 | fireSelectionChanged();
|
---|
903 | }
|
---|
904 | }
|
---|
905 |
|
---|
906 | /**
|
---|
907 | * Clears the current selection.
|
---|
908 | */
|
---|
909 | public void clearSelection() {
|
---|
910 | if (!selectedPrimitives.isEmpty()) {
|
---|
911 | synchronized (selectionLock) {
|
---|
912 | selectedPrimitives.clear();
|
---|
913 | selectionSnapshot = null;
|
---|
914 | }
|
---|
915 | fireSelectionChanged();
|
---|
916 | }
|
---|
917 | }
|
---|
918 |
|
---|
919 | @Override
|
---|
920 | public Collection<DataSource> getDataSources() {
|
---|
921 | return Collections.unmodifiableCollection(dataSources);
|
---|
922 | }
|
---|
923 |
|
---|
924 | /**
|
---|
925 | * Returns a primitive with a given id from the data set. null, if no such primitive exists
|
---|
926 | *
|
---|
927 | * @param id uniqueId of the primitive. Might be < 0 for newly created primitives
|
---|
928 | * @param type the type of the primitive. Must not be null.
|
---|
929 | * @return the primitive
|
---|
930 | * @throws NullPointerException if type is null
|
---|
931 | */
|
---|
932 | public OsmPrimitive getPrimitiveById(long id, OsmPrimitiveType type) {
|
---|
933 | return getPrimitiveById(new SimplePrimitiveId(id, type));
|
---|
934 | }
|
---|
935 |
|
---|
936 | /**
|
---|
937 | * Returns a primitive with a given id from the data set. null, if no such primitive exists
|
---|
938 | *
|
---|
939 | * @param primitiveId type and uniqueId of the primitive. Might be < 0 for newly created primitives
|
---|
940 | * @return the primitive
|
---|
941 | */
|
---|
942 | public OsmPrimitive getPrimitiveById(PrimitiveId primitiveId) {
|
---|
943 | return primitiveId != null ? primitivesMap.get(primitiveId) : null;
|
---|
944 | }
|
---|
945 |
|
---|
946 | /**
|
---|
947 | * Show message and stack trace in log in case primitive is not found
|
---|
948 | * @param primitiveId primitive id to look for
|
---|
949 | * @return Primitive by id.
|
---|
950 | */
|
---|
951 | private OsmPrimitive getPrimitiveByIdChecked(PrimitiveId primitiveId) {
|
---|
952 | OsmPrimitive result = getPrimitiveById(primitiveId);
|
---|
953 | if (result == null && primitiveId != null) {
|
---|
954 | Main.warn(tr("JOSM expected to find primitive [{0} {1}] in dataset but it is not there. Please report this "
|
---|
955 | + "at {2}. This is not a critical error, it should be safe to continue in your work.",
|
---|
956 | primitiveId.getType(), Long.toString(primitiveId.getUniqueId()), Main.getJOSMWebsite()));
|
---|
957 | Main.error(new Exception());
|
---|
958 | }
|
---|
959 |
|
---|
960 | return result;
|
---|
961 | }
|
---|
962 |
|
---|
963 | private static void deleteWay(Way way) {
|
---|
964 | way.setNodes(null);
|
---|
965 | way.setDeleted(true);
|
---|
966 | }
|
---|
967 |
|
---|
968 | /**
|
---|
969 | * Removes all references from ways in this dataset to a particular node.
|
---|
970 | *
|
---|
971 | * @param node the node
|
---|
972 | * @return The set of ways that have been modified
|
---|
973 | */
|
---|
974 | public Set<Way> unlinkNodeFromWays(Node node) {
|
---|
975 | Set<Way> result = new HashSet<>();
|
---|
976 | beginUpdate();
|
---|
977 | try {
|
---|
978 | for (Way way: ways) {
|
---|
979 | List<Node> wayNodes = way.getNodes();
|
---|
980 | if (wayNodes.remove(node)) {
|
---|
981 | if (wayNodes.size() < 2) {
|
---|
982 | deleteWay(way);
|
---|
983 | } else {
|
---|
984 | way.setNodes(wayNodes);
|
---|
985 | }
|
---|
986 | result.add(way);
|
---|
987 | }
|
---|
988 | }
|
---|
989 | } finally {
|
---|
990 | endUpdate();
|
---|
991 | }
|
---|
992 | return result;
|
---|
993 | }
|
---|
994 |
|
---|
995 | /**
|
---|
996 | * removes all references from relations in this dataset to this primitive
|
---|
997 | *
|
---|
998 | * @param primitive the primitive
|
---|
999 | * @return The set of relations that have been modified
|
---|
1000 | */
|
---|
1001 | public Set<Relation> unlinkPrimitiveFromRelations(OsmPrimitive primitive) {
|
---|
1002 | Set<Relation> result = new HashSet<>();
|
---|
1003 | beginUpdate();
|
---|
1004 | try {
|
---|
1005 | for (Relation relation : relations) {
|
---|
1006 | List<RelationMember> members = relation.getMembers();
|
---|
1007 |
|
---|
1008 | Iterator<RelationMember> it = members.iterator();
|
---|
1009 | boolean removed = false;
|
---|
1010 | while (it.hasNext()) {
|
---|
1011 | RelationMember member = it.next();
|
---|
1012 | if (member.getMember().equals(primitive)) {
|
---|
1013 | it.remove();
|
---|
1014 | removed = true;
|
---|
1015 | }
|
---|
1016 | }
|
---|
1017 |
|
---|
1018 | if (removed) {
|
---|
1019 | relation.setMembers(members);
|
---|
1020 | result.add(relation);
|
---|
1021 | }
|
---|
1022 | }
|
---|
1023 | } finally {
|
---|
1024 | endUpdate();
|
---|
1025 | }
|
---|
1026 | return result;
|
---|
1027 | }
|
---|
1028 |
|
---|
1029 | /**
|
---|
1030 | * Removes all references from other primitives to the referenced primitive.
|
---|
1031 | *
|
---|
1032 | * @param referencedPrimitive the referenced primitive
|
---|
1033 | * @return The set of primitives that have been modified
|
---|
1034 | */
|
---|
1035 | public Set<OsmPrimitive> unlinkReferencesToPrimitive(OsmPrimitive referencedPrimitive) {
|
---|
1036 | Set<OsmPrimitive> result = new HashSet<>();
|
---|
1037 | beginUpdate();
|
---|
1038 | try {
|
---|
1039 | if (referencedPrimitive instanceof Node) {
|
---|
1040 | result.addAll(unlinkNodeFromWays((Node) referencedPrimitive));
|
---|
1041 | }
|
---|
1042 | result.addAll(unlinkPrimitiveFromRelations(referencedPrimitive));
|
---|
1043 | } finally {
|
---|
1044 | endUpdate();
|
---|
1045 | }
|
---|
1046 | return result;
|
---|
1047 | }
|
---|
1048 |
|
---|
1049 | /**
|
---|
1050 | * Replies true if there is at least one primitive in this dataset with
|
---|
1051 | * {@link OsmPrimitive#isModified()} == <code>true</code>.
|
---|
1052 | *
|
---|
1053 | * @return true if there is at least one primitive in this dataset with
|
---|
1054 | * {@link OsmPrimitive#isModified()} == <code>true</code>.
|
---|
1055 | */
|
---|
1056 | public boolean isModified() {
|
---|
1057 | for (OsmPrimitive p: allPrimitives) {
|
---|
1058 | if (p.isModified())
|
---|
1059 | return true;
|
---|
1060 | }
|
---|
1061 | return false;
|
---|
1062 | }
|
---|
1063 |
|
---|
1064 | private void reindexNode(Node node, LatLon newCoor, EastNorth eastNorth) {
|
---|
1065 | if (!nodes.remove(node))
|
---|
1066 | throw new JosmRuntimeException("Reindexing node failed to remove");
|
---|
1067 | node.setCoorInternal(newCoor, eastNorth);
|
---|
1068 | if (!nodes.add(node))
|
---|
1069 | throw new JosmRuntimeException("Reindexing node failed to add");
|
---|
1070 | for (OsmPrimitive primitive: node.getReferrers()) {
|
---|
1071 | if (primitive instanceof Way) {
|
---|
1072 | reindexWay((Way) primitive);
|
---|
1073 | } else {
|
---|
1074 | reindexRelation((Relation) primitive);
|
---|
1075 | }
|
---|
1076 | }
|
---|
1077 | }
|
---|
1078 |
|
---|
1079 | private void reindexWay(Way way) {
|
---|
1080 | BBox before = way.getBBox();
|
---|
1081 | if (!ways.remove(way))
|
---|
1082 | throw new JosmRuntimeException("Reindexing way failed to remove");
|
---|
1083 | way.updatePosition();
|
---|
1084 | if (!ways.add(way))
|
---|
1085 | throw new JosmRuntimeException("Reindexing way failed to add");
|
---|
1086 | if (!way.getBBox().equals(before)) {
|
---|
1087 | for (OsmPrimitive primitive: way.getReferrers()) {
|
---|
1088 | reindexRelation((Relation) primitive);
|
---|
1089 | }
|
---|
1090 | }
|
---|
1091 | }
|
---|
1092 |
|
---|
1093 | private static void reindexRelation(Relation relation) {
|
---|
1094 | BBox before = relation.getBBox();
|
---|
1095 | relation.updatePosition();
|
---|
1096 | if (!before.equals(relation.getBBox())) {
|
---|
1097 | for (OsmPrimitive primitive: relation.getReferrers()) {
|
---|
1098 | reindexRelation((Relation) primitive);
|
---|
1099 | }
|
---|
1100 | }
|
---|
1101 | }
|
---|
1102 |
|
---|
1103 | /**
|
---|
1104 | * Adds a new data set listener.
|
---|
1105 | * @param dsl The data set listener to add
|
---|
1106 | */
|
---|
1107 | public void addDataSetListener(DataSetListener dsl) {
|
---|
1108 | listeners.addIfAbsent(dsl);
|
---|
1109 | }
|
---|
1110 |
|
---|
1111 | /**
|
---|
1112 | * Removes a data set listener.
|
---|
1113 | * @param dsl The data set listener to remove
|
---|
1114 | */
|
---|
1115 | public void removeDataSetListener(DataSetListener dsl) {
|
---|
1116 | listeners.remove(dsl);
|
---|
1117 | }
|
---|
1118 |
|
---|
1119 | /**
|
---|
1120 | * Can be called before bigger changes on dataset. Events are disabled until {@link #endUpdate()}.
|
---|
1121 | * {@link DataSetListener#dataChanged(DataChangedEvent event)} event is triggered after end of changes
|
---|
1122 | * <br>
|
---|
1123 | * Typical usecase should look like this:
|
---|
1124 | * <pre>
|
---|
1125 | * ds.beginUpdate();
|
---|
1126 | * try {
|
---|
1127 | * ...
|
---|
1128 | * } finally {
|
---|
1129 | * ds.endUpdate();
|
---|
1130 | * }
|
---|
1131 | * </pre>
|
---|
1132 | */
|
---|
1133 | public void beginUpdate() {
|
---|
1134 | lock.writeLock().lock();
|
---|
1135 | updateCount++;
|
---|
1136 | }
|
---|
1137 |
|
---|
1138 | /**
|
---|
1139 | * @see DataSet#beginUpdate()
|
---|
1140 | */
|
---|
1141 | public void endUpdate() {
|
---|
1142 | if (updateCount > 0) {
|
---|
1143 | updateCount--;
|
---|
1144 | List<AbstractDatasetChangedEvent> eventsToFire = Collections.emptyList();
|
---|
1145 | if (updateCount == 0) {
|
---|
1146 | eventsToFire = new ArrayList<>(cachedEvents);
|
---|
1147 | cachedEvents.clear();
|
---|
1148 | }
|
---|
1149 |
|
---|
1150 | if (!eventsToFire.isEmpty()) {
|
---|
1151 | lock.readLock().lock();
|
---|
1152 | lock.writeLock().unlock();
|
---|
1153 | try {
|
---|
1154 | if (eventsToFire.size() < MAX_SINGLE_EVENTS) {
|
---|
1155 | for (AbstractDatasetChangedEvent event: eventsToFire) {
|
---|
1156 | fireEventToListeners(event);
|
---|
1157 | }
|
---|
1158 | } else if (eventsToFire.size() == MAX_EVENTS) {
|
---|
1159 | fireEventToListeners(new DataChangedEvent(this));
|
---|
1160 | } else {
|
---|
1161 | fireEventToListeners(new DataChangedEvent(this, eventsToFire));
|
---|
1162 | }
|
---|
1163 | } finally {
|
---|
1164 | lock.readLock().unlock();
|
---|
1165 | }
|
---|
1166 | } else {
|
---|
1167 | lock.writeLock().unlock();
|
---|
1168 | }
|
---|
1169 |
|
---|
1170 | } else
|
---|
1171 | throw new AssertionError("endUpdate called without beginUpdate");
|
---|
1172 | }
|
---|
1173 |
|
---|
1174 | private void fireEventToListeners(AbstractDatasetChangedEvent event) {
|
---|
1175 | for (DataSetListener listener: listeners) {
|
---|
1176 | event.fire(listener);
|
---|
1177 | }
|
---|
1178 | }
|
---|
1179 |
|
---|
1180 | private void fireEvent(AbstractDatasetChangedEvent event) {
|
---|
1181 | if (updateCount == 0)
|
---|
1182 | throw new AssertionError("dataset events can be fired only when dataset is locked");
|
---|
1183 | if (cachedEvents.size() < MAX_EVENTS) {
|
---|
1184 | cachedEvents.add(event);
|
---|
1185 | }
|
---|
1186 | }
|
---|
1187 |
|
---|
1188 | void firePrimitivesAdded(Collection<? extends OsmPrimitive> added, boolean wasIncomplete) {
|
---|
1189 | fireEvent(new PrimitivesAddedEvent(this, added, wasIncomplete));
|
---|
1190 | }
|
---|
1191 |
|
---|
1192 | void firePrimitivesRemoved(Collection<? extends OsmPrimitive> removed, boolean wasComplete) {
|
---|
1193 | fireEvent(new PrimitivesRemovedEvent(this, removed, wasComplete));
|
---|
1194 | }
|
---|
1195 |
|
---|
1196 | void fireTagsChanged(OsmPrimitive prim, Map<String, String> originalKeys) {
|
---|
1197 | fireEvent(new TagsChangedEvent(this, prim, originalKeys));
|
---|
1198 | }
|
---|
1199 |
|
---|
1200 | void fireRelationMembersChanged(Relation r) {
|
---|
1201 | reindexRelation(r);
|
---|
1202 | fireEvent(new RelationMembersChangedEvent(this, r));
|
---|
1203 | }
|
---|
1204 |
|
---|
1205 | void fireNodeMoved(Node node, LatLon newCoor, EastNorth eastNorth) {
|
---|
1206 | reindexNode(node, newCoor, eastNorth);
|
---|
1207 | fireEvent(new NodeMovedEvent(this, node));
|
---|
1208 | }
|
---|
1209 |
|
---|
1210 | void fireWayNodesChanged(Way way) {
|
---|
1211 | reindexWay(way);
|
---|
1212 | fireEvent(new WayNodesChangedEvent(this, way));
|
---|
1213 | }
|
---|
1214 |
|
---|
1215 | void fireChangesetIdChanged(OsmPrimitive primitive, int oldChangesetId, int newChangesetId) {
|
---|
1216 | fireEvent(new ChangesetIdChangedEvent(this, Collections.singletonList(primitive), oldChangesetId, newChangesetId));
|
---|
1217 | }
|
---|
1218 |
|
---|
1219 | void firePrimitiveFlagsChanged(OsmPrimitive primitive) {
|
---|
1220 | fireEvent(new PrimitiveFlagsChangedEvent(this, primitive));
|
---|
1221 | }
|
---|
1222 |
|
---|
1223 | void fireHighlightingChanged() {
|
---|
1224 | highlightUpdateCount++;
|
---|
1225 | }
|
---|
1226 |
|
---|
1227 | /**
|
---|
1228 | * Invalidates the internal cache of projected east/north coordinates.
|
---|
1229 | *
|
---|
1230 | * This method can be invoked after the globally configured projection method
|
---|
1231 | * changed.
|
---|
1232 | */
|
---|
1233 | public void invalidateEastNorthCache() {
|
---|
1234 | if (Main.getProjection() == null) return; // sanity check
|
---|
1235 | try {
|
---|
1236 | beginUpdate();
|
---|
1237 | for (Node n: Utils.filteredCollection(allPrimitives, Node.class)) {
|
---|
1238 | n.invalidateEastNorthCache();
|
---|
1239 | }
|
---|
1240 | } finally {
|
---|
1241 | endUpdate();
|
---|
1242 | }
|
---|
1243 | }
|
---|
1244 |
|
---|
1245 | /**
|
---|
1246 | * Cleanups all deleted primitives (really delete them from the dataset).
|
---|
1247 | */
|
---|
1248 | public void cleanupDeletedPrimitives() {
|
---|
1249 | beginUpdate();
|
---|
1250 | try {
|
---|
1251 | boolean changed = cleanupDeleted(nodes.iterator());
|
---|
1252 | if (cleanupDeleted(ways.iterator())) {
|
---|
1253 | changed = true;
|
---|
1254 | }
|
---|
1255 | if (cleanupDeleted(relations.iterator())) {
|
---|
1256 | changed = true;
|
---|
1257 | }
|
---|
1258 | if (changed) {
|
---|
1259 | fireSelectionChanged();
|
---|
1260 | }
|
---|
1261 | } finally {
|
---|
1262 | endUpdate();
|
---|
1263 | }
|
---|
1264 | }
|
---|
1265 |
|
---|
1266 | private boolean cleanupDeleted(Iterator<? extends OsmPrimitive> it) {
|
---|
1267 | boolean changed = false;
|
---|
1268 | synchronized (selectionLock) {
|
---|
1269 | while (it.hasNext()) {
|
---|
1270 | OsmPrimitive primitive = it.next();
|
---|
1271 | if (primitive.isDeleted() && (!primitive.isVisible() || primitive.isNew())) {
|
---|
1272 | selectedPrimitives.remove(primitive);
|
---|
1273 | selectionSnapshot = null;
|
---|
1274 | allPrimitives.remove(primitive);
|
---|
1275 | primitive.setDataset(null);
|
---|
1276 | changed = true;
|
---|
1277 | it.remove();
|
---|
1278 | }
|
---|
1279 | }
|
---|
1280 | if (changed) {
|
---|
1281 | selectionSnapshot = null;
|
---|
1282 | }
|
---|
1283 | }
|
---|
1284 | return changed;
|
---|
1285 | }
|
---|
1286 |
|
---|
1287 | /**
|
---|
1288 | * Removes all primitives from the dataset and resets the currently selected primitives
|
---|
1289 | * to the empty collection. Also notifies selection change listeners if necessary.
|
---|
1290 | *
|
---|
1291 | */
|
---|
1292 | public void clear() {
|
---|
1293 | beginUpdate();
|
---|
1294 | try {
|
---|
1295 | clearSelection();
|
---|
1296 | for (OsmPrimitive primitive:allPrimitives) {
|
---|
1297 | primitive.setDataset(null);
|
---|
1298 | }
|
---|
1299 | nodes.clear();
|
---|
1300 | ways.clear();
|
---|
1301 | relations.clear();
|
---|
1302 | allPrimitives.clear();
|
---|
1303 | } finally {
|
---|
1304 | endUpdate();
|
---|
1305 | }
|
---|
1306 | }
|
---|
1307 |
|
---|
1308 | /**
|
---|
1309 | * Marks all "invisible" objects as deleted. These objects should be always marked as
|
---|
1310 | * deleted when downloaded from the server. They can be undeleted later if necessary.
|
---|
1311 | *
|
---|
1312 | */
|
---|
1313 | public void deleteInvisible() {
|
---|
1314 | for (OsmPrimitive primitive:allPrimitives) {
|
---|
1315 | if (!primitive.isVisible()) {
|
---|
1316 | primitive.setDeleted(true);
|
---|
1317 | }
|
---|
1318 | }
|
---|
1319 | }
|
---|
1320 |
|
---|
1321 | /**
|
---|
1322 | * Moves all primitives and datasources from DataSet "from" to this DataSet.
|
---|
1323 | * @param from The source DataSet
|
---|
1324 | */
|
---|
1325 | public void mergeFrom(DataSet from) {
|
---|
1326 | mergeFrom(from, null);
|
---|
1327 | }
|
---|
1328 |
|
---|
1329 | /**
|
---|
1330 | * Moves all primitives and datasources from DataSet "from" to this DataSet.
|
---|
1331 | * @param from The source DataSet
|
---|
1332 | * @param progressMonitor The progress monitor
|
---|
1333 | */
|
---|
1334 | public void mergeFrom(DataSet from, ProgressMonitor progressMonitor) {
|
---|
1335 | if (from != null) {
|
---|
1336 | new DataSetMerger(this, from).merge(progressMonitor);
|
---|
1337 | dataSources.addAll(from.dataSources);
|
---|
1338 | from.dataSources.clear();
|
---|
1339 | }
|
---|
1340 | }
|
---|
1341 |
|
---|
1342 | /* --------------------------------------------------------------------------------- */
|
---|
1343 | /* interface ProjectionChangeListner */
|
---|
1344 | /* --------------------------------------------------------------------------------- */
|
---|
1345 | @Override
|
---|
1346 | public void projectionChanged(Projection oldValue, Projection newValue) {
|
---|
1347 | invalidateEastNorthCache();
|
---|
1348 | }
|
---|
1349 |
|
---|
1350 | public ProjectionBounds getDataSourceBoundingBox() {
|
---|
1351 | BoundingXYVisitor bbox = new BoundingXYVisitor();
|
---|
1352 | for (DataSource source : dataSources) {
|
---|
1353 | bbox.visit(source.bounds);
|
---|
1354 | }
|
---|
1355 | if (bbox.hasExtend()) {
|
---|
1356 | return bbox.getBounds();
|
---|
1357 | }
|
---|
1358 | return null;
|
---|
1359 | }
|
---|
1360 |
|
---|
1361 | }
|
---|