Changeset 3414 in josm for trunk/src/org/openstreetmap
- Timestamp:
- 2010-08-04T08:29:53+02:00 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/data/osm/DataSet.java
r3408 r3414 81 81 82 82 private final ReadWriteLock lock = new ReentrantReadWriteLock(); 83 private final Object selectionLock = new Object(); 83 84 84 85 public Lock getReadLock() { … … 301 302 relations.remove(primitive); 302 303 } 303 selectedPrimitives.remove(primitive); 304 synchronized (selectionLock) { 305 selectedPrimitives.remove(primitive); 306 selectionSnapshot = null; 307 } 304 308 allPrimitives.remove(primitive); 305 309 primitive.setDataset(null); … … 329 333 public void fireSelectionChanged(){ 330 334 synchronized (selListeners) { 331 List<? extends OsmPrimitive> currentSelection = Collections.unmodifiableList(new ArrayList<OsmPrimitive>(selectedPrimitives));335 Collection<? extends OsmPrimitive> currentSelection = getSelected(); 332 336 for (SelectionChangedListener l : selListeners) { 333 337 l.selectionChanged(currentSelection); … … 336 340 } 337 341 338 LinkedHashSet<OsmPrimitive> selectedPrimitives = new LinkedHashSet<OsmPrimitive>(); 342 private LinkedHashSet<OsmPrimitive> selectedPrimitives = new LinkedHashSet<OsmPrimitive>(); 343 private Collection<OsmPrimitive> selectionSnapshot; 339 344 340 345 public Collection<OsmPrimitive> getSelectedNodesAndWays() { 341 Collection<OsmPrimitive> sel = new LinkedList<OsmPrimitive>(); 342 for (OsmPrimitive osm : selectedPrimitives) { 343 if (osm instanceof Way || 344 osm instanceof Node) { 345 sel.add(osm); 346 } 347 } 348 return sel; 346 return new DatasetCollection<OsmPrimitive>(getSelected(), new Predicate<OsmPrimitive>() { 347 @Override 348 public boolean evaluate(OsmPrimitive primitive) { 349 return primitive instanceof Node || primitive instanceof Way; 350 } 351 }); 349 352 } 350 353 … … 356 359 */ 357 360 public Collection<OsmPrimitive> getSelected() { 358 return Collections.unmodifiableSet(selectedPrimitives); 361 Collection<OsmPrimitive> currentList; 362 synchronized (selectionLock) { 363 if (selectionSnapshot == null) { 364 selectionSnapshot = Collections.unmodifiableList(new ArrayList<OsmPrimitive>(selectedPrimitives)); 365 } 366 currentList = selectionSnapshot; 367 } 368 return currentList; 359 369 } 360 370 … … 363 373 */ 364 374 public Collection<Node> getSelectedNodes() { 365 List<Node> result = new ArrayList<Node>(selectedPrimitives.size()); 366 for (OsmPrimitive primitive:selectedPrimitives) { 367 if (primitive instanceof Node) { 368 result.add((Node)primitive); 369 } 370 } 371 return result; 375 return new DatasetCollection<Node>(getSelected(), OsmPrimitive.nodePredicate); 372 376 } 373 377 … … 376 380 */ 377 381 public Collection<Way> getSelectedWays() { 378 List<Way> result = new ArrayList<Way>(selectedPrimitives.size()); 379 for (OsmPrimitive primitive:selectedPrimitives) { 380 if (primitive instanceof Way) { 381 result.add((Way)primitive); 382 } 383 } 384 return result; 382 return new DatasetCollection<Way>(getSelected(), OsmPrimitive.wayPredicate); 385 383 } 386 384 … … 389 387 */ 390 388 public Collection<Relation> getSelectedRelations() { 391 List<Relation> result = new ArrayList<Relation>(selectedPrimitives.size() / 10); 392 for (OsmPrimitive primitive:selectedPrimitives) { 393 if (primitive instanceof Relation) { 394 result.add((Relation)primitive); 395 } 396 } 397 return result; 389 return new DatasetCollection<Relation>(getSelected(), OsmPrimitive.relationPredicate); 398 390 } 399 391 … … 404 396 public void toggleSelected(Collection<? extends PrimitiveId> osm) { 405 397 boolean changed = false; 406 for (PrimitiveId o : osm) { 407 changed = changed | this.__toggleSelected(o); 398 synchronized (selectionLock) { 399 for (PrimitiveId o : osm) { 400 changed = changed | this.__toggleSelected(o); 401 } 402 if (changed) { 403 selectionSnapshot = null; 404 } 408 405 } 409 406 if (changed) { … … 421 418 selectedPrimitives.add(primitive); 422 419 } 420 selectionSnapshot = null; 423 421 return true; 424 422 } … … 432 430 */ 433 431 public void setSelected(Collection<? extends PrimitiveId> selection, boolean fireSelectionChangeEvent) { 434 boolean wasEmpty = selectedPrimitives.isEmpty(); 435 selectedPrimitives = new LinkedHashSet<OsmPrimitive>(); 436 addSelected(selection, fireSelectionChangeEvent); 432 boolean wasEmpty; 433 synchronized (selectionLock) { 434 wasEmpty = selectedPrimitives.isEmpty(); 435 selectedPrimitives = new LinkedHashSet<OsmPrimitive>(); 436 addSelected(selection, fireSelectionChangeEvent); 437 if (!wasEmpty && selectedPrimitives.isEmpty()) { 438 selectionSnapshot = null; 439 } 440 } 441 437 442 if (!wasEmpty && selectedPrimitives.isEmpty() && fireSelectionChangeEvent) { 443 // If selection is not empty then event was already fired in addSelecteds 438 444 fireSelectionChanged(); 439 445 } … … 482 488 public void addSelected(Collection<? extends PrimitiveId> selection, boolean fireSelectionChangeEvent) { 483 489 boolean changed = false; 484 for (PrimitiveId id: selection) { 485 OsmPrimitive primitive = getPrimitiveByIdChecked(id); 486 if (primitive != null) { 487 changed = changed | selectedPrimitives.add(primitive); 490 synchronized (selectionLock) { 491 for (PrimitiveId id: selection) { 492 OsmPrimitive primitive = getPrimitiveByIdChecked(id); 493 if (primitive != null) { 494 changed = changed | selectedPrimitives.add(primitive); 495 } 496 } 497 if (changed) { 498 selectionSnapshot = null; 488 499 } 489 500 } … … 502 513 public void clearSelection(Collection<? extends PrimitiveId> list) { 503 514 boolean changed = false; 504 for (PrimitiveId id:list) { 505 OsmPrimitive primitive = getPrimitiveById(id); 506 if (primitive != null) { 507 changed = changed | selectedPrimitives.remove(primitive); 515 synchronized (selectionLock) { 516 for (PrimitiveId id:list) { 517 OsmPrimitive primitive = getPrimitiveById(id); 518 if (primitive != null) { 519 changed = changed | selectedPrimitives.remove(primitive); 520 } 521 } 522 if (changed) { 523 selectionSnapshot = null; 508 524 } 509 525 } … … 514 530 public void clearSelection() { 515 531 if (!selectedPrimitives.isEmpty()) { 516 selectedPrimitives.clear(); 532 synchronized (selectionLock) { 533 selectedPrimitives.clear(); 534 selectionSnapshot = null; 535 } 517 536 fireSelectionChanged(); 518 537 } … … 887 906 private boolean cleanupDeleted(Iterator<? extends OsmPrimitive> it) { 888 907 boolean changed = false; 889 while (it.hasNext()) { 890 OsmPrimitive primitive = it.next(); 891 if (primitive.isDeleted()) { 892 selectedPrimitives.remove(primitive); 893 allPrimitives.remove(primitive); 894 primitive.setDataset(null); 895 changed = true; 896 it.remove(); 908 synchronized (selectionLock) { 909 while (it.hasNext()) { 910 OsmPrimitive primitive = it.next(); 911 if (primitive.isDeleted()) { 912 selectedPrimitives.remove(primitive); 913 selectionSnapshot = null; 914 allPrimitives.remove(primitive); 915 primitive.setDataset(null); 916 changed = true; 917 it.remove(); 918 } 919 } 920 if (changed) { 921 selectionSnapshot = null; 897 922 } 898 923 }
Note:
See TracChangeset
for help on using the changeset viewer.