Changeset 8642 in josm
- Timestamp:
- 2015-08-05T19:48:13+02:00 (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/gui/MapView.java
r8631 r8642 32 32 import java.util.Set; 33 33 import java.util.concurrent.CopyOnWriteArrayList; 34 import java.util.concurrent.locks.ReentrantReadWriteLock;35 34 36 35 import javax.swing.AbstractButton; … … 202 201 */ 203 202 protected void fireActiveLayerChanged(Layer oldLayer, Layer newLayer) { 204 checkLayerLockNotHeld();205 203 for (LayerChangeListener l : layerChangeListeners) { 206 204 l.activeLayerChange(oldLayer, newLayer); … … 209 207 210 208 protected void fireLayerAdded(Layer newLayer) { 211 checkLayerLockNotHeld();212 209 for (MapView.LayerChangeListener l : MapView.layerChangeListeners) { 213 210 l.layerAdded(newLayer); … … 216 213 217 214 protected void fireLayerRemoved(Layer layer) { 218 checkLayerLockNotHeld();219 215 for (MapView.LayerChangeListener l : MapView.layerChangeListeners) { 220 216 l.layerRemoved(layer); … … 223 219 224 220 protected void fireEditLayerChanged(OsmDataLayer oldLayer, OsmDataLayer newLayer) { 225 checkLayerLockNotHeld();226 221 for (EditLayerChangeListener l : editLayerChangeListeners) { 227 222 l.editLayerChanged(oldLayer, newLayer); 228 }229 }230 231 /**232 * This is a simple invariant check that tests if the {@link #layerLock} is not write locked.233 * This should be the case whenever a layer listener is invoked.234 */235 private void checkLayerLockNotHeld() {236 if (layerLock.isWriteLockedByCurrentThread()) {237 Main.warn("layerLock is write-held while a listener was called.");238 223 } 239 224 } … … 250 235 * The read lock is always held while those fields are read or while layer change listeners are fired. 251 236 */ 252 private final ReentrantReadWriteLock layerLock = new ReentrantReadWriteLock();237 //private final ReentrantReadWriteLock layerLock = new ReentrantReadWriteLock(); 253 238 254 239 /** … … 376 361 */ 377 362 protected void addGpxLayer(GpxLayer layer) { 378 layerLock.writeLock().lock(); 379 try { 363 synchronized (layers) { 380 364 if (layers.isEmpty()) { 381 365 layers.add(layer); … … 393 377 } 394 378 layers.add(0, layer); 395 } finally {396 layerLock.writeLock().unlock();397 379 } 398 380 } … … 409 391 OsmDataLayer oldEditLayer = editLayer; 410 392 411 layerLock.writeLock().lock(); 412 layerLock.readLock().lock(); 413 try { 414 try { 415 if (layer instanceof MarkerLayer && playHeadMarker == null) { 416 playHeadMarker = PlayHeadMarker.create(); 393 synchronized (layers) { 394 if (layer instanceof MarkerLayer && playHeadMarker == null) { 395 playHeadMarker = PlayHeadMarker.create(); 396 } 397 398 if (layer instanceof GpxLayer) { 399 addGpxLayer((GpxLayer) layer); 400 } else if (layers.isEmpty()) { 401 layers.add(layer); 402 } else if (layer.isBackgroundLayer()) { 403 int i = 0; 404 for (; i < layers.size(); i++) { 405 if (layers.get(i).isBackgroundLayer()) { 406 break; 407 } 417 408 } 418 419 if (layer instanceof GpxLayer) { 420 addGpxLayer((GpxLayer) layer); 421 } else if (layers.isEmpty()) { 422 layers.add(layer); 423 } else if (layer.isBackgroundLayer()) { 424 int i = 0; 425 for (; i < layers.size(); i++) { 426 if (layers.get(i).isBackgroundLayer()) { 427 break; 428 } 429 } 430 layers.add(i, layer); 431 } else { 432 layers.add(0, layer); 433 } 434 435 if (isOsmDataLayer || oldActiveLayer == null) { 436 // autoselect the new layer 437 listenersToFire.addAll(setActiveLayer(layer, true)); 438 } 439 } finally { 440 layerLock.writeLock().unlock(); 409 layers.add(i, layer); 410 } else { 411 layers.add(0, layer); 412 } 413 414 if (isOsmDataLayer || oldActiveLayer == null) { 415 // autoselect the new layer 416 listenersToFire.addAll(setActiveLayer(layer, true)); 441 417 } 442 418 … … 449 425 Main.addProjectionChangeListener(layer); 450 426 AudioPlayer.reset(); 451 } finally {452 layerLock.readLock().unlock();453 427 } 454 428 if (!listenersToFire.isEmpty()) { … … 459 433 @Override 460 434 protected DataSet getCurrentDataSet() { 461 layerLock.readLock().lock(); 462 try { 435 synchronized (layers) { 463 436 if (editLayer != null) 464 437 return editLayer.data; 465 438 else 466 439 return null; 467 } finally {468 layerLock.readLock().unlock();469 440 } 470 441 } … … 476 447 */ 477 448 public boolean isActiveLayerDrawable() { 478 layerLock.readLock().lock(); 479 try { 449 synchronized (layers) { 480 450 return editLayer != null; 481 } finally {482 layerLock.readLock().unlock();483 451 } 484 452 } … … 490 458 */ 491 459 public boolean isActiveLayerVisible() { 492 layerLock.readLock().lock(); 493 try { 460 synchronized (layers) { 494 461 return isActiveLayerDrawable() && editLayer.isVisible(); 495 } finally {496 layerLock.readLock().unlock();497 462 } 498 463 } … … 535 500 OsmDataLayer oldEditLayer = editLayer; 536 501 537 layerLock.writeLock().lock(); 538 layerLock.readLock().lock(); 539 try { 540 try { 541 List<Layer> layersList = new ArrayList<>(layers); 542 543 if (!layersList.remove(layer)) 544 return; 545 546 listenersToFire = setEditLayer(layersList); 547 548 if (layer == activeLayer) { 549 listenersToFire.addAll(setActiveLayer(determineNextActiveLayer(layersList), false)); 550 } 551 552 if (layer instanceof OsmDataLayer) { 553 ((OsmDataLayer) layer).removeLayerPropertyChangeListener(this); 554 } 555 556 layers.remove(layer); 557 Main.removeProjectionChangeListener(layer); 558 559 } finally { 560 layerLock.writeLock().unlock(); 561 } 502 synchronized (layers) { 503 List<Layer> layersList = new ArrayList<>(layers); 504 505 if (!layersList.remove(layer)) 506 return; 507 508 listenersToFire = setEditLayer(layersList); 509 510 if (layer == activeLayer) { 511 listenersToFire.addAll(setActiveLayer(determineNextActiveLayer(layersList), false)); 512 } 513 514 if (layer instanceof OsmDataLayer) { 515 ((OsmDataLayer) layer).removeLayerPropertyChangeListener(this); 516 } 517 518 layers.remove(layer); 519 Main.removeProjectionChangeListener(layer); 520 562 521 onActiveEditLayerChanged(oldActiveLayer, oldEditLayer, listenersToFire); 563 522 fireLayerRemoved(layer); … … 565 524 layer.destroy(); 566 525 AudioPlayer.reset(); 567 } finally {568 layerLock.readLock().unlock();569 526 } 570 527 repaint(); … … 606 563 OsmDataLayer oldEditLayer = editLayer; 607 564 608 layerLock.writeLock().lock(); 609 layerLock.readLock().lock(); 610 try { 611 try { 612 int curLayerPos = layers.indexOf(layer); 613 if (curLayerPos == -1) 614 throw new IllegalArgumentException(tr("Layer not in list.")); 615 if (pos == curLayerPos) 616 return; // already in place. 617 layers.remove(curLayerPos); 618 if (pos >= layers.size()) { 619 layers.add(layer); 620 } else { 621 layers.add(pos, layer); 622 } 623 listenersToFire = setEditLayer(layers); 624 } finally { 625 layerLock.writeLock().unlock(); 626 } 565 synchronized (layers) { 566 int curLayerPos = layers.indexOf(layer); 567 if (curLayerPos == -1) 568 throw new IllegalArgumentException(tr("Layer not in list.")); 569 if (pos == curLayerPos) 570 return; // already in place. 571 layers.remove(curLayerPos); 572 if (pos >= layers.size()) { 573 layers.add(layer); 574 } else { 575 layers.add(pos, layer); 576 } 577 listenersToFire = setEditLayer(layers); 627 578 onActiveEditLayerChanged(oldActiveLayer, oldEditLayer, listenersToFire); 628 579 AudioPlayer.reset(); 629 } finally {630 layerLock.readLock().unlock();631 580 } 632 581 repaint(); … … 641 590 public int getLayerPos(Layer layer) { 642 591 int curLayerPos; 643 layerLock.readLock().lock(); 644 try { 592 synchronized (layers) { 645 593 curLayerPos = layers.indexOf(layer); 646 } finally {647 layerLock.readLock().unlock();648 594 } 649 595 if (curLayerPos == -1) … … 662 608 */ 663 609 public List<Layer> getVisibleLayersInZOrder() { 664 layerLock.readLock().lock(); 665 try { 610 synchronized (layers) { 666 611 List<Layer> ret = new ArrayList<>(); 667 612 // This is set while we delay the addition of the active layer. … … 688 633 } 689 634 return ret; 690 } finally {691 layerLock.readLock().unlock();692 635 } 693 636 } … … 878 821 */ 879 822 public Collection<Layer> getAllLayers() { 880 layerLock.readLock().lock(); 881 try { 823 synchronized (layers) { 882 824 return Collections.unmodifiableCollection(new ArrayList<>(layers)); 883 } finally {884 layerLock.readLock().unlock();885 825 } 886 826 } … … 890 830 */ 891 831 public List<Layer> getAllLayersAsList() { 892 layerLock.readLock().lock(); 893 try { 832 synchronized (layers) { 894 833 return Collections.unmodifiableList(new ArrayList<>(layers)); 895 } finally {896 layerLock.readLock().unlock();897 834 } 898 835 } … … 919 856 */ 920 857 public int getNumLayers() { 921 layerLock.readLock().lock(); 922 try { 858 synchronized (layers) { 923 859 return layers.size(); 924 } finally {925 layerLock.readLock().unlock();926 860 } 927 861 } … … 987 921 */ 988 922 public void setActiveLayer(Layer layer) { 989 layerLock.writeLock().lock();990 layerLock.readLock().lock();991 923 EnumSet<LayerListenerType> listenersToFire; 992 Layer oldActiveLayer = activeLayer; 993 OsmDataLayer oldEditLayer = editLayer; 994 try { 995 try { 996 listenersToFire = setActiveLayer(layer, true); 997 } finally { 998 layerLock.writeLock().unlock(); 999 } 924 925 synchronized (layers) { 926 Layer oldActiveLayer = activeLayer; 927 OsmDataLayer oldEditLayer = editLayer; 928 listenersToFire = setActiveLayer(layer, true); 1000 929 onActiveEditLayerChanged(oldActiveLayer, oldEditLayer, listenersToFire); 1001 } finally {1002 layerLock.readLock().unlock();1003 930 } 1004 931 repaint(); … … 1033 960 */ 1034 961 public Layer getActiveLayer() { 1035 layerLock.readLock().lock(); 1036 try { 962 synchronized (layers) { 1037 963 return activeLayer; 1038 } finally {1039 layerLock.readLock().unlock();1040 964 } 1041 965 } … … 1092 1016 */ 1093 1017 public OsmDataLayer getEditLayer() { 1094 layerLock.readLock().lock(); 1095 try { 1018 synchronized (layers) { 1096 1019 return editLayer; 1097 } finally {1098 layerLock.readLock().unlock();1099 1020 } 1100 1021 } … … 1107 1028 */ 1108 1029 public boolean hasLayer(Layer layer) { 1109 layerLock.readLock().lock(); 1110 try { 1030 synchronized (layers) { 1111 1031 return layers.contains(layer); 1112 } finally {1113 layerLock.readLock().unlock();1114 1032 } 1115 1033 } … … 1176 1094 protected void refreshTitle() { 1177 1095 if (Main.parent != null) { 1178 layerLock.readLock().lock(); 1179 try { 1096 synchronized (layers) { 1180 1097 boolean dirty = editLayer != null && 1181 1098 (editLayer.requiresSaveToFile() || (editLayer.requiresUploadToServer() && !editLayer.isUploadDiscouraged())); 1182 1099 ((JFrame) Main.parent).setTitle((dirty ? "* " : "") + tr("Java OpenStreetMap Editor")); 1183 1100 ((JFrame) Main.parent).getRootPane().putClientProperty("Window.documentModified", dirty); 1184 } finally {1185 layerLock.readLock().unlock();1186 1101 } 1187 1102 } … … 1209 1124 mapMover.destroy(); 1210 1125 } 1211 layerLock.writeLock().lock(); 1212 try { 1126 synchronized (layers) { 1213 1127 activeLayer = null; 1214 1128 changedLayer = null; 1215 1129 editLayer = null; 1216 1130 layers.clear(); 1217 } finally { 1218 layerLock.writeLock().unlock(); 1219 } 1220 nonChangedLayers.clear(); 1131 nonChangedLayers.clear(); 1132 } 1221 1133 synchronized (temporaryLayers) { 1222 1134 temporaryLayers.clear();
Note:
See TracChangeset
for help on using the changeset viewer.