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