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