diff --git a/src/org/openstreetmap/josm/Main.java b/src/org/openstreetmap/josm/Main.java
index 9ccc7eb..ac883f4 100644
a
|
b
|
public abstract class Main {
|
143 | 143 | /** |
144 | 144 | * Replies true if JOSM currently displays a map view. False, if it doesn't, i.e. if |
145 | 145 | * it only shows the MOTD panel. |
| 146 | * <p> |
| 147 | * You do not need this when accessing the layer manager. The layer manager will be empty if no map view is shown. |
146 | 148 | * |
147 | 149 | * @return <code>true</code> if JOSM currently displays a map view |
148 | 150 | */ |
diff --git a/src/org/openstreetmap/josm/gui/dialogs/LayerListDialog.java b/src/org/openstreetmap/josm/gui/dialogs/LayerListDialog.java
index 8c6ae23..6c9e2fc 100644
a
|
b
|
import org.openstreetmap.josm.gui.dialogs.layer.MoveUpAction;
|
59 | 59 | import org.openstreetmap.josm.gui.dialogs.layer.ShowHideLayerAction; |
60 | 60 | import org.openstreetmap.josm.gui.layer.JumpToMarkerActions; |
61 | 61 | import org.openstreetmap.josm.gui.layer.Layer; |
| 62 | import org.openstreetmap.josm.gui.layer.LayerManager.LayerAddEvent; |
| 63 | import org.openstreetmap.josm.gui.layer.LayerManager.LayerChangeListener; |
| 64 | import org.openstreetmap.josm.gui.layer.LayerManager.LayerOrderChangeEvent; |
| 65 | import org.openstreetmap.josm.gui.layer.LayerManager.LayerRemoveEvent; |
| 66 | import org.openstreetmap.josm.gui.layer.MainLayerManager; |
| 67 | import org.openstreetmap.josm.gui.layer.MainLayerManager.ActiveLayerChangeEvent; |
| 68 | import org.openstreetmap.josm.gui.layer.MainLayerManager.ActiveLayerChangeListener; |
62 | 69 | import org.openstreetmap.josm.gui.layer.NativeScaleLayer; |
63 | 70 | import org.openstreetmap.josm.gui.util.GuiHelper; |
64 | 71 | import org.openstreetmap.josm.gui.widgets.DisableShortcutsOnFocusGainedTextField; |
… |
… |
import org.openstreetmap.josm.tools.Shortcut;
|
74 | 81 | * This is a toggle dialog which displays the list of layers. Actions allow to |
75 | 82 | * change the ordering of the layers, to hide/show layers, to activate layers, |
76 | 83 | * and to delete layers. |
| 84 | * <p> |
| 85 | * Support for multiple {@link LayerListDialog} is currently not complete but intended for the future. |
77 | 86 | * @since 17 |
78 | 87 | */ |
79 | 88 | public class LayerListDialog extends ToggleDialog { |
… |
… |
public class LayerListDialog extends ToggleDialog {
|
135 | 144 | private final ToggleLayerIndexVisibility[] visibilityToggleActions = new ToggleLayerIndexVisibility[10]; |
136 | 145 | |
137 | 146 | /** |
| 147 | * The {@link MainLayerManager} this list is for. |
| 148 | * @since xxx |
| 149 | */ |
| 150 | private final transient MainLayerManager layerManager; |
| 151 | |
| 152 | /** |
138 | 153 | * registers (shortcut to toggle right hand side toggle dialogs)+(number keys) shortcuts |
139 | 154 | * to toggle the visibility of the first ten layers. |
140 | 155 | */ |
… |
… |
public class LayerListDialog extends ToggleDialog {
|
154 | 169 | * @param mapFrame map frame |
155 | 170 | */ |
156 | 171 | protected LayerListDialog(MapFrame mapFrame) { |
| 172 | this(mapFrame.mapView.getLayerManager()); |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Creates a layer list and attach it to the given mapView. |
| 177 | * @param layerManager The layer manager this list is for |
| 178 | * @since xxx |
| 179 | */ |
| 180 | private LayerListDialog(MainLayerManager layerManager) { |
157 | 181 | super(tr("Layers"), "layerlist", tr("Open a list of all loaded layers."), |
158 | 182 | Shortcut.registerShortcut("subwindow:layers", tr("Toggle: {0}", tr("Layers")), KeyEvent.VK_L, |
159 | 183 | Shortcut.ALT_SHIFT), 100, true); |
| 184 | this.layerManager = layerManager; |
160 | 185 | |
161 | 186 | // create the models |
162 | 187 | // |
… |
… |
public class LayerListDialog extends ToggleDialog {
|
166 | 191 | |
167 | 192 | // create the list control |
168 | 193 | // |
169 | | layerList = new LayerList(model); |
| 194 | layerList = new LayerList(model, layerManager); |
170 | 195 | layerList.setSelectionModel(selectionModel); |
171 | 196 | layerList.addMouseListener(new PopupMenuHandler()); |
172 | 197 | layerList.setBackground(UIManager.getColor("Button.background")); |
… |
… |
public class LayerListDialog extends ToggleDialog {
|
218 | 243 | |
219 | 244 | // init the model |
220 | 245 | // |
221 | | final MapView mapView = mapFrame.mapView; |
222 | 246 | model.populate(); |
223 | | model.setSelectedLayer(mapView.getActiveLayer()); |
| 247 | model.setSelectedLayer(layerManager.getActiveLayer()); |
224 | 248 | model.addLayerListModelListener( |
225 | 249 | new LayerListModelListener() { |
226 | 250 | @Override |
… |
… |
public class LayerListDialog extends ToggleDialog {
|
296 | 320 | createVisibilityToggleShortcuts(); |
297 | 321 | } |
298 | 322 | |
| 323 | /** |
| 324 | * Gets the layer manager this dialog is for. |
| 325 | * @return The layer manager. |
| 326 | * @since xxx |
| 327 | */ |
| 328 | public MainLayerManager getLayerManager() { |
| 329 | return layerManager; |
| 330 | } |
| 331 | |
299 | 332 | @Override |
300 | 333 | public void showNotify() { |
301 | 334 | MapView.addLayerChangeListener(activateLayerAction); |
302 | | MapView.addLayerChangeListener(model); |
| 335 | layerManager.addLayerChangeListener(model); |
| 336 | layerManager.addActiveLayerChangeListener(model, true); |
303 | 337 | model.populate(); |
304 | 338 | } |
305 | 339 | |
306 | 340 | @Override |
307 | 341 | public void hideNotify() { |
308 | | MapView.removeLayerChangeListener(model); |
| 342 | layerManager.removeLayerChangeListener(model); |
| 343 | layerManager.removeActiveLayerChangeListener(model); |
309 | 344 | MapView.removeLayerChangeListener(activateLayerAction); |
310 | 345 | } |
311 | 346 | |
… |
… |
public class LayerListDialog extends ToggleDialog {
|
512 | 547 | private class LayerNameCellRenderer extends DefaultTableCellRenderer { |
513 | 548 | |
514 | 549 | protected boolean isActiveLayer(Layer layer) { |
515 | | if (!Main.isDisplayingMapView()) |
516 | | return false; |
517 | | return Main.map.mapView.getActiveLayer() == layer; |
| 550 | return getLayerManager().getActiveLayer() == layer; |
518 | 551 | } |
519 | 552 | |
520 | 553 | @Override |
… |
… |
public class LayerListDialog extends ToggleDialog {
|
611 | 644 | * It also listens to {@link PropertyChangeEvent}s of every {@link Layer} it manages, in particular to |
612 | 645 | * the properties {@link Layer#VISIBLE_PROP} and {@link Layer#NAME_PROP}. |
613 | 646 | */ |
614 | | public static final class LayerListModel extends AbstractTableModel implements MapView.LayerChangeListener, PropertyChangeListener { |
| 647 | public static final class LayerListModel extends AbstractTableModel |
| 648 | implements LayerChangeListener, ActiveLayerChangeListener, PropertyChangeListener { |
615 | 649 | /** manages list selection state*/ |
616 | 650 | private final DefaultListSelectionModel selectionModel; |
617 | 651 | private final CopyOnWriteArrayList<LayerListModelListener> listeners; |
… |
… |
public class LayerListDialog extends ToggleDialog {
|
627 | 661 | listeners = new CopyOnWriteArrayList<>(); |
628 | 662 | } |
629 | 663 | |
630 | | void setlayerList(LayerList layerList) { |
| 664 | void setLayerList(LayerList layerList) { |
631 | 665 | this.layerList = layerList; |
632 | 666 | } |
633 | 667 | |
| 668 | private MainLayerManager getLayerManager() { |
| 669 | // layerList should never be null. But if it is, we should not crash. |
| 670 | if (layerList == null) { |
| 671 | return new MainLayerManager(); |
| 672 | } else { |
| 673 | return layerList.getLayerManager(); |
| 674 | } |
| 675 | } |
| 676 | |
634 | 677 | /** |
635 | 678 | * Adds a listener to this model |
636 | 679 | * |
… |
… |
public class LayerListDialog extends ToggleDialog {
|
743 | 786 | layer.removePropertyChangeListener(this); |
744 | 787 | final int size = getRowCount(); |
745 | 788 | final List<Integer> rows = getSelectedRows(); |
746 | | GuiHelper.runInEDTAndWait(new Runnable() { |
747 | | @Override |
748 | | public void run() { |
749 | | if (rows.isEmpty() && size > 0) { |
750 | | selectionModel.setSelectionInterval(size-1, size-1); |
751 | | } |
752 | | fireTableDataChanged(); |
753 | | fireRefresh(); |
754 | | ensureActiveSelected(); |
755 | | } |
756 | | }); |
| 789 | |
| 790 | if (rows.isEmpty() && size > 0) { |
| 791 | selectionModel.setSelectionInterval(size-1, size-1); |
| 792 | } |
| 793 | fireTableDataChanged(); |
| 794 | fireRefresh(); |
| 795 | ensureActiveSelected(); |
757 | 796 | } |
758 | 797 | |
759 | 798 | /** |
… |
… |
public class LayerListDialog extends ToggleDialog {
|
887 | 926 | */ |
888 | 927 | public List<Layer> getPossibleMergeTargets(Layer source) { |
889 | 928 | List<Layer> targets = new ArrayList<>(); |
890 | | if (source == null || !Main.isDisplayingMapView()) { |
| 929 | if (source == null) { |
891 | 930 | return targets; |
892 | 931 | } |
893 | | for (Layer target : Main.map.mapView.getAllLayersAsList()) { |
| 932 | for (Layer target : getLayers()) { |
894 | 933 | if (source == target) { |
895 | 934 | continue; |
896 | 935 | } |
… |
… |
public class LayerListDialog extends ToggleDialog {
|
909 | 948 | * Never null, but can be empty. |
910 | 949 | */ |
911 | 950 | public List<Layer> getLayers() { |
912 | | if (!Main.isDisplayingMapView()) |
913 | | return Collections.<Layer>emptyList(); |
914 | | return Main.map.mapView.getAllLayersAsList(); |
| 951 | return getLayerManager().getLayers(); |
915 | 952 | } |
916 | 953 | |
917 | 954 | /** |
… |
… |
public class LayerListDialog extends ToggleDialog {
|
941 | 978 | * @return the active layer. null, if no active layer is available |
942 | 979 | */ |
943 | 980 | protected Layer getActiveLayer() { |
944 | | return Main.isDisplayingMapView() ? Main.map.mapView.getActiveLayer() : null; |
| 981 | return getLayerManager().getActiveLayer(); |
945 | 982 | } |
946 | 983 | |
947 | 984 | /** |
948 | | * Replies the scale layer. null, if no active layer is available |
| 985 | * Replies the scale layer. null, if no active layer is available. |
949 | 986 | * |
950 | 987 | * @return the scale layer. null, if no active layer is available |
| 988 | * @deprecated Deprecated since it is unused in JOSM and does not really belong here. Can be removed soon (August 2016). |
| 989 | * You can directly query MapView. |
951 | 990 | */ |
| 991 | @Deprecated |
952 | 992 | protected NativeScaleLayer getNativeScaleLayer() { |
953 | 993 | return Main.isDisplayingMapView() ? Main.map.mapView.getNativeScaleLayer() : null; |
954 | 994 | } |
… |
… |
public class LayerListDialog extends ToggleDialog {
|
997 | 1037 | Layer l = layers.get(row); |
998 | 1038 | switch (col) { |
999 | 1039 | case 0: |
1000 | | Main.map.mapView.setActiveLayer(l); |
| 1040 | getLayerManager().setActiveLayer(l); |
1001 | 1041 | l.setVisible(true); |
1002 | 1042 | break; |
1003 | 1043 | case 1: |
… |
… |
public class LayerListDialog extends ToggleDialog {
|
1027 | 1067 | } |
1028 | 1068 | |
1029 | 1069 | /* ------------------------------------------------------------------------------ */ |
1030 | | /* Interface LayerChangeListener */ |
| 1070 | /* Interface ActiveLayerChangeListener */ |
1031 | 1071 | /* ------------------------------------------------------------------------------ */ |
1032 | 1072 | @Override |
1033 | | public void activeLayerChange(final Layer oldLayer, final Layer newLayer) { |
1034 | | GuiHelper.runInEDTAndWait(new Runnable() { |
1035 | | @Override |
1036 | | public void run() { |
1037 | | if (oldLayer != null) { |
1038 | | int idx = getLayers().indexOf(oldLayer); |
1039 | | if (idx >= 0) { |
1040 | | fireTableRowsUpdated(idx, idx); |
1041 | | } |
1042 | | } |
| 1073 | public void activeOrEditLayerChanged(ActiveLayerChangeEvent e) { |
| 1074 | Layer oldLayer = e.getPreviousActiveLayer(); |
| 1075 | if (oldLayer != null) { |
| 1076 | int idx = getLayers().indexOf(oldLayer); |
| 1077 | if (idx >= 0) { |
| 1078 | fireTableRowsUpdated(idx, idx); |
| 1079 | } |
| 1080 | } |
1043 | 1081 | |
1044 | | if (newLayer != null) { |
1045 | | int idx = getLayers().indexOf(newLayer); |
1046 | | if (idx >= 0) { |
1047 | | fireTableRowsUpdated(idx, idx); |
1048 | | } |
1049 | | } |
1050 | | ensureActiveSelected(); |
| 1082 | Layer newLayer = getActiveLayer(); |
| 1083 | if (newLayer != null) { |
| 1084 | int idx = getLayers().indexOf(newLayer); |
| 1085 | if (idx >= 0) { |
| 1086 | fireTableRowsUpdated(idx, idx); |
1051 | 1087 | } |
1052 | | }); |
| 1088 | } |
| 1089 | ensureActiveSelected(); |
1053 | 1090 | } |
1054 | 1091 | |
| 1092 | /* ------------------------------------------------------------------------------ */ |
| 1093 | /* Interface LayerChangeListener */ |
| 1094 | /* ------------------------------------------------------------------------------ */ |
1055 | 1095 | @Override |
1056 | | public void layerAdded(Layer newLayer) { |
1057 | | onAddLayer(newLayer); |
| 1096 | public void layerAdded(LayerAddEvent e) { |
| 1097 | onAddLayer(e.getAddedLayer()); |
1058 | 1098 | } |
1059 | 1099 | |
1060 | 1100 | @Override |
1061 | | public void layerRemoved(final Layer oldLayer) { |
1062 | | onRemoveLayer(oldLayer); |
| 1101 | public void layerRemoving(LayerRemoveEvent e) { |
| 1102 | onRemoveLayer(e.getRemovedLayer()); |
| 1103 | } |
| 1104 | |
| 1105 | @Override |
| 1106 | public void layerOrderChanged(LayerOrderChangeEvent e) { |
| 1107 | // ignored for now, since only we change layer order. |
1063 | 1108 | } |
1064 | 1109 | |
1065 | 1110 | /* ------------------------------------------------------------------------------ */ |
… |
… |
public class LayerListDialog extends ToggleDialog {
|
1077 | 1122 | } |
1078 | 1123 | } |
1079 | 1124 | |
| 1125 | /** |
| 1126 | * This component displays a list of layers and provides the methods needed by {@link LayerListModel}. |
| 1127 | */ |
1080 | 1128 | static class LayerList extends JTable { |
1081 | | LayerList(LayerListModel dataModel) { |
| 1129 | private final transient MainLayerManager layerManager; |
| 1130 | |
| 1131 | LayerList(LayerListModel dataModel, MainLayerManager layerManager) { |
1082 | 1132 | super(dataModel); |
1083 | | dataModel.setlayerList(this); |
| 1133 | this.layerManager = layerManager; |
| 1134 | dataModel.setLayerList(this); |
1084 | 1135 | } |
1085 | 1136 | |
1086 | 1137 | public void scrollToVisible(int row, int col) { |
… |
… |
public class LayerListDialog extends ToggleDialog {
|
1092 | 1143 | rect.setLocation(rect.x - pt.x, rect.y - pt.y); |
1093 | 1144 | viewport.scrollRectToVisible(rect); |
1094 | 1145 | } |
| 1146 | |
| 1147 | /** |
| 1148 | * Gets you the layer manager used for this list. |
| 1149 | * @return The layer manager. |
| 1150 | * @since xxx |
| 1151 | */ |
| 1152 | public MainLayerManager getLayerManager() { |
| 1153 | return layerManager; |
| 1154 | } |
1095 | 1155 | } |
1096 | 1156 | |
1097 | 1157 | /** |
… |
… |
public class LayerListDialog extends ToggleDialog {
|
1148 | 1208 | * @return the layer at given index, or {@code null} if index out of range |
1149 | 1209 | */ |
1150 | 1210 | public static Layer getLayerForIndex(int index) { |
1151 | | if (!Main.isDisplayingMapView()) |
1152 | | return null; |
1153 | | |
1154 | | List<Layer> layers = Main.map.mapView.getAllLayersAsList(); |
| 1211 | List<Layer> layers = Main.getLayerManager().getLayers(); |
1155 | 1212 | |
1156 | 1213 | if (index < layers.size() && index >= 0) |
1157 | 1214 | return layers.get(index); |
… |
… |
public class LayerListDialog extends ToggleDialog {
|
1168 | 1225 | public static List<MultikeyInfo> getLayerInfoByClass(Class<?> layerClass) { |
1169 | 1226 | List<MultikeyInfo> result = new ArrayList<>(); |
1170 | 1227 | |
1171 | | if (!Main.isDisplayingMapView()) |
1172 | | return result; |
1173 | | |
1174 | | List<Layer> layers = Main.map.mapView.getAllLayersAsList(); |
| 1228 | List<Layer> layers = Main.getLayerManager().getLayers(); |
1175 | 1229 | |
1176 | 1230 | int index = 0; |
1177 | 1231 | for (Layer l: layers) { |
… |
… |
public class LayerListDialog extends ToggleDialog {
|
1185 | 1239 | } |
1186 | 1240 | |
1187 | 1241 | /** |
1188 | | * Determines if a layer is valid (contained in layer list). |
| 1242 | * Determines if a layer is valid (contained in global layer list). |
1189 | 1243 | * @param l the layer |
1190 | 1244 | * @return {@code true} if layer {@code l} is contained in current layer list |
1191 | 1245 | */ |
1192 | 1246 | public static boolean isLayerValid(Layer l) { |
1193 | | if (l == null || !Main.isDisplayingMapView()) |
| 1247 | if (l == null) |
1194 | 1248 | return false; |
1195 | 1249 | |
1196 | | return Main.map.mapView.getAllLayersAsList().contains(l); |
| 1250 | return Main.getLayerManager().containsLayer(l); |
1197 | 1251 | } |
1198 | 1252 | |
1199 | 1253 | /** |
… |
… |
public class LayerListDialog extends ToggleDialog {
|
1202 | 1256 | * @return info about layer {@code l} |
1203 | 1257 | */ |
1204 | 1258 | public static MultikeyInfo getLayerInfo(Layer l) { |
1205 | | if (l == null || !Main.isDisplayingMapView()) |
| 1259 | if (l == null) |
1206 | 1260 | return null; |
1207 | 1261 | |
1208 | | int index = Main.map.mapView.getAllLayersAsList().indexOf(l); |
| 1262 | int index = Main.getLayerManager().getLayers().indexOf(l); |
1209 | 1263 | if (index < 0) |
1210 | 1264 | return null; |
1211 | 1265 | |