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