source: josm/trunk/src/org/openstreetmap/josm/data/osm/DataSet.java@ 11294

Last change on this file since 11294 was 11269, checked in by michael2402, 7 years ago

Fix #13361: Use a more consistent invalid bbox for primitives.

Patch by Gerd Petermann

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