Changeset 1895 in josm for trunk/src/org
- Timestamp:
- 2009-08-03T17:21:37+02:00 (15 years ago)
- Location:
- trunk/src/org/openstreetmap/josm
- Files:
-
- 9 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/Main.java
r1857 r1895 43 43 import org.openstreetmap.josm.gui.OptionPaneUtil; 44 44 import org.openstreetmap.josm.gui.SplashScreen; 45 import org.openstreetmap.josm.gui.dialogs.LayerListDialog; 45 46 import org.openstreetmap.josm.gui.download.DownloadDialog.DownloadTask; 46 47 import org.openstreetmap.josm.gui.layer.Layer; … … 216 217 mapFrame.setVisible(true); 217 218 mapFrame.setVisibleDialogs(); 219 // bootstrapping problem: make sure the layer list dialog is going to 220 // listen to change events of the very first layer 221 // 222 layer.addPropertyChangeListener(LayerListDialog.getInstance().getModel()); 218 223 } 219 224 map.mapView.addLayer(layer); -
trunk/src/org/openstreetmap/josm/actions/AutoScaleAction.java
r1868 r1895 135 135 Main.map != null 136 136 && Main.map.mapView != null 137 && Main.map.mapView. getAllLayers().size() > 0137 && Main.map.mapView.hasLayers() 138 138 ); 139 139 } -
trunk/src/org/openstreetmap/josm/actions/ZoomInAction.java
r1854 r1895 8 8 9 9 import org.openstreetmap.josm.Main; 10 import org.openstreetmap.josm.gui.layer.Layer;11 10 import org.openstreetmap.josm.tools.Shortcut; 12 11 … … 28 27 Main.map != null 29 28 && Main.map.mapView != null 30 && Main.map.mapView. getAllLayers().size() > 029 && Main.map.mapView.hasLayers() 31 30 ); 32 31 } -
trunk/src/org/openstreetmap/josm/actions/ZoomOutAction.java
r1854 r1895 27 27 Main.map != null 28 28 && Main.map.mapView != null 29 && Main.map.mapView. getAllLayers().size() > 029 && Main.map.mapView.hasLayers() 30 30 ); 31 31 } -
trunk/src/org/openstreetmap/josm/actions/downloadtasks/DownloadOsmTask.java
r1885 r1895 95 95 if (Main.map == null) return null; 96 96 if (Main.map.mapView == null) return null; 97 Collection<Layer> layers = Main.map.mapView.getAllLayers ();97 Collection<Layer> layers = Main.map.mapView.getAllLayersAsList(); 98 98 for (Layer layer : layers) { 99 99 if (layer instanceof OsmDataLayer) -
trunk/src/org/openstreetmap/josm/gui/MapView.java
r1890 r1895 22 22 import java.util.Enumeration; 23 23 import java.util.LinkedList; 24 import java.util.List; 24 25 25 26 import javax.swing.AbstractButton; … … 209 210 } 210 211 212 protected void fireActiveLayerChanged(Layer oldLayer, Layer newLayer) { 213 for (Layer.LayerChangeListener l : Layer.listeners) { 214 l.activeLayerChange(oldLayer, newLayer); 215 } 216 } 217 211 218 /** 212 219 * Remove the layer from the mapview. If the layer was in the list before, … … 214 221 */ 215 222 public void removeLayer(Layer layer) { 223 boolean deletedLayerWasActiveLayer = false; 224 216 225 if (layer == activeLayer) { 217 for (Layer.LayerChangeListener l : Layer.listeners) {218 l.activeLayerChange(layer, null);219 }220 226 activeLayer = null; 227 deletedLayerWasActiveLayer = true; 228 fireActiveLayerChanged(layer, null); 221 229 } 222 230 if (layers.remove(layer)) { … … 228 236 layer.destroy(); 229 237 AudioPlayer.reset(); 238 if (layer instanceof OsmDataLayer && deletedLayerWasActiveLayer) { 239 for (Layer l : layers) { 240 if (l instanceof OsmDataLayer) { 241 activeLayer = l; 242 fireActiveLayerChanged(null, activeLayer); 243 } 244 } 245 } 246 repaint(); 230 247 } 231 248 … … 368 385 public Collection<Layer> getAllLayers() { 369 386 return Collections.unmodifiableCollection(layers); 387 } 388 389 /** 390 * @return An unmodifiable ordered list of all layers 391 */ 392 public List<Layer> getAllLayersAsList() { 393 return Collections.unmodifiableList(layers); 394 } 395 396 /** 397 * Replies the number of layers managed by this mav view 398 * 399 * @return the number of layers managed by this mav view 400 */ 401 public int getNumLayers() { 402 return layers.size(); 403 } 404 405 /** 406 * Replies true if there is at least one layer in this map view 407 * 408 * @return true if there is at least one layer in this map view 409 */ 410 public boolean hasLayers() { 411 return getNumLayers() > 0; 370 412 } 371 413 -
trunk/src/org/openstreetmap/josm/gui/OptionPaneUtil.java
r1865 r1895 10 10 import javax.swing.JDialog; 11 11 import javax.swing.JOptionPane; 12 import javax.swing.JRootPane;13 12 import javax.swing.UIManager; 14 13 -
trunk/src/org/openstreetmap/josm/gui/dialogs/LayerListDialog.java
r1890 r1895 15 15 import java.beans.PropertyChangeListener; 16 16 import java.util.ArrayList; 17 import java.util.Collection;18 17 import java.util.Collections; 19 18 import java.util.List; 20 19 import java.util.concurrent.CopyOnWriteArrayList; 20 import java.util.logging.Logger; 21 21 22 22 import javax.swing.AbstractAction; … … 32 32 import javax.swing.ListModel; 33 33 import javax.swing.ListSelectionModel; 34 import javax.swing.SwingUtilities; 34 35 import javax.swing.UIManager; 35 36 import javax.swing.event.ListDataEvent; … … 59 60 */ 60 61 public class LayerListDialog extends ToggleDialog { 62 static private final Logger logger = Logger.getLogger(LayerListDialog.class.getName()); 61 63 62 64 /** the unique instance of the dialog */ … … 112 114 ActivateLayerAction activateLayerAction = new ActivateLayerAction(); 113 115 adaptTo(activateLayerAction, selectionModel); 116 adaptToLayerChanges(activateLayerAction); 114 117 buttonPanel.add(new SideButton(activateLayerAction, "activate")); 115 118 … … 159 162 // 160 163 final MapView mapView = mapFrame.mapView; 161 model.populate( mapView.getAllLayers());164 model.populate(); 162 165 model.setSelectedLayer(mapView.getActiveLayer()); 163 166 model.addLayerListModelListener( … … 166 169 layerList.ensureIndexIsVisible(index); 167 170 } 171 172 public void refresh() { 173 layerList.repaint(); 174 } 168 175 } 169 176 ); … … 208 215 } 209 216 217 protected void adaptToLayerChanges(final IEnabledStateUpdating listener) { 218 Layer.listeners.add( 219 new LayerChangeListener() { 220 public void activeLayerChange(Layer oldLayer, Layer newLayer) { 221 listener.updateEnabledState(); 222 } 223 public void layerAdded(Layer newLayer) { 224 listener.updateEnabledState(); 225 } 226 public void layerRemoved(Layer oldLayer) { 227 listener.updateEnabledState(); 228 } 229 } 230 ); 231 } 232 233 234 private enum DeleteDecision { 235 deleteCurrent, 236 dontDeleteCurrent, 237 deleteAll, 238 cancel 239 } 240 210 241 /** 211 242 * The action to delete the currently selected layer 212 243 */ 213 244 public final class DeleteLayerAction extends AbstractAction implements IEnabledStateUpdating { 245 246 214 247 215 248 private Layer layer; … … 256 289 "delete_layer", 257 290 Main.parent, 258 tr("Do you really want to delete the wholelayer ''{0}''?", layer.getName()),291 tr("Do you really want to delete layer ''{0}''?", layer.getName()), 259 292 tr("Confirmation"), 260 293 JOptionPane.YES_NO_OPTION, … … 263 296 } 264 297 265 public void deleteLayer(Layer layer) { 298 protected DeleteDecision confirmDeleteMultipleLayer(Layer layer, int idx, int numLayers) { 299 String options[] = new String[] { 300 tr("Yes"), 301 tr("No"), 302 tr("Delete all"), 303 tr("Cancel") 304 }; 305 int ret = ConditionalOptionPaneUtil.showOptionDialog( 306 "delete_layer", 307 Main.parent, 308 tr("Do you really want to delete layer ''{0}''?", layer.getName()), 309 tr("Deleting layer {0} of {1}", idx+1, numLayers), 310 JOptionPane.DEFAULT_OPTION, 311 JOptionPane.QUESTION_MESSAGE, 312 options, 313 options[0] 314 ); 315 switch(ret) { 316 case ConditionalOptionPaneUtil.DIALOG_DISABLED_OPTION: return DeleteDecision.deleteAll; 317 case JOptionPane.CLOSED_OPTION: return DeleteDecision.cancel; 318 case 0: return DeleteDecision.deleteCurrent; 319 case 1: return DeleteDecision.dontDeleteCurrent; 320 case 2: return DeleteDecision.deleteAll; 321 case 3: return DeleteDecision.cancel; 322 default: 323 // shouldn't happen. This is the safest option. 324 return DeleteDecision.cancel; 325 } 326 } 327 328 329 public void deleteSingleLayer(Layer layer) { 266 330 if (layer == null) 267 331 return; … … 282 346 } 283 347 348 public void deleteMultipleLayers(List<Layer> layers) { 349 boolean doAskConfirmation = true; 350 for (int i=0; i < layers.size(); i++) { 351 Layer layer = layers.get(i); 352 if (layer instanceof OsmDataLayer) { 353 OsmDataLayer dataLayer = (OsmDataLayer)layer; 354 if (dataLayer.isModified() && ! confirmSkipSaving(dataLayer)) { 355 continue; 356 } 357 } 358 if (doAskConfirmation) { 359 DeleteDecision decision = confirmDeleteMultipleLayer(layer, i, layers.size()); 360 switch(decision) { 361 case deleteCurrent: /* do nothing */ break; 362 case deleteAll: doAskConfirmation = false; break; 363 case dontDeleteCurrent: continue; 364 case cancel: return; 365 } 366 } 367 // model and view are going to be updated via LayerChangeListener 368 // 369 Main.main.removeLayer(layer); 370 } 371 } 372 284 373 public void actionPerformed(ActionEvent e) { 285 374 if (this.layer == null) { 286 375 List<Layer> selectedLayers = getModel().getSelectedLayers(); 287 for (Layer layer: selectedLayers) { 288 deleteLayer(layer); 376 if (selectedLayers.size() == 1) { 377 deleteSingleLayer(selectedLayers.get(0)); 378 } else { 379 deleteMultipleLayers(selectedLayers); 289 380 } 290 381 } else { 291 delete Layer(this.layer);382 deleteSingleLayer(this.layer); 292 383 } 293 384 } … … 566 657 public interface LayerListModelListener { 567 658 public void makeVisible(int index, Layer layer); 659 public void refresh(); 568 660 } 569 661 … … 581 673 public class LayerListModel extends DefaultListModel implements LayerChangeListener, PropertyChangeListener{ 582 674 583 private ArrayList<Layer> layers;675 //private ArrayList<Layer> layers; 584 676 private DefaultListSelectionModel selectionModel; 585 677 private CopyOnWriteArrayList<LayerListModelListener> listeners; 586 678 587 679 private LayerListModel(DefaultListSelectionModel selectionModel) { 588 layers = new ArrayList<Layer>();589 680 this.selectionModel = selectionModel; 590 681 listeners = new CopyOnWriteArrayList<LayerListModelListener>(); … … 613 704 } 614 705 615 public void populate(Collection<Layer> layers) { 616 if (layers == null) 706 protected void fireRefresh() { 707 for (LayerListModelListener listener : listeners) { 708 listener.refresh(); 709 } 710 } 711 712 public void populate() { 713 if (getLayers() != null) { 714 for (Layer layer: getLayers()) { 715 // make sure the model is registered exactly once 716 // 717 layer.removePropertyChangeListener(this); 718 layer.addPropertyChangeListener(this); 719 } 720 } 721 fireContentsChanged(this, 0, getSize()); 722 } 723 724 public void setSelectedLayer(Layer layer) { 725 if (layer == null || getLayers() == null) 617 726 return; 618 this.layers.clear(); 619 this.layers.addAll(layers); 620 for (Layer layer: this.layers) { 621 layer.addPropertyChangeListener(this); 622 } 623 fireContentsChanged(this, 0, getSize()); 624 } 625 626 public void setSelectedLayer(Layer layer) { 627 int idx = layers.indexOf(layer); 727 int idx = getLayers().indexOf(layer); 628 728 if (idx >= 0) { 629 729 selectionModel.setSelectionInterval(idx, idx); … … 635 735 public List<Layer> getSelectedLayers() { 636 736 ArrayList<Layer> selected = new ArrayList<Layer>(); 637 for (int i=0; i<layers.size(); i++) { 737 if (getLayers() == null) return selected; 738 for (int i=0; i<getLayers().size(); i++) { 638 739 if (selectionModel.isSelectedIndex(i)) { 639 selected.add( layers.get(i));740 selected.add(getLayers().get(i)); 640 741 } 641 742 } … … 645 746 public List<Integer> getSelectedRows() { 646 747 ArrayList<Integer> selected = new ArrayList<Integer>(); 647 for (int i=0; i<layers.size();i++) { 748 if (getLayers() == null) return selected; 749 for (int i=0; i<getLayers().size();i++) { 648 750 if (selectionModel.isSelectedIndex(i)) { 649 751 selected.add(i); … … 653 755 } 654 756 655 p ublicvoid removeLayer(Layer layer) {757 protected void removeLayer(Layer layer) { 656 758 if (layer == null) 657 759 return; 658 List<Integer> selectedRows = getSelectedRows(); 659 int deletedRow = layers.indexOf(layer); 660 if (deletedRow < 0) 661 // layer not found in the list of layers 662 return; 663 layers.remove(layer); 664 fireContentsChanged(this, 0,getSize()); 665 for (int row: selectedRows) { 666 if (row < deletedRow) { 667 selectionModel.addSelectionInterval(row, row); 668 } else if (row == deletedRow){ 669 // do nothing 670 } else { 671 selectionModel.addSelectionInterval(row-1, row-1); 672 } 673 } 760 fireRefresh(); 674 761 ensureSelectedIsVisible(); 675 762 } 676 763 677 p ublicvoid addLayer(Layer layer) {764 protected void addLayer(Layer layer) { 678 765 if (layer == null) return; 679 if (layers.contains(layer)) return;680 layers.add(layer);681 766 layer.addPropertyChangeListener(this); 682 767 fireContentsChanged(this, 0, getSize()); … … 685 770 public Layer getFirstLayer() { 686 771 if (getSize() == 0) return null; 687 return layers.get(0);772 return getLayers().get(0); 688 773 } 689 774 … … 691 776 if (index < 0 || index >= getSize()) 692 777 return null; 693 return layers.get(index);778 return getLayers().get(index); 694 779 } 695 780 … … 703 788 List<Integer> sel = getSelectedRows(); 704 789 for (int row: sel) { 705 Layer l1 = layers.get(row); 706 Layer l2 = layers.get(row-1); 707 layers.set(row, l2); 708 layers.set(row-1,l1); 790 Layer l1 = getLayers().get(row); 791 Layer l2 = getLayers().get(row-1); 792 Main.map.mapView.moveLayer(l2,row); 709 793 Main.map.mapView.moveLayer(l1, row-1); 710 794 } … … 719 803 public boolean canMoveDown() { 720 804 List<Integer> sel = getSelectedRows(); 721 return !sel.isEmpty() && sel.get(sel.size()-1) < layers.size()-1;805 return !sel.isEmpty() && sel.get(sel.size()-1) < getLayers().size()-1; 722 806 } 723 807 … … 727 811 Collections.reverse(sel); 728 812 for (int row: sel) { 729 Layer l1 = layers.get(row); 730 Layer l2 = layers.get(row+1); 731 layers.set(row, l2); 732 layers.set(row+1,l1); 813 Layer l1 = getLayers().get(row); 814 Layer l2 = getLayers().get(row+1); 733 815 Main.map.mapView.moveLayer(l1, row+1); 816 Main.map.mapView.moveLayer(l2, row); 734 817 } 735 818 fireContentsChanged(this, 0, getSize()); … … 744 827 int index = selectionModel.getMinSelectionIndex(); 745 828 if (index <0 )return; 746 if (index >= layers.size()) return; 747 Layer layer = layers.get(index); 829 if (getLayers() == null) return; 830 if (index >= getLayers().size()) return; 831 Layer layer = getLayers().get(index); 748 832 fireMakeVisible(index, layer); 749 833 } … … 753 837 if (layer == null) 754 838 return targets; 755 for(Layer target: layers) {839 for(Layer target: getLayers()) { 756 840 if (layer == target) { 757 841 continue; … … 765 849 766 850 public void activateLayer(Layer layer) { 767 layers.remove(layer); 768 layers.add(0,layer); 851 Main.map.mapView.moveLayer(layer,0); 769 852 Main.map.mapView.setActiveLayer(layer); 770 853 layer.setVisible(true); 771 854 selectionModel.setSelectionInterval(0,0); 772 855 ensureSelectedIsVisible(); 856 } 857 858 protected List<Layer> getLayers() { 859 if (Main.map == null) return null; 860 if (Main.map.mapView == null) return null; 861 return Main.map.mapView.getAllLayersAsList(); 773 862 } 774 863 … … 778 867 @Override 779 868 public Object getElementAt(int index) { 780 return layers.get(index);869 return getLayers().get(index); 781 870 } 782 871 783 872 @Override 784 873 public int getSize() { 874 List<Layer> layers = getLayers(); 875 if (layers == null) return 0; 785 876 return layers.size(); 786 877 } … … 791 882 public void activeLayerChange(Layer oldLayer, Layer newLayer) { 792 883 if (oldLayer != null) { 793 int idx = layers.indexOf(oldLayer);884 int idx = getLayers().indexOf(oldLayer); 794 885 if (idx >= 0) { 795 886 fireContentsChanged(this, idx,idx); … … 798 889 799 890 if (newLayer != null) { 800 int idx = layers.indexOf(newLayer);891 int idx = getLayers().indexOf(newLayer); 801 892 if (idx >= 0) { 802 893 fireContentsChanged(this, idx,idx); … … 809 900 } 810 901 811 public void layerRemoved(Layer oldLayer) { 812 removeLayer(oldLayer); 813 } 902 public void layerRemoved(final Layer oldLayer) { 903 SwingUtilities.invokeLater( 904 new Runnable() { 905 public void run() { 906 removeLayer(oldLayer); 907 } 908 } 909 ); 910 } 911 814 912 815 913 /* ------------------------------------------------------------------------------ */ … … 819 917 if (evt.getSource() instanceof Layer) { 820 918 Layer layer = (Layer)evt.getSource(); 821 int idx = layers.indexOf(layer);919 final int idx = getLayers().indexOf(layer); 822 920 if (idx < 0) return; 823 fire ContentsChanged(this, idx, idx);921 fireRefresh(); 824 922 } 825 923 } -
trunk/src/org/openstreetmap/josm/gui/dialogs/RelationListDialog.java
r1892 r1895 11 11 import java.util.Collection; 12 12 import java.util.HashSet; 13 import java.util.logging.Logger; 13 14 14 15 import javax.swing.AbstractAction; … … 46 47 */ 47 48 public class RelationListDialog extends ToggleDialog implements LayerChangeListener, DataChangeListener { 49 private static final Logger logger = Logger.getLogger(RelationListDialog.class.getName()); 48 50 static private final PrimitiveNameFormatter NAME_FORMATTER = new PrimitiveNameFormatter(); 49 51
Note:
See TracChangeset
for help on using the changeset viewer.