1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.dialogs;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.Color;
|
---|
7 | import java.awt.Component;
|
---|
8 | import java.awt.Dimension;
|
---|
9 | import java.awt.Font;
|
---|
10 | import java.awt.Point;
|
---|
11 | import java.awt.Rectangle;
|
---|
12 | import java.awt.event.ActionEvent;
|
---|
13 | import java.awt.event.InputEvent;
|
---|
14 | import java.awt.event.KeyEvent;
|
---|
15 | import java.awt.event.MouseEvent;
|
---|
16 | import java.beans.PropertyChangeEvent;
|
---|
17 | import java.beans.PropertyChangeListener;
|
---|
18 | import java.util.ArrayList;
|
---|
19 | import java.util.Arrays;
|
---|
20 | import java.util.Collections;
|
---|
21 | import java.util.List;
|
---|
22 | import java.util.concurrent.CopyOnWriteArrayList;
|
---|
23 |
|
---|
24 | import javax.swing.AbstractAction;
|
---|
25 | import javax.swing.DefaultCellEditor;
|
---|
26 | import javax.swing.DefaultListSelectionModel;
|
---|
27 | import javax.swing.ImageIcon;
|
---|
28 | import javax.swing.JCheckBox;
|
---|
29 | import javax.swing.JComponent;
|
---|
30 | import javax.swing.JLabel;
|
---|
31 | import javax.swing.JTable;
|
---|
32 | import javax.swing.JViewport;
|
---|
33 | import javax.swing.KeyStroke;
|
---|
34 | import javax.swing.ListSelectionModel;
|
---|
35 | import javax.swing.UIManager;
|
---|
36 | import javax.swing.event.ListDataEvent;
|
---|
37 | import javax.swing.event.ListSelectionEvent;
|
---|
38 | import javax.swing.event.ListSelectionListener;
|
---|
39 | import javax.swing.event.TableModelEvent;
|
---|
40 | import javax.swing.event.TableModelListener;
|
---|
41 | import javax.swing.table.AbstractTableModel;
|
---|
42 | import javax.swing.table.DefaultTableCellRenderer;
|
---|
43 | import javax.swing.table.TableCellRenderer;
|
---|
44 | import javax.swing.table.TableModel;
|
---|
45 |
|
---|
46 | import org.openstreetmap.josm.Main;
|
---|
47 | import org.openstreetmap.josm.actions.MergeLayerAction;
|
---|
48 | import org.openstreetmap.josm.gui.MapFrame;
|
---|
49 | import org.openstreetmap.josm.gui.MapView;
|
---|
50 | import org.openstreetmap.josm.gui.SideButton;
|
---|
51 | import org.openstreetmap.josm.gui.dialogs.layer.ActivateLayerAction;
|
---|
52 | import org.openstreetmap.josm.gui.dialogs.layer.DeleteLayerAction;
|
---|
53 | import org.openstreetmap.josm.gui.dialogs.layer.DuplicateAction;
|
---|
54 | import org.openstreetmap.josm.gui.dialogs.layer.IEnabledStateUpdating;
|
---|
55 | import org.openstreetmap.josm.gui.dialogs.layer.LayerVisibilityAction;
|
---|
56 | import org.openstreetmap.josm.gui.dialogs.layer.MergeAction;
|
---|
57 | import org.openstreetmap.josm.gui.dialogs.layer.MoveDownAction;
|
---|
58 | import org.openstreetmap.josm.gui.dialogs.layer.MoveUpAction;
|
---|
59 | import org.openstreetmap.josm.gui.dialogs.layer.ShowHideLayerAction;
|
---|
60 | import org.openstreetmap.josm.gui.layer.JumpToMarkerActions;
|
---|
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;
|
---|
69 | import org.openstreetmap.josm.gui.layer.NativeScaleLayer;
|
---|
70 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
---|
71 | import org.openstreetmap.josm.gui.widgets.DisableShortcutsOnFocusGainedTextField;
|
---|
72 | import org.openstreetmap.josm.gui.widgets.JosmTextField;
|
---|
73 | import org.openstreetmap.josm.gui.widgets.PopupMenuLauncher;
|
---|
74 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
75 | import org.openstreetmap.josm.tools.InputMapUtils;
|
---|
76 | import org.openstreetmap.josm.tools.MultikeyActionsHandler;
|
---|
77 | import org.openstreetmap.josm.tools.MultikeyShortcutAction.MultikeyInfo;
|
---|
78 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
79 |
|
---|
80 | /**
|
---|
81 | * This is a toggle dialog which displays the list of layers. Actions allow to
|
---|
82 | * change the ordering of the layers, to hide/show layers, to activate layers,
|
---|
83 | * and to delete layers.
|
---|
84 | * <p>
|
---|
85 | * Support for multiple {@link LayerListDialog} is currently not complete but intended for the future.
|
---|
86 | * @since 17
|
---|
87 | */
|
---|
88 | public class LayerListDialog extends ToggleDialog {
|
---|
89 | /** the unique instance of the dialog */
|
---|
90 | private static volatile LayerListDialog instance;
|
---|
91 |
|
---|
92 | /**
|
---|
93 | * Creates the instance of the dialog. It's connected to the map frame <code>mapFrame</code>
|
---|
94 | *
|
---|
95 | * @param mapFrame the map frame
|
---|
96 | */
|
---|
97 | public static void createInstance(MapFrame mapFrame) {
|
---|
98 | if (instance != null)
|
---|
99 | throw new IllegalStateException("Dialog was already created");
|
---|
100 | instance = new LayerListDialog(mapFrame);
|
---|
101 | }
|
---|
102 |
|
---|
103 | /**
|
---|
104 | * Replies the instance of the dialog
|
---|
105 | *
|
---|
106 | * @return the instance of the dialog
|
---|
107 | * @throws IllegalStateException if the dialog is not created yet
|
---|
108 | * @see #createInstance(MapFrame)
|
---|
109 | */
|
---|
110 | public static LayerListDialog getInstance() {
|
---|
111 | if (instance == null)
|
---|
112 | throw new IllegalStateException("Dialog not created yet. Invoke createInstance() first");
|
---|
113 | return instance;
|
---|
114 | }
|
---|
115 |
|
---|
116 | /** the model for the layer list */
|
---|
117 | private final LayerListModel model;
|
---|
118 |
|
---|
119 | /** the list of layers (technically its a JTable, but appears like a list) */
|
---|
120 | private final LayerList layerList;
|
---|
121 |
|
---|
122 | private final ActivateLayerAction activateLayerAction;
|
---|
123 | private final ShowHideLayerAction showHideLayerAction;
|
---|
124 |
|
---|
125 | //TODO This duplicates ShowHide actions functionality
|
---|
126 | /** stores which layer index to toggle and executes the ShowHide action if the layer is present */
|
---|
127 | private final class ToggleLayerIndexVisibility extends AbstractAction {
|
---|
128 | private final int layerIndex;
|
---|
129 |
|
---|
130 | ToggleLayerIndexVisibility(int layerIndex) {
|
---|
131 | this.layerIndex = layerIndex;
|
---|
132 | }
|
---|
133 |
|
---|
134 | @Override
|
---|
135 | public void actionPerformed(ActionEvent e) {
|
---|
136 | final Layer l = model.getLayer(model.getRowCount() - layerIndex - 1);
|
---|
137 | if (l != null) {
|
---|
138 | l.toggleVisible();
|
---|
139 | }
|
---|
140 | }
|
---|
141 | }
|
---|
142 |
|
---|
143 | private final transient Shortcut[] visibilityToggleShortcuts = new Shortcut[10];
|
---|
144 | private final ToggleLayerIndexVisibility[] visibilityToggleActions = new ToggleLayerIndexVisibility[10];
|
---|
145 |
|
---|
146 | /**
|
---|
147 | * The {@link MainLayerManager} this list is for.
|
---|
148 | */
|
---|
149 | private final transient MainLayerManager layerManager;
|
---|
150 |
|
---|
151 | /**
|
---|
152 | * registers (shortcut to toggle right hand side toggle dialogs)+(number keys) shortcuts
|
---|
153 | * to toggle the visibility of the first ten layers.
|
---|
154 | */
|
---|
155 | private void createVisibilityToggleShortcuts() {
|
---|
156 | for (int i = 0; i < 10; i++) {
|
---|
157 | final int i1 = i + 1;
|
---|
158 | /* POSSIBLE SHORTCUTS: 1,2,3,4,5,6,7,8,9,0=10 */
|
---|
159 | visibilityToggleShortcuts[i] = Shortcut.registerShortcut("subwindow:layers:toggleLayer" + i1,
|
---|
160 | tr("Toggle visibility of layer: {0}", i1), KeyEvent.VK_0 + (i1 % 10), Shortcut.ALT);
|
---|
161 | visibilityToggleActions[i] = new ToggleLayerIndexVisibility(i);
|
---|
162 | Main.registerActionShortcut(visibilityToggleActions[i], visibilityToggleShortcuts[i]);
|
---|
163 | }
|
---|
164 | }
|
---|
165 |
|
---|
166 | /**
|
---|
167 | * Creates a layer list and attach it to the given mapView.
|
---|
168 | * @param mapFrame map frame
|
---|
169 | */
|
---|
170 | protected LayerListDialog(MapFrame mapFrame) {
|
---|
171 | this(mapFrame.mapView.getLayerManager());
|
---|
172 | }
|
---|
173 |
|
---|
174 | /**
|
---|
175 | * Creates a layer list and attach it to the given mapView.
|
---|
176 | * @param layerManager The layer manager this list is for
|
---|
177 | */
|
---|
178 | private LayerListDialog(MainLayerManager layerManager) {
|
---|
179 | super(tr("Layers"), "layerlist", tr("Open a list of all loaded layers."),
|
---|
180 | Shortcut.registerShortcut("subwindow:layers", tr("Toggle: {0}", tr("Layers")), KeyEvent.VK_L,
|
---|
181 | Shortcut.ALT_SHIFT), 100, true);
|
---|
182 | this.layerManager = layerManager;
|
---|
183 |
|
---|
184 | // create the models
|
---|
185 | //
|
---|
186 | DefaultListSelectionModel selectionModel = new DefaultListSelectionModel();
|
---|
187 | selectionModel.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
|
---|
188 | model = new LayerListModel(layerManager, selectionModel);
|
---|
189 |
|
---|
190 | // create the list control
|
---|
191 | //
|
---|
192 | layerList = new LayerList(model);
|
---|
193 | layerList.setSelectionModel(selectionModel);
|
---|
194 | layerList.addMouseListener(new PopupMenuHandler());
|
---|
195 | layerList.setBackground(UIManager.getColor("Button.background"));
|
---|
196 | layerList.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
|
---|
197 | layerList.putClientProperty("JTable.autoStartsEdit", Boolean.FALSE);
|
---|
198 | layerList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
|
---|
199 | layerList.setTableHeader(null);
|
---|
200 | layerList.setShowGrid(false);
|
---|
201 | layerList.setIntercellSpacing(new Dimension(0, 0));
|
---|
202 | layerList.getColumnModel().getColumn(0).setCellRenderer(new ActiveLayerCellRenderer());
|
---|
203 | layerList.getColumnModel().getColumn(0).setCellEditor(new DefaultCellEditor(new ActiveLayerCheckBox()));
|
---|
204 | layerList.getColumnModel().getColumn(0).setMaxWidth(12);
|
---|
205 | layerList.getColumnModel().getColumn(0).setPreferredWidth(12);
|
---|
206 | layerList.getColumnModel().getColumn(0).setResizable(false);
|
---|
207 |
|
---|
208 | layerList.getColumnModel().getColumn(1).setCellRenderer(new NativeScaleLayerCellRenderer());
|
---|
209 | layerList.getColumnModel().getColumn(1).setCellEditor(new DefaultCellEditor(new NativeScaleLayerCheckBox()));
|
---|
210 | layerList.getColumnModel().getColumn(1).setMaxWidth(12);
|
---|
211 | layerList.getColumnModel().getColumn(1).setPreferredWidth(12);
|
---|
212 | layerList.getColumnModel().getColumn(1).setResizable(false);
|
---|
213 |
|
---|
214 | layerList.getColumnModel().getColumn(2).setCellRenderer(new LayerVisibleCellRenderer());
|
---|
215 | layerList.getColumnModel().getColumn(2).setCellEditor(new LayerVisibleCellEditor(new LayerVisibleCheckBox()));
|
---|
216 | layerList.getColumnModel().getColumn(2).setMaxWidth(16);
|
---|
217 | layerList.getColumnModel().getColumn(2).setPreferredWidth(16);
|
---|
218 | layerList.getColumnModel().getColumn(2).setResizable(false);
|
---|
219 |
|
---|
220 | layerList.getColumnModel().getColumn(3).setCellRenderer(new LayerNameCellRenderer());
|
---|
221 | layerList.getColumnModel().getColumn(3).setCellEditor(new LayerNameCellEditor(new DisableShortcutsOnFocusGainedTextField()));
|
---|
222 | // Disable some default JTable shortcuts to use JOSM ones (see #5678, #10458)
|
---|
223 | for (KeyStroke ks : new KeyStroke[] {
|
---|
224 | KeyStroke.getKeyStroke(KeyEvent.VK_C, GuiHelper.getMenuShortcutKeyMaskEx()),
|
---|
225 | KeyStroke.getKeyStroke(KeyEvent.VK_V, GuiHelper.getMenuShortcutKeyMaskEx()),
|
---|
226 | KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, InputEvent.SHIFT_DOWN_MASK),
|
---|
227 | KeyStroke.getKeyStroke(KeyEvent.VK_UP, InputEvent.SHIFT_DOWN_MASK),
|
---|
228 | KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, InputEvent.SHIFT_DOWN_MASK),
|
---|
229 | KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, InputEvent.SHIFT_DOWN_MASK),
|
---|
230 | KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, InputEvent.CTRL_DOWN_MASK),
|
---|
231 | KeyStroke.getKeyStroke(KeyEvent.VK_UP, InputEvent.CTRL_DOWN_MASK),
|
---|
232 | KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, InputEvent.CTRL_DOWN_MASK),
|
---|
233 | KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, InputEvent.CTRL_DOWN_MASK),
|
---|
234 | KeyStroke.getKeyStroke(KeyEvent.VK_PAGE_UP, 0),
|
---|
235 | KeyStroke.getKeyStroke(KeyEvent.VK_PAGE_DOWN, 0),
|
---|
236 | KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0),
|
---|
237 | KeyStroke.getKeyStroke(KeyEvent.VK_F8, 0),
|
---|
238 | }) {
|
---|
239 | layerList.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(ks, new Object());
|
---|
240 | }
|
---|
241 |
|
---|
242 | // init the model
|
---|
243 | //
|
---|
244 | model.populate();
|
---|
245 | model.setSelectedLayer(layerManager.getActiveLayer());
|
---|
246 | model.addLayerListModelListener(
|
---|
247 | new LayerListModelListener() {
|
---|
248 | @Override
|
---|
249 | public void makeVisible(int row, Layer layer) {
|
---|
250 | layerList.scrollToVisible(row, 0);
|
---|
251 | layerList.repaint();
|
---|
252 | }
|
---|
253 |
|
---|
254 | @Override
|
---|
255 | public void refresh() {
|
---|
256 | layerList.repaint();
|
---|
257 | }
|
---|
258 | }
|
---|
259 | );
|
---|
260 |
|
---|
261 | // -- move up action
|
---|
262 | MoveUpAction moveUpAction = new MoveUpAction(model);
|
---|
263 | adaptTo(moveUpAction, model);
|
---|
264 | adaptTo(moveUpAction, selectionModel);
|
---|
265 |
|
---|
266 | // -- move down action
|
---|
267 | MoveDownAction moveDownAction = new MoveDownAction(model);
|
---|
268 | adaptTo(moveDownAction, model);
|
---|
269 | adaptTo(moveDownAction, selectionModel);
|
---|
270 |
|
---|
271 | // -- activate action
|
---|
272 | activateLayerAction = new ActivateLayerAction(model);
|
---|
273 | activateLayerAction.updateEnabledState();
|
---|
274 | MultikeyActionsHandler.getInstance().addAction(activateLayerAction);
|
---|
275 | adaptTo(activateLayerAction, selectionModel);
|
---|
276 |
|
---|
277 | JumpToMarkerActions.initialize();
|
---|
278 |
|
---|
279 | // -- show hide action
|
---|
280 | showHideLayerAction = new ShowHideLayerAction(model);
|
---|
281 | MultikeyActionsHandler.getInstance().addAction(showHideLayerAction);
|
---|
282 | adaptTo(showHideLayerAction, selectionModel);
|
---|
283 |
|
---|
284 | LayerVisibilityAction visibilityAction = new LayerVisibilityAction(model);
|
---|
285 | adaptTo(visibilityAction, selectionModel);
|
---|
286 | SideButton visibilityButton = new SideButton(visibilityAction, false);
|
---|
287 | visibilityAction.setCorrespondingSideButton(visibilityButton);
|
---|
288 |
|
---|
289 | // -- delete layer action
|
---|
290 | DeleteLayerAction deleteLayerAction = new DeleteLayerAction(model);
|
---|
291 | layerList.getActionMap().put("deleteLayer", deleteLayerAction);
|
---|
292 | adaptTo(deleteLayerAction, selectionModel);
|
---|
293 | getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(
|
---|
294 | KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0), "delete"
|
---|
295 | );
|
---|
296 | getActionMap().put("delete", deleteLayerAction);
|
---|
297 |
|
---|
298 | // Activate layer on Enter key press
|
---|
299 | InputMapUtils.addEnterAction(layerList, new AbstractAction() {
|
---|
300 | @Override
|
---|
301 | public void actionPerformed(ActionEvent e) {
|
---|
302 | activateLayerAction.actionPerformed(null);
|
---|
303 | layerList.requestFocus();
|
---|
304 | }
|
---|
305 | });
|
---|
306 |
|
---|
307 | // Show/Activate layer on Enter key press
|
---|
308 | InputMapUtils.addSpacebarAction(layerList, showHideLayerAction);
|
---|
309 |
|
---|
310 | createLayout(layerList, true, Arrays.asList(
|
---|
311 | new SideButton(moveUpAction, false),
|
---|
312 | new SideButton(moveDownAction, false),
|
---|
313 | new SideButton(activateLayerAction, false),
|
---|
314 | visibilityButton,
|
---|
315 | new SideButton(deleteLayerAction, false)
|
---|
316 | ));
|
---|
317 |
|
---|
318 | createVisibilityToggleShortcuts();
|
---|
319 | }
|
---|
320 |
|
---|
321 | /**
|
---|
322 | * Gets the layer manager this dialog is for.
|
---|
323 | * @return The layer manager.
|
---|
324 | * @since 10288
|
---|
325 | */
|
---|
326 | public MainLayerManager getLayerManager() {
|
---|
327 | return layerManager;
|
---|
328 | }
|
---|
329 |
|
---|
330 | @Override
|
---|
331 | public void showNotify() {
|
---|
332 | layerManager.addActiveLayerChangeListener(activateLayerAction);
|
---|
333 | layerManager.addLayerChangeListener(model);
|
---|
334 | layerManager.addAndFireActiveLayerChangeListener(model);
|
---|
335 | model.populate();
|
---|
336 | }
|
---|
337 |
|
---|
338 | @Override
|
---|
339 | public void hideNotify() {
|
---|
340 | layerManager.removeLayerChangeListener(model);
|
---|
341 | layerManager.removeActiveLayerChangeListener(model);
|
---|
342 | layerManager.removeActiveLayerChangeListener(activateLayerAction);
|
---|
343 | }
|
---|
344 |
|
---|
345 | /**
|
---|
346 | * Returns the layer list model.
|
---|
347 | * @return the layer list model
|
---|
348 | */
|
---|
349 | public LayerListModel getModel() {
|
---|
350 | return model;
|
---|
351 | }
|
---|
352 |
|
---|
353 | /**
|
---|
354 | * Wires <code>listener</code> to <code>listSelectionModel</code> in such a way, that
|
---|
355 | * <code>listener</code> receives a {@link IEnabledStateUpdating#updateEnabledState()}
|
---|
356 | * on every {@link ListSelectionEvent}.
|
---|
357 | *
|
---|
358 | * @param listener the listener
|
---|
359 | * @param listSelectionModel the source emitting {@link ListSelectionEvent}s
|
---|
360 | */
|
---|
361 | protected void adaptTo(final IEnabledStateUpdating listener, ListSelectionModel listSelectionModel) {
|
---|
362 | listSelectionModel.addListSelectionListener(
|
---|
363 | new ListSelectionListener() {
|
---|
364 | @Override
|
---|
365 | public void valueChanged(ListSelectionEvent e) {
|
---|
366 | listener.updateEnabledState();
|
---|
367 | }
|
---|
368 | }
|
---|
369 | );
|
---|
370 | }
|
---|
371 |
|
---|
372 | /**
|
---|
373 | * Wires <code>listener</code> to <code>listModel</code> in such a way, that
|
---|
374 | * <code>listener</code> receives a {@link IEnabledStateUpdating#updateEnabledState()}
|
---|
375 | * on every {@link ListDataEvent}.
|
---|
376 | *
|
---|
377 | * @param listener the listener
|
---|
378 | * @param listModel the source emitting {@link ListDataEvent}s
|
---|
379 | */
|
---|
380 | protected void adaptTo(final IEnabledStateUpdating listener, LayerListModel listModel) {
|
---|
381 | listModel.addTableModelListener(
|
---|
382 | new TableModelListener() {
|
---|
383 | @Override
|
---|
384 | public void tableChanged(TableModelEvent e) {
|
---|
385 | listener.updateEnabledState();
|
---|
386 | }
|
---|
387 | }
|
---|
388 | );
|
---|
389 | }
|
---|
390 |
|
---|
391 | @Override
|
---|
392 | public void destroy() {
|
---|
393 | for (int i = 0; i < 10; i++) {
|
---|
394 | Main.unregisterActionShortcut(visibilityToggleActions[i], visibilityToggleShortcuts[i]);
|
---|
395 | }
|
---|
396 | MultikeyActionsHandler.getInstance().removeAction(activateLayerAction);
|
---|
397 | MultikeyActionsHandler.getInstance().removeAction(showHideLayerAction);
|
---|
398 | JumpToMarkerActions.unregisterActions();
|
---|
399 | super.destroy();
|
---|
400 | instance = null;
|
---|
401 | }
|
---|
402 |
|
---|
403 | private static class ActiveLayerCheckBox extends JCheckBox {
|
---|
404 | ActiveLayerCheckBox() {
|
---|
405 | setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
|
---|
406 | ImageIcon blank = ImageProvider.get("dialogs/layerlist", "blank");
|
---|
407 | ImageIcon active = ImageProvider.get("dialogs/layerlist", "active");
|
---|
408 | setIcon(blank);
|
---|
409 | setSelectedIcon(active);
|
---|
410 | setRolloverIcon(blank);
|
---|
411 | setRolloverSelectedIcon(active);
|
---|
412 | setPressedIcon(ImageProvider.get("dialogs/layerlist", "active-pressed"));
|
---|
413 | }
|
---|
414 | }
|
---|
415 |
|
---|
416 | private static class LayerVisibleCheckBox extends JCheckBox {
|
---|
417 | private final ImageIcon iconEye;
|
---|
418 | private final ImageIcon iconEyeTranslucent;
|
---|
419 | private boolean isTranslucent;
|
---|
420 |
|
---|
421 | /**
|
---|
422 | * Constructs a new {@code LayerVisibleCheckBox}.
|
---|
423 | */
|
---|
424 | LayerVisibleCheckBox() {
|
---|
425 | setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
|
---|
426 | iconEye = ImageProvider.get("dialogs/layerlist", "eye");
|
---|
427 | iconEyeTranslucent = ImageProvider.get("dialogs/layerlist", "eye-translucent");
|
---|
428 | setIcon(ImageProvider.get("dialogs/layerlist", "eye-off"));
|
---|
429 | setPressedIcon(ImageProvider.get("dialogs/layerlist", "eye-pressed"));
|
---|
430 | setSelectedIcon(iconEye);
|
---|
431 | isTranslucent = false;
|
---|
432 | }
|
---|
433 |
|
---|
434 | public void setTranslucent(boolean isTranslucent) {
|
---|
435 | if (this.isTranslucent == isTranslucent) return;
|
---|
436 | if (isTranslucent) {
|
---|
437 | setSelectedIcon(iconEyeTranslucent);
|
---|
438 | } else {
|
---|
439 | setSelectedIcon(iconEye);
|
---|
440 | }
|
---|
441 | this.isTranslucent = isTranslucent;
|
---|
442 | }
|
---|
443 |
|
---|
444 | public void updateStatus(Layer layer) {
|
---|
445 | boolean visible = layer.isVisible();
|
---|
446 | setSelected(visible);
|
---|
447 | setTranslucent(layer.getOpacity() < 1.0);
|
---|
448 | setToolTipText(visible ?
|
---|
449 | tr("layer is currently visible (click to hide layer)") :
|
---|
450 | tr("layer is currently hidden (click to show layer)"));
|
---|
451 | }
|
---|
452 | }
|
---|
453 |
|
---|
454 | private static class NativeScaleLayerCheckBox extends JCheckBox {
|
---|
455 | NativeScaleLayerCheckBox() {
|
---|
456 | setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
|
---|
457 | ImageIcon blank = ImageProvider.get("dialogs/layerlist", "blank");
|
---|
458 | ImageIcon active = ImageProvider.get("dialogs/layerlist", "scale");
|
---|
459 | setIcon(blank);
|
---|
460 | setSelectedIcon(active);
|
---|
461 | }
|
---|
462 | }
|
---|
463 |
|
---|
464 | private static class ActiveLayerCellRenderer implements TableCellRenderer {
|
---|
465 | private final JCheckBox cb;
|
---|
466 |
|
---|
467 | /**
|
---|
468 | * Constructs a new {@code ActiveLayerCellRenderer}.
|
---|
469 | */
|
---|
470 | ActiveLayerCellRenderer() {
|
---|
471 | cb = new ActiveLayerCheckBox();
|
---|
472 | }
|
---|
473 |
|
---|
474 | @Override
|
---|
475 | public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
|
---|
476 | boolean active = value != null && (Boolean) value;
|
---|
477 | cb.setSelected(active);
|
---|
478 | cb.setToolTipText(active ? tr("this layer is the active layer") : tr("this layer is not currently active (click to activate)"));
|
---|
479 | return cb;
|
---|
480 | }
|
---|
481 | }
|
---|
482 |
|
---|
483 | private static class LayerVisibleCellRenderer implements TableCellRenderer {
|
---|
484 | private final LayerVisibleCheckBox cb;
|
---|
485 |
|
---|
486 | /**
|
---|
487 | * Constructs a new {@code LayerVisibleCellRenderer}.
|
---|
488 | */
|
---|
489 | LayerVisibleCellRenderer() {
|
---|
490 | this.cb = new LayerVisibleCheckBox();
|
---|
491 | }
|
---|
492 |
|
---|
493 | @Override
|
---|
494 | public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
|
---|
495 | if (value != null) {
|
---|
496 | cb.updateStatus((Layer) value);
|
---|
497 | }
|
---|
498 | return cb;
|
---|
499 | }
|
---|
500 | }
|
---|
501 |
|
---|
502 | private static class LayerVisibleCellEditor extends DefaultCellEditor {
|
---|
503 | private final LayerVisibleCheckBox cb;
|
---|
504 |
|
---|
505 | LayerVisibleCellEditor(LayerVisibleCheckBox cb) {
|
---|
506 | super(cb);
|
---|
507 | this.cb = cb;
|
---|
508 | }
|
---|
509 |
|
---|
510 | @Override
|
---|
511 | public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
|
---|
512 | cb.updateStatus((Layer) value);
|
---|
513 | return cb;
|
---|
514 | }
|
---|
515 | }
|
---|
516 |
|
---|
517 | private static class NativeScaleLayerCellRenderer implements TableCellRenderer {
|
---|
518 | private final JCheckBox cb;
|
---|
519 |
|
---|
520 | /**
|
---|
521 | * Constructs a new {@code ActiveLayerCellRenderer}.
|
---|
522 | */
|
---|
523 | NativeScaleLayerCellRenderer() {
|
---|
524 | cb = new NativeScaleLayerCheckBox();
|
---|
525 | }
|
---|
526 |
|
---|
527 | @Override
|
---|
528 | public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
|
---|
529 | Layer layer = (Layer) value;
|
---|
530 | if (layer instanceof NativeScaleLayer) {
|
---|
531 | boolean active = ((NativeScaleLayer) layer) == Main.map.mapView.getNativeScaleLayer();
|
---|
532 | cb.setSelected(active);
|
---|
533 | cb.setToolTipText(active
|
---|
534 | ? tr("scale follows native resolution of this layer")
|
---|
535 | : tr("scale follows native resolution of another layer (click to set this layer)")
|
---|
536 | );
|
---|
537 | } else {
|
---|
538 | cb.setSelected(false);
|
---|
539 | cb.setToolTipText(tr("this layer has no native resolution"));
|
---|
540 | }
|
---|
541 | return cb;
|
---|
542 | }
|
---|
543 | }
|
---|
544 |
|
---|
545 | private class LayerNameCellRenderer extends DefaultTableCellRenderer {
|
---|
546 |
|
---|
547 | protected boolean isActiveLayer(Layer layer) {
|
---|
548 | return getLayerManager().getActiveLayer() == layer;
|
---|
549 | }
|
---|
550 |
|
---|
551 | @Override
|
---|
552 | public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
|
---|
553 | if (value == null)
|
---|
554 | return this;
|
---|
555 | Layer layer = (Layer) value;
|
---|
556 | JLabel label = (JLabel) super.getTableCellRendererComponent(table,
|
---|
557 | layer.getName(), isSelected, hasFocus, row, column);
|
---|
558 | if (isActiveLayer(layer)) {
|
---|
559 | label.setFont(label.getFont().deriveFont(Font.BOLD));
|
---|
560 | }
|
---|
561 | if (Main.pref.getBoolean("dialog.layer.colorname", true)) {
|
---|
562 | Color c = layer.getColor(false);
|
---|
563 | if (c != null) {
|
---|
564 | Color oc = null;
|
---|
565 | for (Layer l : model.getLayers()) {
|
---|
566 | oc = l.getColor(false);
|
---|
567 | if (oc != null) {
|
---|
568 | if (oc.equals(c)) {
|
---|
569 | oc = null;
|
---|
570 | } else {
|
---|
571 | break;
|
---|
572 | }
|
---|
573 | }
|
---|
574 | }
|
---|
575 | /* not more than one color, don't use coloring */
|
---|
576 | if (oc == null) {
|
---|
577 | c = null;
|
---|
578 | }
|
---|
579 | }
|
---|
580 | if (c == null) {
|
---|
581 | c = UIManager.getColor(isSelected ? "Table.selectionForeground" : "Table.foreground");
|
---|
582 | }
|
---|
583 | label.setForeground(c);
|
---|
584 | }
|
---|
585 | label.setIcon(layer.getIcon());
|
---|
586 | label.setToolTipText(layer.getToolTipText());
|
---|
587 | return label;
|
---|
588 | }
|
---|
589 | }
|
---|
590 |
|
---|
591 | private static class LayerNameCellEditor extends DefaultCellEditor {
|
---|
592 | LayerNameCellEditor(DisableShortcutsOnFocusGainedTextField tf) {
|
---|
593 | super(tf);
|
---|
594 | }
|
---|
595 |
|
---|
596 | @Override
|
---|
597 | public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
|
---|
598 | JosmTextField tf = (JosmTextField) super.getTableCellEditorComponent(table, value, isSelected, row, column);
|
---|
599 | tf.setText(value == null ? "" : ((Layer) value).getName());
|
---|
600 | return tf;
|
---|
601 | }
|
---|
602 | }
|
---|
603 |
|
---|
604 | class PopupMenuHandler extends PopupMenuLauncher {
|
---|
605 | @Override
|
---|
606 | public void showMenu(MouseEvent evt) {
|
---|
607 | menu = new LayerListPopup(getModel().getSelectedLayers());
|
---|
608 | super.showMenu(evt);
|
---|
609 | }
|
---|
610 | }
|
---|
611 |
|
---|
612 | /**
|
---|
613 | * Observer interface to be implemented by views using {@link LayerListModel}.
|
---|
614 | */
|
---|
615 | public interface LayerListModelListener {
|
---|
616 |
|
---|
617 | /**
|
---|
618 | * Fired when a layer is made visible.
|
---|
619 | * @param index the layer index
|
---|
620 | * @param layer the layer
|
---|
621 | */
|
---|
622 | void makeVisible(int index, Layer layer);
|
---|
623 |
|
---|
624 |
|
---|
625 | /**
|
---|
626 | * Fired when something has changed in the layer list model.
|
---|
627 | */
|
---|
628 | void refresh();
|
---|
629 | }
|
---|
630 |
|
---|
631 | /**
|
---|
632 | * The layer list model. The model manages a list of layers and provides methods for
|
---|
633 | * moving layers up and down, for toggling their visibility, and for activating a layer.
|
---|
634 | *
|
---|
635 | * The model is a {@link TableModel} and it provides a {@link ListSelectionModel}. It expects
|
---|
636 | * to be configured with a {@link DefaultListSelectionModel}. The selection model is used
|
---|
637 | * to update the selection state of views depending on messages sent to the model.
|
---|
638 | *
|
---|
639 | * The model manages a list of {@link LayerListModelListener} which are mainly notified if
|
---|
640 | * the model requires views to make a specific list entry visible.
|
---|
641 | *
|
---|
642 | * It also listens to {@link PropertyChangeEvent}s of every {@link Layer} it manages, in particular to
|
---|
643 | * the properties {@link Layer#VISIBLE_PROP} and {@link Layer#NAME_PROP}.
|
---|
644 | */
|
---|
645 | public static final class LayerListModel extends AbstractTableModel
|
---|
646 | implements LayerChangeListener, ActiveLayerChangeListener, PropertyChangeListener {
|
---|
647 | /** manages list selection state*/
|
---|
648 | private final DefaultListSelectionModel selectionModel;
|
---|
649 | private final CopyOnWriteArrayList<LayerListModelListener> listeners;
|
---|
650 | private LayerList layerList;
|
---|
651 | private final MainLayerManager layerManager;
|
---|
652 |
|
---|
653 | /**
|
---|
654 | * constructor
|
---|
655 | * @param layerManager The layer manager to use for the list.
|
---|
656 | * @param selectionModel the list selection model
|
---|
657 | */
|
---|
658 | LayerListModel(MainLayerManager layerManager, DefaultListSelectionModel selectionModel) {
|
---|
659 | this.layerManager = layerManager;
|
---|
660 | this.selectionModel = selectionModel;
|
---|
661 | listeners = new CopyOnWriteArrayList<>();
|
---|
662 | }
|
---|
663 |
|
---|
664 | void setLayerList(LayerList layerList) {
|
---|
665 | this.layerList = layerList;
|
---|
666 | }
|
---|
667 |
|
---|
668 | /**
|
---|
669 | * The layer manager this model is for.
|
---|
670 | * @return The layer manager.
|
---|
671 | */
|
---|
672 | public MainLayerManager getLayerManager() {
|
---|
673 | return layerManager;
|
---|
674 | }
|
---|
675 |
|
---|
676 | /**
|
---|
677 | * Adds a listener to this model
|
---|
678 | *
|
---|
679 | * @param listener the listener
|
---|
680 | */
|
---|
681 | public void addLayerListModelListener(LayerListModelListener listener) {
|
---|
682 | if (listener != null) {
|
---|
683 | listeners.addIfAbsent(listener);
|
---|
684 | }
|
---|
685 | }
|
---|
686 |
|
---|
687 | /**
|
---|
688 | * removes a listener from this model
|
---|
689 | * @param listener the listener
|
---|
690 | */
|
---|
691 | public void removeLayerListModelListener(LayerListModelListener listener) {
|
---|
692 | listeners.remove(listener);
|
---|
693 | }
|
---|
694 |
|
---|
695 | /**
|
---|
696 | * Fires a make visible event to listeners
|
---|
697 | *
|
---|
698 | * @param index the index of the row to make visible
|
---|
699 | * @param layer the layer at this index
|
---|
700 | * @see LayerListModelListener#makeVisible(int, Layer)
|
---|
701 | */
|
---|
702 | protected void fireMakeVisible(int index, Layer layer) {
|
---|
703 | for (LayerListModelListener listener : listeners) {
|
---|
704 | listener.makeVisible(index, layer);
|
---|
705 | }
|
---|
706 | }
|
---|
707 |
|
---|
708 | /**
|
---|
709 | * Fires a refresh event to listeners of this model
|
---|
710 | *
|
---|
711 | * @see LayerListModelListener#refresh()
|
---|
712 | */
|
---|
713 | protected void fireRefresh() {
|
---|
714 | for (LayerListModelListener listener : listeners) {
|
---|
715 | listener.refresh();
|
---|
716 | }
|
---|
717 | }
|
---|
718 |
|
---|
719 | /**
|
---|
720 | * Populates the model with the current layers managed by {@link MapView}.
|
---|
721 | */
|
---|
722 | public void populate() {
|
---|
723 | for (Layer layer: getLayers()) {
|
---|
724 | // make sure the model is registered exactly once
|
---|
725 | layer.removePropertyChangeListener(this);
|
---|
726 | layer.addPropertyChangeListener(this);
|
---|
727 | }
|
---|
728 | fireTableDataChanged();
|
---|
729 | }
|
---|
730 |
|
---|
731 | /**
|
---|
732 | * Marks <code>layer</code> as selected layer. Ignored, if layer is null.
|
---|
733 | *
|
---|
734 | * @param layer the layer.
|
---|
735 | */
|
---|
736 | public void setSelectedLayer(Layer layer) {
|
---|
737 | if (layer == null)
|
---|
738 | return;
|
---|
739 | int idx = getLayers().indexOf(layer);
|
---|
740 | if (idx >= 0) {
|
---|
741 | selectionModel.setSelectionInterval(idx, idx);
|
---|
742 | }
|
---|
743 | ensureSelectedIsVisible();
|
---|
744 | }
|
---|
745 |
|
---|
746 | /**
|
---|
747 | * Replies the list of currently selected layers. Never null, but may be empty.
|
---|
748 | *
|
---|
749 | * @return the list of currently selected layers. Never null, but may be empty.
|
---|
750 | */
|
---|
751 | public List<Layer> getSelectedLayers() {
|
---|
752 | List<Layer> selected = new ArrayList<>();
|
---|
753 | List<Layer> layers = getLayers();
|
---|
754 | for (int i = 0; i < layers.size(); i++) {
|
---|
755 | if (selectionModel.isSelectedIndex(i)) {
|
---|
756 | selected.add(layers.get(i));
|
---|
757 | }
|
---|
758 | }
|
---|
759 | return selected;
|
---|
760 | }
|
---|
761 |
|
---|
762 | /**
|
---|
763 | * Replies a the list of indices of the selected rows. Never null, but may be empty.
|
---|
764 | *
|
---|
765 | * @return the list of indices of the selected rows. Never null, but may be empty.
|
---|
766 | */
|
---|
767 | public List<Integer> getSelectedRows() {
|
---|
768 | List<Integer> selected = new ArrayList<>();
|
---|
769 | for (int i = 0; i < getLayers().size(); i++) {
|
---|
770 | if (selectionModel.isSelectedIndex(i)) {
|
---|
771 | selected.add(i);
|
---|
772 | }
|
---|
773 | }
|
---|
774 | return selected;
|
---|
775 | }
|
---|
776 |
|
---|
777 | /**
|
---|
778 | * Invoked if a layer managed by {@link MapView} is removed
|
---|
779 | *
|
---|
780 | * @param layer the layer which is removed
|
---|
781 | */
|
---|
782 | private void onRemoveLayer(Layer layer) {
|
---|
783 | if (layer == null)
|
---|
784 | return;
|
---|
785 | layer.removePropertyChangeListener(this);
|
---|
786 | final int size = getRowCount();
|
---|
787 | final List<Integer> rows = getSelectedRows();
|
---|
788 |
|
---|
789 | if (rows.isEmpty() && size > 0) {
|
---|
790 | selectionModel.setSelectionInterval(size-1, size-1);
|
---|
791 | }
|
---|
792 | fireTableDataChanged();
|
---|
793 | fireRefresh();
|
---|
794 | ensureActiveSelected();
|
---|
795 | }
|
---|
796 |
|
---|
797 | /**
|
---|
798 | * Invoked when a layer managed by {@link MapView} is added
|
---|
799 | *
|
---|
800 | * @param layer the layer
|
---|
801 | */
|
---|
802 | private void onAddLayer(Layer layer) {
|
---|
803 | if (layer == null)
|
---|
804 | return;
|
---|
805 | layer.addPropertyChangeListener(this);
|
---|
806 | fireTableDataChanged();
|
---|
807 | int idx = getLayers().indexOf(layer);
|
---|
808 | if (layerList != null) {
|
---|
809 | layerList.setRowHeight(idx, Math.max(16, layer.getIcon().getIconHeight()));
|
---|
810 | }
|
---|
811 | selectionModel.setSelectionInterval(idx, idx);
|
---|
812 | ensureSelectedIsVisible();
|
---|
813 | }
|
---|
814 |
|
---|
815 | /**
|
---|
816 | * Replies the first layer. Null if no layers are present
|
---|
817 | *
|
---|
818 | * @return the first layer. Null if no layers are present
|
---|
819 | */
|
---|
820 | public Layer getFirstLayer() {
|
---|
821 | if (getRowCount() == 0)
|
---|
822 | return null;
|
---|
823 | return getLayers().get(0);
|
---|
824 | }
|
---|
825 |
|
---|
826 | /**
|
---|
827 | * Replies the layer at position <code>index</code>
|
---|
828 | *
|
---|
829 | * @param index the index
|
---|
830 | * @return the layer at position <code>index</code>. Null,
|
---|
831 | * if index is out of range.
|
---|
832 | */
|
---|
833 | public Layer getLayer(int index) {
|
---|
834 | if (index < 0 || index >= getRowCount())
|
---|
835 | return null;
|
---|
836 | return getLayers().get(index);
|
---|
837 | }
|
---|
838 |
|
---|
839 | /**
|
---|
840 | * Replies true if the currently selected layers can move up by one position
|
---|
841 | *
|
---|
842 | * @return true if the currently selected layers can move up by one position
|
---|
843 | */
|
---|
844 | public boolean canMoveUp() {
|
---|
845 | List<Integer> sel = getSelectedRows();
|
---|
846 | return !sel.isEmpty() && sel.get(0) > 0;
|
---|
847 | }
|
---|
848 |
|
---|
849 | /**
|
---|
850 | * Move up the currently selected layers by one position
|
---|
851 | *
|
---|
852 | */
|
---|
853 | public void moveUp() {
|
---|
854 | if (!canMoveUp())
|
---|
855 | return;
|
---|
856 | List<Integer> sel = getSelectedRows();
|
---|
857 | List<Layer> layers = getLayers();
|
---|
858 | for (int row : sel) {
|
---|
859 | Layer l1 = layers.get(row);
|
---|
860 | Layer l2 = layers.get(row-1);
|
---|
861 | Main.map.mapView.moveLayer(l2, row);
|
---|
862 | Main.map.mapView.moveLayer(l1, row-1);
|
---|
863 | }
|
---|
864 | fireTableDataChanged();
|
---|
865 | selectionModel.clearSelection();
|
---|
866 | for (int row : sel) {
|
---|
867 | selectionModel.addSelectionInterval(row-1, row-1);
|
---|
868 | }
|
---|
869 | ensureSelectedIsVisible();
|
---|
870 | }
|
---|
871 |
|
---|
872 | /**
|
---|
873 | * Replies true if the currently selected layers can move down by one position
|
---|
874 | *
|
---|
875 | * @return true if the currently selected layers can move down by one position
|
---|
876 | */
|
---|
877 | public boolean canMoveDown() {
|
---|
878 | List<Integer> sel = getSelectedRows();
|
---|
879 | return !sel.isEmpty() && sel.get(sel.size()-1) < getLayers().size()-1;
|
---|
880 | }
|
---|
881 |
|
---|
882 | /**
|
---|
883 | * Move down the currently selected layers by one position
|
---|
884 | */
|
---|
885 | public void moveDown() {
|
---|
886 | if (!canMoveDown())
|
---|
887 | return;
|
---|
888 | List<Integer> sel = getSelectedRows();
|
---|
889 | Collections.reverse(sel);
|
---|
890 | List<Layer> layers = getLayers();
|
---|
891 | for (int row : sel) {
|
---|
892 | Layer l1 = layers.get(row);
|
---|
893 | Layer l2 = layers.get(row+1);
|
---|
894 | Main.map.mapView.moveLayer(l1, row+1);
|
---|
895 | Main.map.mapView.moveLayer(l2, row);
|
---|
896 | }
|
---|
897 | fireTableDataChanged();
|
---|
898 | selectionModel.clearSelection();
|
---|
899 | for (int row : sel) {
|
---|
900 | selectionModel.addSelectionInterval(row+1, row+1);
|
---|
901 | }
|
---|
902 | ensureSelectedIsVisible();
|
---|
903 | }
|
---|
904 |
|
---|
905 | /**
|
---|
906 | * Make sure the first of the selected layers is visible in the views of this model.
|
---|
907 | */
|
---|
908 | protected void ensureSelectedIsVisible() {
|
---|
909 | int index = selectionModel.getMinSelectionIndex();
|
---|
910 | if (index < 0)
|
---|
911 | return;
|
---|
912 | List<Layer> layers = getLayers();
|
---|
913 | if (index >= layers.size())
|
---|
914 | return;
|
---|
915 | Layer layer = layers.get(index);
|
---|
916 | fireMakeVisible(index, layer);
|
---|
917 | }
|
---|
918 |
|
---|
919 | /**
|
---|
920 | * Replies a list of layers which are possible merge targets for <code>source</code>
|
---|
921 | *
|
---|
922 | * @param source the source layer
|
---|
923 | * @return a list of layers which are possible merge targets
|
---|
924 | * for <code>source</code>. Never null, but can be empty.
|
---|
925 | */
|
---|
926 | public List<Layer> getPossibleMergeTargets(Layer source) {
|
---|
927 | List<Layer> targets = new ArrayList<>();
|
---|
928 | if (source == null) {
|
---|
929 | return targets;
|
---|
930 | }
|
---|
931 | for (Layer target : getLayers()) {
|
---|
932 | if (source == target) {
|
---|
933 | continue;
|
---|
934 | }
|
---|
935 | if (target.isMergable(source) && source.isMergable(target)) {
|
---|
936 | targets.add(target);
|
---|
937 | }
|
---|
938 | }
|
---|
939 | return targets;
|
---|
940 | }
|
---|
941 |
|
---|
942 | /**
|
---|
943 | * Replies the list of layers currently managed by {@link MapView}.
|
---|
944 | * Never null, but can be empty.
|
---|
945 | *
|
---|
946 | * @return the list of layers currently managed by {@link MapView}.
|
---|
947 | * Never null, but can be empty.
|
---|
948 | */
|
---|
949 | public List<Layer> getLayers() {
|
---|
950 | return getLayerManager().getLayers();
|
---|
951 | }
|
---|
952 |
|
---|
953 | /**
|
---|
954 | * Ensures that at least one layer is selected in the layer dialog
|
---|
955 | *
|
---|
956 | */
|
---|
957 | protected void ensureActiveSelected() {
|
---|
958 | List<Layer> layers = getLayers();
|
---|
959 | if (layers.isEmpty())
|
---|
960 | return;
|
---|
961 | final Layer activeLayer = getActiveLayer();
|
---|
962 | if (activeLayer != null) {
|
---|
963 | // there's an active layer - select it and make it visible
|
---|
964 | int idx = layers.indexOf(activeLayer);
|
---|
965 | selectionModel.setSelectionInterval(idx, idx);
|
---|
966 | ensureSelectedIsVisible();
|
---|
967 | } else {
|
---|
968 | // no active layer - select the first one and make it visible
|
---|
969 | selectionModel.setSelectionInterval(0, 0);
|
---|
970 | ensureSelectedIsVisible();
|
---|
971 | }
|
---|
972 | }
|
---|
973 |
|
---|
974 | /**
|
---|
975 | * Replies the active layer. null, if no active layer is available
|
---|
976 | *
|
---|
977 | * @return the active layer. null, if no active layer is available
|
---|
978 | */
|
---|
979 | protected Layer getActiveLayer() {
|
---|
980 | return getLayerManager().getActiveLayer();
|
---|
981 | }
|
---|
982 |
|
---|
983 | /**
|
---|
984 | * Replies the scale layer. null, if no active layer is available.
|
---|
985 | *
|
---|
986 | * @return the scale layer. null, if no active layer is available
|
---|
987 | * @deprecated Deprecated since it is unused in JOSM and does not really belong here. Can be removed soon (August 2016).
|
---|
988 | * You can directly query MapView.
|
---|
989 | */
|
---|
990 | @Deprecated
|
---|
991 | protected NativeScaleLayer getNativeScaleLayer() {
|
---|
992 | return Main.isDisplayingMapView() ? Main.map.mapView.getNativeScaleLayer() : null;
|
---|
993 | }
|
---|
994 |
|
---|
995 | /* ------------------------------------------------------------------------------ */
|
---|
996 | /* Interface TableModel */
|
---|
997 | /* ------------------------------------------------------------------------------ */
|
---|
998 |
|
---|
999 | @Override
|
---|
1000 | public int getRowCount() {
|
---|
1001 | List<Layer> layers = getLayers();
|
---|
1002 | return layers == null ? 0 : layers.size();
|
---|
1003 | }
|
---|
1004 |
|
---|
1005 | @Override
|
---|
1006 | public int getColumnCount() {
|
---|
1007 | return 4;
|
---|
1008 | }
|
---|
1009 |
|
---|
1010 | @Override
|
---|
1011 | public Object getValueAt(int row, int col) {
|
---|
1012 | List<Layer> layers = getLayers();
|
---|
1013 | if (row >= 0 && row < layers.size()) {
|
---|
1014 | switch (col) {
|
---|
1015 | case 0: return layers.get(row) == getActiveLayer();
|
---|
1016 | case 1:
|
---|
1017 | case 2:
|
---|
1018 | case 3: return layers.get(row);
|
---|
1019 | default: // Do nothing
|
---|
1020 | }
|
---|
1021 | }
|
---|
1022 | return null;
|
---|
1023 | }
|
---|
1024 |
|
---|
1025 | @Override
|
---|
1026 | public boolean isCellEditable(int row, int col) {
|
---|
1027 | if (col == 0 && getActiveLayer() == getLayers().get(row))
|
---|
1028 | return false;
|
---|
1029 | return true;
|
---|
1030 | }
|
---|
1031 |
|
---|
1032 | @Override
|
---|
1033 | public void setValueAt(Object value, int row, int col) {
|
---|
1034 | List<Layer> layers = getLayers();
|
---|
1035 | if (row < layers.size()) {
|
---|
1036 | Layer l = layers.get(row);
|
---|
1037 | switch (col) {
|
---|
1038 | case 0:
|
---|
1039 | getLayerManager().setActiveLayer(l);
|
---|
1040 | l.setVisible(true);
|
---|
1041 | break;
|
---|
1042 | case 1:
|
---|
1043 | NativeScaleLayer oldLayer = Main.map.mapView.getNativeScaleLayer();
|
---|
1044 | if (oldLayer == l) {
|
---|
1045 | Main.map.mapView.setNativeScaleLayer(null);
|
---|
1046 | } else if (l instanceof NativeScaleLayer) {
|
---|
1047 | Main.map.mapView.setNativeScaleLayer((NativeScaleLayer) l);
|
---|
1048 | if (oldLayer != null) {
|
---|
1049 | int idx = getLayers().indexOf(oldLayer);
|
---|
1050 | if (idx >= 0) {
|
---|
1051 | fireTableCellUpdated(idx, col);
|
---|
1052 | }
|
---|
1053 | }
|
---|
1054 | }
|
---|
1055 | break;
|
---|
1056 | case 2:
|
---|
1057 | l.setVisible((Boolean) value);
|
---|
1058 | break;
|
---|
1059 | case 3:
|
---|
1060 | l.rename((String) value);
|
---|
1061 | break;
|
---|
1062 | default: throw new RuntimeException();
|
---|
1063 | }
|
---|
1064 | fireTableCellUpdated(row, col);
|
---|
1065 | }
|
---|
1066 | }
|
---|
1067 |
|
---|
1068 | /* ------------------------------------------------------------------------------ */
|
---|
1069 | /* Interface ActiveLayerChangeListener */
|
---|
1070 | /* ------------------------------------------------------------------------------ */
|
---|
1071 | @Override
|
---|
1072 | public void activeOrEditLayerChanged(ActiveLayerChangeEvent e) {
|
---|
1073 | Layer oldLayer = e.getPreviousActiveLayer();
|
---|
1074 | if (oldLayer != null) {
|
---|
1075 | int idx = getLayers().indexOf(oldLayer);
|
---|
1076 | if (idx >= 0) {
|
---|
1077 | fireTableRowsUpdated(idx, idx);
|
---|
1078 | }
|
---|
1079 | }
|
---|
1080 |
|
---|
1081 | Layer newLayer = getActiveLayer();
|
---|
1082 | if (newLayer != null) {
|
---|
1083 | int idx = getLayers().indexOf(newLayer);
|
---|
1084 | if (idx >= 0) {
|
---|
1085 | fireTableRowsUpdated(idx, idx);
|
---|
1086 | }
|
---|
1087 | }
|
---|
1088 | ensureActiveSelected();
|
---|
1089 | }
|
---|
1090 |
|
---|
1091 | /* ------------------------------------------------------------------------------ */
|
---|
1092 | /* Interface LayerChangeListener */
|
---|
1093 | /* ------------------------------------------------------------------------------ */
|
---|
1094 | @Override
|
---|
1095 | public void layerAdded(LayerAddEvent e) {
|
---|
1096 | onAddLayer(e.getAddedLayer());
|
---|
1097 | }
|
---|
1098 |
|
---|
1099 | @Override
|
---|
1100 | public void layerRemoving(LayerRemoveEvent e) {
|
---|
1101 | onRemoveLayer(e.getRemovedLayer());
|
---|
1102 | }
|
---|
1103 |
|
---|
1104 | @Override
|
---|
1105 | public void layerOrderChanged(LayerOrderChangeEvent e) {
|
---|
1106 | // ignored for now, since only we change layer order.
|
---|
1107 | }
|
---|
1108 |
|
---|
1109 | /* ------------------------------------------------------------------------------ */
|
---|
1110 | /* Interface PropertyChangeListener */
|
---|
1111 | /* ------------------------------------------------------------------------------ */
|
---|
1112 | @Override
|
---|
1113 | public void propertyChange(PropertyChangeEvent evt) {
|
---|
1114 | if (evt.getSource() instanceof Layer) {
|
---|
1115 | Layer layer = (Layer) evt.getSource();
|
---|
1116 | final int idx = getLayers().indexOf(layer);
|
---|
1117 | if (idx < 0)
|
---|
1118 | return;
|
---|
1119 | fireRefresh();
|
---|
1120 | }
|
---|
1121 | }
|
---|
1122 | }
|
---|
1123 |
|
---|
1124 | /**
|
---|
1125 | * This component displays a list of layers and provides the methods needed by {@link LayerListModel}.
|
---|
1126 | */
|
---|
1127 | static class LayerList extends JTable {
|
---|
1128 |
|
---|
1129 | LayerList(LayerListModel dataModel) {
|
---|
1130 | super(dataModel);
|
---|
1131 | dataModel.setLayerList(this);
|
---|
1132 | }
|
---|
1133 |
|
---|
1134 | public void scrollToVisible(int row, int col) {
|
---|
1135 | if (!(getParent() instanceof JViewport))
|
---|
1136 | return;
|
---|
1137 | JViewport viewport = (JViewport) getParent();
|
---|
1138 | Rectangle rect = getCellRect(row, col, true);
|
---|
1139 | Point pt = viewport.getViewPosition();
|
---|
1140 | rect.setLocation(rect.x - pt.x, rect.y - pt.y);
|
---|
1141 | viewport.scrollRectToVisible(rect);
|
---|
1142 | }
|
---|
1143 | }
|
---|
1144 |
|
---|
1145 | /**
|
---|
1146 | * Creates a {@link ShowHideLayerAction} in the context of this {@link LayerListDialog}.
|
---|
1147 | *
|
---|
1148 | * @return the action
|
---|
1149 | */
|
---|
1150 | public ShowHideLayerAction createShowHideLayerAction() {
|
---|
1151 | return new ShowHideLayerAction(model);
|
---|
1152 | }
|
---|
1153 |
|
---|
1154 | /**
|
---|
1155 | * Creates a {@link DeleteLayerAction} in the context of this {@link LayerListDialog}.
|
---|
1156 | *
|
---|
1157 | * @return the action
|
---|
1158 | */
|
---|
1159 | public DeleteLayerAction createDeleteLayerAction() {
|
---|
1160 | return new DeleteLayerAction(model);
|
---|
1161 | }
|
---|
1162 |
|
---|
1163 | /**
|
---|
1164 | * Creates a {@link ActivateLayerAction} for <code>layer</code> in the context of this {@link LayerListDialog}.
|
---|
1165 | *
|
---|
1166 | * @param layer the layer
|
---|
1167 | * @return the action
|
---|
1168 | */
|
---|
1169 | public ActivateLayerAction createActivateLayerAction(Layer layer) {
|
---|
1170 | return new ActivateLayerAction(layer, model);
|
---|
1171 | }
|
---|
1172 |
|
---|
1173 | /**
|
---|
1174 | * Creates a {@link MergeLayerAction} for <code>layer</code> in the context of this {@link LayerListDialog}.
|
---|
1175 | *
|
---|
1176 | * @param layer the layer
|
---|
1177 | * @return the action
|
---|
1178 | */
|
---|
1179 | public MergeAction createMergeLayerAction(Layer layer) {
|
---|
1180 | return new MergeAction(layer, model);
|
---|
1181 | }
|
---|
1182 |
|
---|
1183 | /**
|
---|
1184 | * Creates a {@link DuplicateAction} for <code>layer</code> in the context of this {@link LayerListDialog}.
|
---|
1185 | *
|
---|
1186 | * @param layer the layer
|
---|
1187 | * @return the action
|
---|
1188 | */
|
---|
1189 | public DuplicateAction createDuplicateLayerAction(Layer layer) {
|
---|
1190 | return new DuplicateAction(layer, model);
|
---|
1191 | }
|
---|
1192 |
|
---|
1193 | /**
|
---|
1194 | * Returns the layer at given index, or {@code null}.
|
---|
1195 | * @param index the index
|
---|
1196 | * @return the layer at given index, or {@code null} if index out of range
|
---|
1197 | */
|
---|
1198 | public static Layer getLayerForIndex(int index) {
|
---|
1199 | List<Layer> layers = Main.getLayerManager().getLayers();
|
---|
1200 |
|
---|
1201 | if (index < layers.size() && index >= 0)
|
---|
1202 | return layers.get(index);
|
---|
1203 | else
|
---|
1204 | return null;
|
---|
1205 | }
|
---|
1206 |
|
---|
1207 | /**
|
---|
1208 | * Returns a list of info on all layers of a given class.
|
---|
1209 | * @param layerClass The layer class. This is not {@code Class<? extends Layer>} on purpose,
|
---|
1210 | * to allow asking for layers implementing some interface
|
---|
1211 | * @return list of info on all layers assignable from {@code layerClass}
|
---|
1212 | */
|
---|
1213 | public static List<MultikeyInfo> getLayerInfoByClass(Class<?> layerClass) {
|
---|
1214 | List<MultikeyInfo> result = new ArrayList<>();
|
---|
1215 |
|
---|
1216 | List<Layer> layers = Main.getLayerManager().getLayers();
|
---|
1217 |
|
---|
1218 | int index = 0;
|
---|
1219 | for (Layer l: layers) {
|
---|
1220 | if (layerClass.isAssignableFrom(l.getClass())) {
|
---|
1221 | result.add(new MultikeyInfo(index, l.getName()));
|
---|
1222 | }
|
---|
1223 | index++;
|
---|
1224 | }
|
---|
1225 |
|
---|
1226 | return result;
|
---|
1227 | }
|
---|
1228 |
|
---|
1229 | /**
|
---|
1230 | * Determines if a layer is valid (contained in global layer list).
|
---|
1231 | * @param l the layer
|
---|
1232 | * @return {@code true} if layer {@code l} is contained in current layer list
|
---|
1233 | */
|
---|
1234 | public static boolean isLayerValid(Layer l) {
|
---|
1235 | if (l == null)
|
---|
1236 | return false;
|
---|
1237 |
|
---|
1238 | return Main.getLayerManager().containsLayer(l);
|
---|
1239 | }
|
---|
1240 |
|
---|
1241 | /**
|
---|
1242 | * Returns info about layer.
|
---|
1243 | * @param l the layer
|
---|
1244 | * @return info about layer {@code l}
|
---|
1245 | */
|
---|
1246 | public static MultikeyInfo getLayerInfo(Layer l) {
|
---|
1247 | if (l == null)
|
---|
1248 | return null;
|
---|
1249 |
|
---|
1250 | int index = Main.getLayerManager().getLayers().indexOf(l);
|
---|
1251 | if (index < 0)
|
---|
1252 | return null;
|
---|
1253 |
|
---|
1254 | return new MultikeyInfo(index, l.getName());
|
---|
1255 | }
|
---|
1256 | }
|
---|