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.lang.ref.WeakReference;
|
---|
19 | import java.util.ArrayList;
|
---|
20 | import java.util.Arrays;
|
---|
21 | import java.util.Collections;
|
---|
22 | import java.util.List;
|
---|
23 | import java.util.concurrent.CopyOnWriteArrayList;
|
---|
24 |
|
---|
25 | import javax.swing.AbstractAction;
|
---|
26 | import javax.swing.DefaultCellEditor;
|
---|
27 | import javax.swing.DefaultListSelectionModel;
|
---|
28 | import javax.swing.ImageIcon;
|
---|
29 | import javax.swing.JCheckBox;
|
---|
30 | import javax.swing.JComponent;
|
---|
31 | import javax.swing.JLabel;
|
---|
32 | import javax.swing.JMenuItem;
|
---|
33 | import javax.swing.JPopupMenu;
|
---|
34 | import javax.swing.JSlider;
|
---|
35 | import javax.swing.JTable;
|
---|
36 | import javax.swing.JViewport;
|
---|
37 | import javax.swing.KeyStroke;
|
---|
38 | import javax.swing.ListSelectionModel;
|
---|
39 | import javax.swing.UIManager;
|
---|
40 | import javax.swing.event.ChangeEvent;
|
---|
41 | import javax.swing.event.ChangeListener;
|
---|
42 | import javax.swing.event.ListDataEvent;
|
---|
43 | import javax.swing.event.ListSelectionEvent;
|
---|
44 | import javax.swing.event.ListSelectionListener;
|
---|
45 | import javax.swing.event.TableModelEvent;
|
---|
46 | import javax.swing.event.TableModelListener;
|
---|
47 | import javax.swing.table.AbstractTableModel;
|
---|
48 | import javax.swing.table.DefaultTableCellRenderer;
|
---|
49 | import javax.swing.table.TableCellRenderer;
|
---|
50 | import javax.swing.table.TableModel;
|
---|
51 |
|
---|
52 | import org.openstreetmap.josm.Main;
|
---|
53 | import org.openstreetmap.josm.actions.MergeLayerAction;
|
---|
54 | import org.openstreetmap.josm.gui.MapFrame;
|
---|
55 | import org.openstreetmap.josm.gui.MapView;
|
---|
56 | import org.openstreetmap.josm.gui.SideButton;
|
---|
57 | import org.openstreetmap.josm.gui.help.HelpUtil;
|
---|
58 | import org.openstreetmap.josm.gui.layer.JumpToMarkerActions;
|
---|
59 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
60 | import org.openstreetmap.josm.gui.layer.Layer.LayerAction;
|
---|
61 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
62 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
---|
63 | import org.openstreetmap.josm.gui.widgets.DisableShortcutsOnFocusGainedTextField;
|
---|
64 | import org.openstreetmap.josm.gui.widgets.JosmTextField;
|
---|
65 | import org.openstreetmap.josm.gui.widgets.PopupMenuLauncher;
|
---|
66 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
67 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
68 | import org.openstreetmap.josm.tools.InputMapUtils;
|
---|
69 | import org.openstreetmap.josm.tools.MultikeyActionsHandler;
|
---|
70 | import org.openstreetmap.josm.tools.MultikeyShortcutAction;
|
---|
71 | import org.openstreetmap.josm.tools.MultikeyShortcutAction.MultikeyInfo;
|
---|
72 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
73 |
|
---|
74 | /**
|
---|
75 | * This is a toggle dialog which displays the list of layers. Actions allow to
|
---|
76 | * change the ordering of the layers, to hide/show layers, to activate layers,
|
---|
77 | * and to delete layers.
|
---|
78 | * @since 17
|
---|
79 | */
|
---|
80 | public class LayerListDialog extends ToggleDialog {
|
---|
81 | /** the unique instance of the dialog */
|
---|
82 | private static volatile LayerListDialog instance;
|
---|
83 |
|
---|
84 | /**
|
---|
85 | * Creates the instance of the dialog. It's connected to the map frame <code>mapFrame</code>
|
---|
86 | *
|
---|
87 | * @param mapFrame the map frame
|
---|
88 | */
|
---|
89 | public static void createInstance(MapFrame mapFrame) {
|
---|
90 | if (instance != null)
|
---|
91 | throw new IllegalStateException("Dialog was already created");
|
---|
92 | instance = new LayerListDialog(mapFrame);
|
---|
93 | }
|
---|
94 |
|
---|
95 | /**
|
---|
96 | * Replies the instance of the dialog
|
---|
97 | *
|
---|
98 | * @return the instance of the dialog
|
---|
99 | * @throws IllegalStateException if the dialog is not created yet
|
---|
100 | * @see #createInstance(MapFrame)
|
---|
101 | */
|
---|
102 | public static LayerListDialog getInstance() {
|
---|
103 | if (instance == null)
|
---|
104 | throw new IllegalStateException("Dialog not created yet. Invoke createInstance() first");
|
---|
105 | return instance;
|
---|
106 | }
|
---|
107 |
|
---|
108 | /** the model for the layer list */
|
---|
109 | private LayerListModel model;
|
---|
110 |
|
---|
111 | /** the list of layers (technically its a JTable, but appears like a list) */
|
---|
112 | private LayerList layerList;
|
---|
113 |
|
---|
114 | private SideButton opacityButton;
|
---|
115 |
|
---|
116 | private ActivateLayerAction activateLayerAction;
|
---|
117 | private ShowHideLayerAction showHideLayerAction;
|
---|
118 |
|
---|
119 | //TODO This duplicates ShowHide actions functionality
|
---|
120 | /** stores which layer index to toggle and executes the ShowHide action if the layer is present */
|
---|
121 | private final class ToggleLayerIndexVisibility extends AbstractAction {
|
---|
122 | private int layerIndex = -1;
|
---|
123 | public ToggleLayerIndexVisibility(int layerIndex) {
|
---|
124 | this.layerIndex = layerIndex;
|
---|
125 | }
|
---|
126 | @Override
|
---|
127 | public void actionPerformed(ActionEvent e) {
|
---|
128 | final Layer l = model.getLayer(model.getRowCount() - layerIndex - 1);
|
---|
129 | if (l != null) {
|
---|
130 | l.toggleVisible();
|
---|
131 | }
|
---|
132 | }
|
---|
133 | }
|
---|
134 |
|
---|
135 | private final transient Shortcut[] visibilityToggleShortcuts = new Shortcut[10];
|
---|
136 | private final ToggleLayerIndexVisibility[] visibilityToggleActions = new ToggleLayerIndexVisibility[10];
|
---|
137 |
|
---|
138 | /**
|
---|
139 | * registers (shortcut to toggle right hand side toggle dialogs)+(number keys) shortcuts
|
---|
140 | * to toggle the visibility of the first ten layers.
|
---|
141 | */
|
---|
142 | private final void createVisibilityToggleShortcuts() {
|
---|
143 | final int[] k = { KeyEvent.VK_1, KeyEvent.VK_2, KeyEvent.VK_3, KeyEvent.VK_4,
|
---|
144 | KeyEvent.VK_5, KeyEvent.VK_6, KeyEvent.VK_7, KeyEvent.VK_8,
|
---|
145 | KeyEvent.VK_9, KeyEvent.VK_0 };
|
---|
146 |
|
---|
147 | for(int i=0; i < 10; i++) {
|
---|
148 | visibilityToggleShortcuts[i] = Shortcut.registerShortcut("subwindow:layers:toggleLayer" + (i+1),
|
---|
149 | tr("Toggle visibility of layer: {0}", i+1), k[i], Shortcut.ALT);
|
---|
150 | visibilityToggleActions[i] = new ToggleLayerIndexVisibility(i);
|
---|
151 | Main.registerActionShortcut(visibilityToggleActions[i], visibilityToggleShortcuts[i]);
|
---|
152 | }
|
---|
153 | }
|
---|
154 |
|
---|
155 | /**
|
---|
156 | * Creates a layer list and attach it to the given mapView.
|
---|
157 | */
|
---|
158 | protected LayerListDialog(MapFrame mapFrame) {
|
---|
159 | super(tr("Layers"), "layerlist", tr("Open a list of all loaded layers."),
|
---|
160 | Shortcut.registerShortcut("subwindow:layers", tr("Toggle: {0}", tr("Layers")), KeyEvent.VK_L,
|
---|
161 | Shortcut.ALT_SHIFT), 100, true);
|
---|
162 |
|
---|
163 | // create the models
|
---|
164 | //
|
---|
165 | DefaultListSelectionModel selectionModel = new DefaultListSelectionModel();
|
---|
166 | selectionModel.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
|
---|
167 | model = new LayerListModel(selectionModel);
|
---|
168 |
|
---|
169 | // create the list control
|
---|
170 | //
|
---|
171 | layerList = new LayerList(model);
|
---|
172 | layerList.setSelectionModel(selectionModel);
|
---|
173 | layerList.addMouseListener(new PopupMenuHandler());
|
---|
174 | layerList.setBackground(UIManager.getColor("Button.background"));
|
---|
175 | layerList.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
|
---|
176 | layerList.putClientProperty("JTable.autoStartsEdit", Boolean.FALSE);
|
---|
177 | layerList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
|
---|
178 | layerList.setTableHeader(null);
|
---|
179 | layerList.setShowGrid(false);
|
---|
180 | layerList.setIntercellSpacing(new Dimension(0, 0));
|
---|
181 | layerList.getColumnModel().getColumn(0).setCellRenderer(new ActiveLayerCellRenderer());
|
---|
182 | layerList.getColumnModel().getColumn(0).setCellEditor(new DefaultCellEditor(new ActiveLayerCheckBox()));
|
---|
183 | layerList.getColumnModel().getColumn(0).setMaxWidth(12);
|
---|
184 | layerList.getColumnModel().getColumn(0).setPreferredWidth(12);
|
---|
185 | layerList.getColumnModel().getColumn(0).setResizable(false);
|
---|
186 | layerList.getColumnModel().getColumn(1).setCellRenderer(new LayerVisibleCellRenderer());
|
---|
187 | layerList.getColumnModel().getColumn(1).setCellEditor(new LayerVisibleCellEditor(new LayerVisibleCheckBox()));
|
---|
188 | layerList.getColumnModel().getColumn(1).setMaxWidth(16);
|
---|
189 | layerList.getColumnModel().getColumn(1).setPreferredWidth(16);
|
---|
190 | layerList.getColumnModel().getColumn(1).setResizable(false);
|
---|
191 | layerList.getColumnModel().getColumn(2).setCellRenderer(new LayerNameCellRenderer());
|
---|
192 | layerList.getColumnModel().getColumn(2).setCellEditor(new LayerNameCellEditor(new DisableShortcutsOnFocusGainedTextField()));
|
---|
193 | // Disable some default JTable shortcuts to use JOSM ones (see #5678, #10458)
|
---|
194 | for (KeyStroke ks : new KeyStroke[] {
|
---|
195 | KeyStroke.getKeyStroke(KeyEvent.VK_C, GuiHelper.getMenuShortcutKeyMaskEx()),
|
---|
196 | KeyStroke.getKeyStroke(KeyEvent.VK_V, GuiHelper.getMenuShortcutKeyMaskEx()),
|
---|
197 | KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, InputEvent.SHIFT_DOWN_MASK),
|
---|
198 | KeyStroke.getKeyStroke(KeyEvent.VK_UP, InputEvent.SHIFT_DOWN_MASK),
|
---|
199 | KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, InputEvent.SHIFT_DOWN_MASK),
|
---|
200 | KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, InputEvent.SHIFT_DOWN_MASK),
|
---|
201 | KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, InputEvent.CTRL_DOWN_MASK),
|
---|
202 | KeyStroke.getKeyStroke(KeyEvent.VK_UP, InputEvent.CTRL_DOWN_MASK),
|
---|
203 | KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, InputEvent.CTRL_DOWN_MASK),
|
---|
204 | KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, InputEvent.CTRL_DOWN_MASK),
|
---|
205 | KeyStroke.getKeyStroke(KeyEvent.VK_PAGE_UP, 0),
|
---|
206 | KeyStroke.getKeyStroke(KeyEvent.VK_PAGE_DOWN, 0),
|
---|
207 | KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0),
|
---|
208 | KeyStroke.getKeyStroke(KeyEvent.VK_F8, 0),
|
---|
209 | }) {
|
---|
210 | layerList.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(ks, new Object());
|
---|
211 | }
|
---|
212 |
|
---|
213 | // init the model
|
---|
214 | //
|
---|
215 | final MapView mapView = mapFrame.mapView;
|
---|
216 | model.populate();
|
---|
217 | model.setSelectedLayer(mapView.getActiveLayer());
|
---|
218 | model.addLayerListModelListener(
|
---|
219 | new LayerListModelListener() {
|
---|
220 | @Override
|
---|
221 | public void makeVisible(int row, Layer layer) {
|
---|
222 | layerList.scrollToVisible(row, 0);
|
---|
223 | layerList.repaint();
|
---|
224 | }
|
---|
225 | @Override
|
---|
226 | public void refresh() {
|
---|
227 | layerList.repaint();
|
---|
228 | }
|
---|
229 | }
|
---|
230 | );
|
---|
231 |
|
---|
232 | // -- move up action
|
---|
233 | MoveUpAction moveUpAction = new MoveUpAction();
|
---|
234 | adaptTo(moveUpAction, model);
|
---|
235 | adaptTo(moveUpAction,selectionModel);
|
---|
236 |
|
---|
237 | // -- move down action
|
---|
238 | MoveDownAction moveDownAction = new MoveDownAction();
|
---|
239 | adaptTo(moveDownAction, model);
|
---|
240 | adaptTo(moveDownAction,selectionModel);
|
---|
241 |
|
---|
242 | // -- activate action
|
---|
243 | activateLayerAction = new ActivateLayerAction();
|
---|
244 | activateLayerAction.updateEnabledState();
|
---|
245 | MultikeyActionsHandler.getInstance().addAction(activateLayerAction);
|
---|
246 | adaptTo(activateLayerAction, selectionModel);
|
---|
247 |
|
---|
248 | JumpToMarkerActions.initialize();
|
---|
249 |
|
---|
250 | // -- show hide action
|
---|
251 | showHideLayerAction = new ShowHideLayerAction();
|
---|
252 | MultikeyActionsHandler.getInstance().addAction(showHideLayerAction);
|
---|
253 | adaptTo(showHideLayerAction, selectionModel);
|
---|
254 |
|
---|
255 | // -- layer opacity action
|
---|
256 | LayerOpacityAction layerOpacityAction = new LayerOpacityAction();
|
---|
257 | adaptTo(layerOpacityAction, selectionModel);
|
---|
258 | opacityButton = new SideButton(layerOpacityAction, false);
|
---|
259 |
|
---|
260 | // -- merge layer action
|
---|
261 | MergeAction mergeLayerAction = new MergeAction();
|
---|
262 | adaptTo(mergeLayerAction, model);
|
---|
263 | adaptTo(mergeLayerAction,selectionModel);
|
---|
264 |
|
---|
265 | // -- duplicate layer action
|
---|
266 | DuplicateAction duplicateLayerAction = new DuplicateAction();
|
---|
267 | adaptTo(duplicateLayerAction, model);
|
---|
268 | adaptTo(duplicateLayerAction, selectionModel);
|
---|
269 |
|
---|
270 | // -- delete layer action
|
---|
271 | DeleteLayerAction deleteLayerAction = new DeleteLayerAction();
|
---|
272 | layerList.getActionMap().put("deleteLayer", deleteLayerAction);
|
---|
273 | adaptTo(deleteLayerAction, selectionModel);
|
---|
274 | getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(
|
---|
275 | KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0),"delete"
|
---|
276 | );
|
---|
277 | getActionMap().put("delete", deleteLayerAction);
|
---|
278 |
|
---|
279 | // Activate layer on Enter key press
|
---|
280 | InputMapUtils.addEnterAction(layerList, new AbstractAction() {
|
---|
281 | @Override
|
---|
282 | public void actionPerformed(ActionEvent e) {
|
---|
283 | activateLayerAction.actionPerformed(null);
|
---|
284 | layerList.requestFocus();
|
---|
285 | }
|
---|
286 | });
|
---|
287 |
|
---|
288 | // Show/Activate layer on Enter key press
|
---|
289 | InputMapUtils.addSpacebarAction(layerList, showHideLayerAction);
|
---|
290 |
|
---|
291 | createLayout(layerList, true, Arrays.asList(new SideButton[] {
|
---|
292 | new SideButton(moveUpAction, false),
|
---|
293 | new SideButton(moveDownAction, false),
|
---|
294 | new SideButton(activateLayerAction, false),
|
---|
295 | new SideButton(showHideLayerAction, false),
|
---|
296 | opacityButton,
|
---|
297 | new SideButton(mergeLayerAction, false),
|
---|
298 | new SideButton(duplicateLayerAction, false),
|
---|
299 | new SideButton(deleteLayerAction, false)
|
---|
300 | }));
|
---|
301 |
|
---|
302 | createVisibilityToggleShortcuts();
|
---|
303 | }
|
---|
304 |
|
---|
305 | @Override
|
---|
306 | public void showNotify() {
|
---|
307 | MapView.addLayerChangeListener(activateLayerAction);
|
---|
308 | MapView.addLayerChangeListener(model);
|
---|
309 | model.populate();
|
---|
310 | }
|
---|
311 |
|
---|
312 | @Override
|
---|
313 | public void hideNotify() {
|
---|
314 | MapView.removeLayerChangeListener(model);
|
---|
315 | MapView.removeLayerChangeListener(activateLayerAction);
|
---|
316 | }
|
---|
317 |
|
---|
318 | /**
|
---|
319 | * Returns the layer list model.
|
---|
320 | * @return the layer list model
|
---|
321 | */
|
---|
322 | public LayerListModel getModel() {
|
---|
323 | return model;
|
---|
324 | }
|
---|
325 |
|
---|
326 | protected interface IEnabledStateUpdating {
|
---|
327 | void updateEnabledState();
|
---|
328 | }
|
---|
329 |
|
---|
330 | /**
|
---|
331 | * Wires <code>listener</code> to <code>listSelectionModel</code> in such a way, that
|
---|
332 | * <code>listener</code> receives a {@link IEnabledStateUpdating#updateEnabledState()}
|
---|
333 | * on every {@link ListSelectionEvent}.
|
---|
334 | *
|
---|
335 | * @param listener the listener
|
---|
336 | * @param listSelectionModel the source emitting {@link ListSelectionEvent}s
|
---|
337 | */
|
---|
338 | protected void adaptTo(final IEnabledStateUpdating listener, ListSelectionModel listSelectionModel) {
|
---|
339 | listSelectionModel.addListSelectionListener(
|
---|
340 | new ListSelectionListener() {
|
---|
341 | @Override
|
---|
342 | public void valueChanged(ListSelectionEvent e) {
|
---|
343 | listener.updateEnabledState();
|
---|
344 | }
|
---|
345 | }
|
---|
346 | );
|
---|
347 | }
|
---|
348 |
|
---|
349 | /**
|
---|
350 | * Wires <code>listener</code> to <code>listModel</code> in such a way, that
|
---|
351 | * <code>listener</code> receives a {@link IEnabledStateUpdating#updateEnabledState()}
|
---|
352 | * on every {@link ListDataEvent}.
|
---|
353 | *
|
---|
354 | * @param listener the listener
|
---|
355 | * @param listModel the source emitting {@link ListDataEvent}s
|
---|
356 | */
|
---|
357 | protected void adaptTo(final IEnabledStateUpdating listener, LayerListModel listModel) {
|
---|
358 | listModel.addTableModelListener(
|
---|
359 | new TableModelListener() {
|
---|
360 |
|
---|
361 | @Override
|
---|
362 | public void tableChanged(TableModelEvent e) {
|
---|
363 | listener.updateEnabledState();
|
---|
364 | }
|
---|
365 | }
|
---|
366 | );
|
---|
367 | }
|
---|
368 |
|
---|
369 | @Override
|
---|
370 | public void destroy() {
|
---|
371 | for(int i=0; i < 10; i++) {
|
---|
372 | Main.unregisterActionShortcut(visibilityToggleActions[i], visibilityToggleShortcuts[i]);
|
---|
373 | }
|
---|
374 | MultikeyActionsHandler.getInstance().removeAction(activateLayerAction);
|
---|
375 | MultikeyActionsHandler.getInstance().removeAction(showHideLayerAction);
|
---|
376 | JumpToMarkerActions.unregisterActions();
|
---|
377 | super.destroy();
|
---|
378 | instance = null;
|
---|
379 | }
|
---|
380 |
|
---|
381 | /**
|
---|
382 | * The action to delete the currently selected layer
|
---|
383 | */
|
---|
384 | public final class DeleteLayerAction extends AbstractAction implements IEnabledStateUpdating, LayerAction {
|
---|
385 |
|
---|
386 | /**
|
---|
387 | * Creates a {@link DeleteLayerAction} which will delete the currently
|
---|
388 | * selected layers in the layer dialog.
|
---|
389 | */
|
---|
390 | public DeleteLayerAction() {
|
---|
391 | putValue(SMALL_ICON,ImageProvider.get("dialogs", "delete"));
|
---|
392 | putValue(SHORT_DESCRIPTION, tr("Delete the selected layers."));
|
---|
393 | putValue(NAME, tr("Delete"));
|
---|
394 | putValue("help", HelpUtil.ht("/Dialog/LayerList#DeleteLayer"));
|
---|
395 | updateEnabledState();
|
---|
396 | }
|
---|
397 |
|
---|
398 | @Override
|
---|
399 | public void actionPerformed(ActionEvent e) {
|
---|
400 | List<Layer> selectedLayers = getModel().getSelectedLayers();
|
---|
401 | if (selectedLayers.isEmpty())
|
---|
402 | return;
|
---|
403 | if (!Main.saveUnsavedModifications(selectedLayers, false))
|
---|
404 | return;
|
---|
405 | for (Layer l: selectedLayers) {
|
---|
406 | Main.main.removeLayer(l);
|
---|
407 | }
|
---|
408 | }
|
---|
409 |
|
---|
410 | @Override
|
---|
411 | public void updateEnabledState() {
|
---|
412 | setEnabled(! getModel().getSelectedLayers().isEmpty());
|
---|
413 | }
|
---|
414 |
|
---|
415 | @Override
|
---|
416 | public Component createMenuComponent() {
|
---|
417 | return new JMenuItem(this);
|
---|
418 | }
|
---|
419 |
|
---|
420 | @Override
|
---|
421 | public boolean supportLayers(List<Layer> layers) {
|
---|
422 | return true;
|
---|
423 | }
|
---|
424 |
|
---|
425 | @Override
|
---|
426 | public boolean equals(Object obj) {
|
---|
427 | return obj instanceof DeleteLayerAction;
|
---|
428 | }
|
---|
429 |
|
---|
430 | @Override
|
---|
431 | public int hashCode() {
|
---|
432 | return getClass().hashCode();
|
---|
433 | }
|
---|
434 | }
|
---|
435 |
|
---|
436 | /**
|
---|
437 | * Action which will toggle the visibility of the currently selected layers.
|
---|
438 | */
|
---|
439 | public final class ShowHideLayerAction extends AbstractAction implements IEnabledStateUpdating, LayerAction, MultikeyShortcutAction {
|
---|
440 |
|
---|
441 | private transient WeakReference<Layer> lastLayer;
|
---|
442 | private transient Shortcut multikeyShortcut;
|
---|
443 |
|
---|
444 | /**
|
---|
445 | * Creates a {@link ShowHideLayerAction} which will toggle the visibility of
|
---|
446 | * the currently selected layers
|
---|
447 | */
|
---|
448 | public ShowHideLayerAction() {
|
---|
449 | putValue(NAME, tr("Show/hide"));
|
---|
450 | putValue(SMALL_ICON, ImageProvider.get("dialogs", "showhide"));
|
---|
451 | putValue(SHORT_DESCRIPTION, tr("Toggle visible state of the selected layer."));
|
---|
452 | putValue("help", HelpUtil.ht("/Dialog/LayerList#ShowHideLayer"));
|
---|
453 | multikeyShortcut = Shortcut.registerShortcut("core_multikey:showHideLayer", tr("Multikey: {0}",
|
---|
454 | tr("Show/hide layer")), KeyEvent.VK_S, Shortcut.SHIFT);
|
---|
455 | multikeyShortcut.setAccelerator(this);
|
---|
456 | updateEnabledState();
|
---|
457 | }
|
---|
458 |
|
---|
459 | @Override
|
---|
460 | public Shortcut getMultikeyShortcut() {
|
---|
461 | return multikeyShortcut;
|
---|
462 | }
|
---|
463 |
|
---|
464 | @Override
|
---|
465 | public void actionPerformed(ActionEvent e) {
|
---|
466 | for(Layer l : model.getSelectedLayers()) {
|
---|
467 | l.toggleVisible();
|
---|
468 | }
|
---|
469 | }
|
---|
470 |
|
---|
471 | @Override
|
---|
472 | public void executeMultikeyAction(int index, boolean repeat) {
|
---|
473 | Layer l = LayerListDialog.getLayerForIndex(index);
|
---|
474 | if (l != null) {
|
---|
475 | l.toggleVisible();
|
---|
476 | lastLayer = new WeakReference<>(l);
|
---|
477 | } else if (repeat && lastLayer != null) {
|
---|
478 | l = lastLayer.get();
|
---|
479 | if (LayerListDialog.isLayerValid(l)) {
|
---|
480 | l.toggleVisible();
|
---|
481 | }
|
---|
482 | }
|
---|
483 | }
|
---|
484 |
|
---|
485 | @Override
|
---|
486 | public void updateEnabledState() {
|
---|
487 | setEnabled(!model.getSelectedLayers().isEmpty());
|
---|
488 | }
|
---|
489 |
|
---|
490 | @Override
|
---|
491 | public Component createMenuComponent() {
|
---|
492 | return new JMenuItem(this);
|
---|
493 | }
|
---|
494 |
|
---|
495 | @Override
|
---|
496 | public boolean supportLayers(List<Layer> layers) {
|
---|
497 | return true;
|
---|
498 | }
|
---|
499 |
|
---|
500 | @Override
|
---|
501 | public boolean equals(Object obj) {
|
---|
502 | return obj instanceof ShowHideLayerAction;
|
---|
503 | }
|
---|
504 |
|
---|
505 | @Override
|
---|
506 | public int hashCode() {
|
---|
507 | return getClass().hashCode();
|
---|
508 | }
|
---|
509 |
|
---|
510 | @Override
|
---|
511 | public List<MultikeyInfo> getMultikeyCombinations() {
|
---|
512 | return LayerListDialog.getLayerInfoByClass(Layer.class);
|
---|
513 | }
|
---|
514 |
|
---|
515 | @Override
|
---|
516 | public MultikeyInfo getLastMultikeyAction() {
|
---|
517 | if (lastLayer != null)
|
---|
518 | return LayerListDialog.getLayerInfo(lastLayer.get());
|
---|
519 | return null;
|
---|
520 | }
|
---|
521 | }
|
---|
522 |
|
---|
523 | /**
|
---|
524 | * Action which allows to change the opacity of one or more layers.
|
---|
525 | */
|
---|
526 | public final class LayerOpacityAction extends AbstractAction implements IEnabledStateUpdating, LayerAction {
|
---|
527 | private transient Layer layer;
|
---|
528 | private JPopupMenu popup;
|
---|
529 | private JSlider slider = new JSlider(JSlider.VERTICAL);
|
---|
530 |
|
---|
531 | /**
|
---|
532 | * Creates a {@link LayerOpacityAction} which allows to change the
|
---|
533 | * opacity of one or more layers.
|
---|
534 | *
|
---|
535 | * @param layer the layer. Must not be null.
|
---|
536 | * @throws IllegalArgumentException if layer is null
|
---|
537 | */
|
---|
538 | public LayerOpacityAction(Layer layer) {
|
---|
539 | this();
|
---|
540 | putValue(NAME, tr("Opacity"));
|
---|
541 | CheckParameterUtil.ensureParameterNotNull(layer, "layer");
|
---|
542 | this.layer = layer;
|
---|
543 | updateEnabledState();
|
---|
544 | }
|
---|
545 |
|
---|
546 | /**
|
---|
547 | * Creates a {@link ShowHideLayerAction} which will toggle the visibility of
|
---|
548 | * the currently selected layers
|
---|
549 | *
|
---|
550 | */
|
---|
551 | public LayerOpacityAction() {
|
---|
552 | putValue(NAME, tr("Opacity"));
|
---|
553 | putValue(SHORT_DESCRIPTION, tr("Adjust opacity of the layer."));
|
---|
554 | putValue(SMALL_ICON, ImageProvider.get("dialogs/layerlist", "transparency"));
|
---|
555 | updateEnabledState();
|
---|
556 |
|
---|
557 | popup = new JPopupMenu();
|
---|
558 | slider.addChangeListener(new ChangeListener() {
|
---|
559 | @Override
|
---|
560 | public void stateChanged(ChangeEvent e) {
|
---|
561 | setOpacity((double)slider.getValue()/100);
|
---|
562 | }
|
---|
563 | });
|
---|
564 | popup.add(slider);
|
---|
565 | }
|
---|
566 |
|
---|
567 | private void setOpacity(double value) {
|
---|
568 | if (!isEnabled()) return;
|
---|
569 | if (layer != null) {
|
---|
570 | layer.setOpacity(value);
|
---|
571 | } else {
|
---|
572 | for(Layer layer: model.getSelectedLayers()) {
|
---|
573 | layer.setOpacity(value);
|
---|
574 | }
|
---|
575 | }
|
---|
576 | }
|
---|
577 |
|
---|
578 | private double getOpacity() {
|
---|
579 | if (layer != null)
|
---|
580 | return layer.getOpacity();
|
---|
581 | else {
|
---|
582 | double opacity = 0;
|
---|
583 | List<Layer> layers = model.getSelectedLayers();
|
---|
584 | for(Layer layer: layers) {
|
---|
585 | opacity += layer.getOpacity();
|
---|
586 | }
|
---|
587 | return opacity / layers.size();
|
---|
588 | }
|
---|
589 | }
|
---|
590 |
|
---|
591 | @Override
|
---|
592 | public void actionPerformed(ActionEvent e) {
|
---|
593 | slider.setValue((int)Math.round(getOpacity()*100));
|
---|
594 | if (e.getSource() == opacityButton) {
|
---|
595 | popup.show(opacityButton, 0, opacityButton.getHeight());
|
---|
596 | } else {
|
---|
597 | // Action can be trigger either by opacity button or by popup menu (in case toggle buttons are hidden).
|
---|
598 | // In that case, show it in the middle of screen (because opacityButton is not visible)
|
---|
599 | popup.show(Main.parent, Main.parent.getWidth() / 2, (Main.parent.getHeight() - popup.getHeight()) / 2);
|
---|
600 | }
|
---|
601 | }
|
---|
602 |
|
---|
603 | @Override
|
---|
604 | public void updateEnabledState() {
|
---|
605 | if (layer == null) {
|
---|
606 | setEnabled(! getModel().getSelectedLayers().isEmpty());
|
---|
607 | } else {
|
---|
608 | setEnabled(true);
|
---|
609 | }
|
---|
610 | }
|
---|
611 |
|
---|
612 | @Override
|
---|
613 | public Component createMenuComponent() {
|
---|
614 | return new JMenuItem(this);
|
---|
615 | }
|
---|
616 |
|
---|
617 | @Override
|
---|
618 | public boolean supportLayers(List<Layer> layers) {
|
---|
619 | return true;
|
---|
620 | }
|
---|
621 |
|
---|
622 | @Override
|
---|
623 | public boolean equals(Object obj) {
|
---|
624 | return obj instanceof LayerOpacityAction;
|
---|
625 | }
|
---|
626 |
|
---|
627 | @Override
|
---|
628 | public int hashCode() {
|
---|
629 | return getClass().hashCode();
|
---|
630 | }
|
---|
631 | }
|
---|
632 |
|
---|
633 | /**
|
---|
634 | * The action to activate the currently selected layer
|
---|
635 | */
|
---|
636 |
|
---|
637 | public final class ActivateLayerAction extends AbstractAction implements IEnabledStateUpdating, MapView.LayerChangeListener, MultikeyShortcutAction{
|
---|
638 | private transient Layer layer;
|
---|
639 | private transient Shortcut multikeyShortcut;
|
---|
640 |
|
---|
641 | /**
|
---|
642 | * Constructs a new {@code ActivateLayerAction}.
|
---|
643 | * @param layer the layer
|
---|
644 | */
|
---|
645 | public ActivateLayerAction(Layer layer) {
|
---|
646 | this();
|
---|
647 | CheckParameterUtil.ensureParameterNotNull(layer, "layer");
|
---|
648 | this.layer = layer;
|
---|
649 | putValue(NAME, tr("Activate"));
|
---|
650 | updateEnabledState();
|
---|
651 | }
|
---|
652 |
|
---|
653 | /**
|
---|
654 | * Constructs a new {@code ActivateLayerAction}.
|
---|
655 | */
|
---|
656 | public ActivateLayerAction() {
|
---|
657 | putValue(NAME, tr("Activate"));
|
---|
658 | putValue(SMALL_ICON, ImageProvider.get("dialogs", "activate"));
|
---|
659 | putValue(SHORT_DESCRIPTION, tr("Activate the selected layer"));
|
---|
660 | multikeyShortcut = Shortcut.registerShortcut("core_multikey:activateLayer", tr("Multikey: {0}",
|
---|
661 | tr("Activate layer")), KeyEvent.VK_A, Shortcut.SHIFT);
|
---|
662 | multikeyShortcut.setAccelerator(this);
|
---|
663 | putValue("help", HelpUtil.ht("/Dialog/LayerList#ActivateLayer"));
|
---|
664 | }
|
---|
665 |
|
---|
666 | @Override
|
---|
667 | public Shortcut getMultikeyShortcut() {
|
---|
668 | return multikeyShortcut;
|
---|
669 | }
|
---|
670 |
|
---|
671 | @Override
|
---|
672 | public void actionPerformed(ActionEvent e) {
|
---|
673 | Layer toActivate;
|
---|
674 | if (layer != null) {
|
---|
675 | toActivate = layer;
|
---|
676 | } else {
|
---|
677 | toActivate = model.getSelectedLayers().get(0);
|
---|
678 | }
|
---|
679 | execute(toActivate);
|
---|
680 | }
|
---|
681 |
|
---|
682 | private void execute(Layer layer) {
|
---|
683 | // model is going to be updated via LayerChangeListener and PropertyChangeEvents
|
---|
684 | Main.map.mapView.setActiveLayer(layer);
|
---|
685 | layer.setVisible(true);
|
---|
686 | }
|
---|
687 |
|
---|
688 | protected boolean isActiveLayer(Layer layer) {
|
---|
689 | if (!Main.isDisplayingMapView()) return false;
|
---|
690 | return Main.map.mapView.getActiveLayer() == layer;
|
---|
691 | }
|
---|
692 |
|
---|
693 | @Override
|
---|
694 | public void updateEnabledState() {
|
---|
695 | GuiHelper.runInEDTAndWait(new Runnable() {
|
---|
696 | @Override
|
---|
697 | public void run() {
|
---|
698 | if (layer == null) {
|
---|
699 | if (getModel().getSelectedLayers().size() != 1) {
|
---|
700 | setEnabled(false);
|
---|
701 | return;
|
---|
702 | }
|
---|
703 | Layer selectedLayer = getModel().getSelectedLayers().get(0);
|
---|
704 | setEnabled(!isActiveLayer(selectedLayer));
|
---|
705 | } else {
|
---|
706 | setEnabled(!isActiveLayer(layer));
|
---|
707 | }
|
---|
708 | }
|
---|
709 | });
|
---|
710 | }
|
---|
711 |
|
---|
712 | @Override
|
---|
713 | public void activeLayerChange(Layer oldLayer, Layer newLayer) {
|
---|
714 | updateEnabledState();
|
---|
715 | }
|
---|
716 |
|
---|
717 | @Override
|
---|
718 | public void layerAdded(Layer newLayer) {
|
---|
719 | updateEnabledState();
|
---|
720 | }
|
---|
721 |
|
---|
722 | @Override
|
---|
723 | public void layerRemoved(Layer oldLayer) {
|
---|
724 | updateEnabledState();
|
---|
725 | }
|
---|
726 |
|
---|
727 | @Override
|
---|
728 | public void executeMultikeyAction(int index, boolean repeat) {
|
---|
729 | Layer l = LayerListDialog.getLayerForIndex(index);
|
---|
730 | if (l != null) {
|
---|
731 | execute(l);
|
---|
732 | }
|
---|
733 | }
|
---|
734 |
|
---|
735 | @Override
|
---|
736 | public List<MultikeyInfo> getMultikeyCombinations() {
|
---|
737 | return LayerListDialog.getLayerInfoByClass(Layer.class);
|
---|
738 | }
|
---|
739 |
|
---|
740 | @Override
|
---|
741 | public MultikeyInfo getLastMultikeyAction() {
|
---|
742 | return null; // Repeating action doesn't make much sense for activating
|
---|
743 | }
|
---|
744 | }
|
---|
745 |
|
---|
746 | /**
|
---|
747 | * The action to merge the currently selected layer into another layer.
|
---|
748 | */
|
---|
749 | public final class MergeAction extends AbstractAction implements IEnabledStateUpdating {
|
---|
750 | private transient Layer layer;
|
---|
751 |
|
---|
752 | /**
|
---|
753 | * Constructs a new {@code MergeAction}.
|
---|
754 | * @param layer the layer
|
---|
755 | * @throws IllegalArgumentException if {@code layer} is null
|
---|
756 | */
|
---|
757 | public MergeAction(Layer layer) {
|
---|
758 | this();
|
---|
759 | CheckParameterUtil.ensureParameterNotNull(layer, "layer");
|
---|
760 | this.layer = layer;
|
---|
761 | putValue(NAME, tr("Merge"));
|
---|
762 | updateEnabledState();
|
---|
763 | }
|
---|
764 |
|
---|
765 | /**
|
---|
766 | * Constructs a new {@code MergeAction}.
|
---|
767 | */
|
---|
768 | public MergeAction() {
|
---|
769 | putValue(NAME, tr("Merge"));
|
---|
770 | putValue(SMALL_ICON, ImageProvider.get("dialogs", "mergedown"));
|
---|
771 | putValue(SHORT_DESCRIPTION, tr("Merge this layer into another layer"));
|
---|
772 | putValue("help", HelpUtil.ht("/Dialog/LayerList#MergeLayer"));
|
---|
773 | updateEnabledState();
|
---|
774 | }
|
---|
775 |
|
---|
776 | @Override
|
---|
777 | public void actionPerformed(ActionEvent e) {
|
---|
778 | if (layer != null) {
|
---|
779 | Main.main.menu.merge.merge(layer);
|
---|
780 | } else {
|
---|
781 | if (getModel().getSelectedLayers().size() == 1) {
|
---|
782 | Layer selectedLayer = getModel().getSelectedLayers().get(0);
|
---|
783 | Main.main.menu.merge.merge(selectedLayer);
|
---|
784 | } else {
|
---|
785 | Main.main.menu.merge.merge(getModel().getSelectedLayers());
|
---|
786 | }
|
---|
787 | }
|
---|
788 | }
|
---|
789 |
|
---|
790 | protected boolean isActiveLayer(Layer layer) {
|
---|
791 | if (!Main.isDisplayingMapView()) return false;
|
---|
792 | return Main.map.mapView.getActiveLayer() == layer;
|
---|
793 | }
|
---|
794 |
|
---|
795 | @Override
|
---|
796 | public void updateEnabledState() {
|
---|
797 | if (layer == null) {
|
---|
798 | if (getModel().getSelectedLayers().isEmpty()) {
|
---|
799 | setEnabled(false);
|
---|
800 | } else if (getModel().getSelectedLayers().size() > 1) {
|
---|
801 | Layer firstLayer = getModel().getSelectedLayers().get(0);
|
---|
802 | for (Layer l: getModel().getSelectedLayers()) {
|
---|
803 | if (l != firstLayer && (!l.isMergable(firstLayer) || !firstLayer.isMergable(l))) {
|
---|
804 | setEnabled(false);
|
---|
805 | return;
|
---|
806 | }
|
---|
807 | }
|
---|
808 | setEnabled(true);
|
---|
809 | } else {
|
---|
810 | Layer selectedLayer = getModel().getSelectedLayers().get(0);
|
---|
811 | List<Layer> targets = getModel().getPossibleMergeTargets(selectedLayer);
|
---|
812 | setEnabled(!targets.isEmpty());
|
---|
813 | }
|
---|
814 | } else {
|
---|
815 | List<Layer> targets = getModel().getPossibleMergeTargets(layer);
|
---|
816 | setEnabled(!targets.isEmpty());
|
---|
817 | }
|
---|
818 | }
|
---|
819 | }
|
---|
820 |
|
---|
821 | /**
|
---|
822 | * The action to merge the currently selected layer into another layer.
|
---|
823 | */
|
---|
824 | public final class DuplicateAction extends AbstractAction implements IEnabledStateUpdating {
|
---|
825 | private transient Layer layer;
|
---|
826 |
|
---|
827 | /**
|
---|
828 | * Constructs a new {@code DuplicateAction}.
|
---|
829 | * @param layer the layer
|
---|
830 | * @throws IllegalArgumentException if {@code layer} is null
|
---|
831 | */
|
---|
832 | public DuplicateAction(Layer layer) {
|
---|
833 | this();
|
---|
834 | CheckParameterUtil.ensureParameterNotNull(layer, "layer");
|
---|
835 | this.layer = layer;
|
---|
836 | updateEnabledState();
|
---|
837 | }
|
---|
838 |
|
---|
839 | /**
|
---|
840 | * Constructs a new {@code DuplicateAction}.
|
---|
841 | */
|
---|
842 | public DuplicateAction() {
|
---|
843 | putValue(NAME, tr("Duplicate"));
|
---|
844 | putValue(SMALL_ICON, ImageProvider.get("dialogs", "duplicatelayer"));
|
---|
845 | putValue(SHORT_DESCRIPTION, tr("Duplicate this layer"));
|
---|
846 | putValue("help", HelpUtil.ht("/Dialog/LayerList#DuplicateLayer"));
|
---|
847 | updateEnabledState();
|
---|
848 | }
|
---|
849 |
|
---|
850 | private void duplicate(Layer layer) {
|
---|
851 | if (!Main.isDisplayingMapView())
|
---|
852 | return;
|
---|
853 |
|
---|
854 | List<String> layerNames = new ArrayList<>();
|
---|
855 | for (Layer l: Main.map.mapView.getAllLayers()) {
|
---|
856 | layerNames.add(l.getName());
|
---|
857 | }
|
---|
858 | if (layer instanceof OsmDataLayer) {
|
---|
859 | OsmDataLayer oldLayer = (OsmDataLayer)layer;
|
---|
860 | // Translators: "Copy of {layer name}"
|
---|
861 | String newName = tr("Copy of {0}", oldLayer.getName());
|
---|
862 | int i = 2;
|
---|
863 | while (layerNames.contains(newName)) {
|
---|
864 | // Translators: "Copy {number} of {layer name}"
|
---|
865 | newName = tr("Copy {1} of {0}", oldLayer.getName(), i);
|
---|
866 | i++;
|
---|
867 | }
|
---|
868 | Main.main.addLayer(new OsmDataLayer(oldLayer.data.clone(), newName, null));
|
---|
869 | }
|
---|
870 | }
|
---|
871 |
|
---|
872 | @Override
|
---|
873 | public void actionPerformed(ActionEvent e) {
|
---|
874 | if (layer != null) {
|
---|
875 | duplicate(layer);
|
---|
876 | } else {
|
---|
877 | duplicate(getModel().getSelectedLayers().get(0));
|
---|
878 | }
|
---|
879 | }
|
---|
880 |
|
---|
881 | protected boolean isActiveLayer(Layer layer) {
|
---|
882 | if (!Main.isDisplayingMapView())
|
---|
883 | return false;
|
---|
884 | return Main.map.mapView.getActiveLayer() == layer;
|
---|
885 | }
|
---|
886 |
|
---|
887 | @Override
|
---|
888 | public void updateEnabledState() {
|
---|
889 | if (layer == null) {
|
---|
890 | if (getModel().getSelectedLayers().size() == 1) {
|
---|
891 | setEnabled(getModel().getSelectedLayers().get(0) instanceof OsmDataLayer);
|
---|
892 | } else {
|
---|
893 | setEnabled(false);
|
---|
894 | }
|
---|
895 | } else {
|
---|
896 | setEnabled(layer instanceof OsmDataLayer);
|
---|
897 | }
|
---|
898 | }
|
---|
899 | }
|
---|
900 |
|
---|
901 | private static class ActiveLayerCheckBox extends JCheckBox {
|
---|
902 | public ActiveLayerCheckBox() {
|
---|
903 | setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
|
---|
904 | ImageIcon blank = ImageProvider.get("dialogs/layerlist", "blank");
|
---|
905 | ImageIcon active = ImageProvider.get("dialogs/layerlist", "active");
|
---|
906 | setIcon(blank);
|
---|
907 | setSelectedIcon(active);
|
---|
908 | setRolloverIcon(blank);
|
---|
909 | setRolloverSelectedIcon(active);
|
---|
910 | setPressedIcon(ImageProvider.get("dialogs/layerlist", "active-pressed"));
|
---|
911 | }
|
---|
912 | }
|
---|
913 |
|
---|
914 | private static class LayerVisibleCheckBox extends JCheckBox {
|
---|
915 | private final ImageIcon iconEye;
|
---|
916 | private final ImageIcon iconEyeTranslucent;
|
---|
917 | private boolean isTranslucent;
|
---|
918 | public LayerVisibleCheckBox() {
|
---|
919 | setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
|
---|
920 | iconEye = ImageProvider.get("dialogs/layerlist", "eye");
|
---|
921 | iconEyeTranslucent = ImageProvider.get("dialogs/layerlist", "eye-translucent");
|
---|
922 | setIcon(ImageProvider.get("dialogs/layerlist", "eye-off"));
|
---|
923 | setPressedIcon(ImageProvider.get("dialogs/layerlist", "eye-pressed"));
|
---|
924 | setSelectedIcon(iconEye);
|
---|
925 | isTranslucent = false;
|
---|
926 | }
|
---|
927 |
|
---|
928 | public void setTranslucent(boolean isTranslucent) {
|
---|
929 | if (this.isTranslucent == isTranslucent) return;
|
---|
930 | if (isTranslucent) {
|
---|
931 | setSelectedIcon(iconEyeTranslucent);
|
---|
932 | } else {
|
---|
933 | setSelectedIcon(iconEye);
|
---|
934 | }
|
---|
935 | this.isTranslucent = isTranslucent;
|
---|
936 | }
|
---|
937 |
|
---|
938 | public void updateStatus(Layer layer) {
|
---|
939 | boolean visible = layer.isVisible();
|
---|
940 | setSelected(visible);
|
---|
941 | setTranslucent(layer.getOpacity()<1.0);
|
---|
942 | setToolTipText(visible ? tr("layer is currently visible (click to hide layer)") : tr("layer is currently hidden (click to show layer)"));
|
---|
943 | }
|
---|
944 | }
|
---|
945 |
|
---|
946 | private static class ActiveLayerCellRenderer implements TableCellRenderer {
|
---|
947 | private final JCheckBox cb;
|
---|
948 | public ActiveLayerCellRenderer() {
|
---|
949 | cb = new ActiveLayerCheckBox();
|
---|
950 | }
|
---|
951 |
|
---|
952 | @Override
|
---|
953 | public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
|
---|
954 | boolean active = value != null && (Boolean) value;
|
---|
955 | cb.setSelected(active);
|
---|
956 | cb.setToolTipText(active ? tr("this layer is the active layer") : tr("this layer is not currently active (click to activate)"));
|
---|
957 | return cb;
|
---|
958 | }
|
---|
959 | }
|
---|
960 |
|
---|
961 | private static class LayerVisibleCellRenderer implements TableCellRenderer {
|
---|
962 | private final LayerVisibleCheckBox cb;
|
---|
963 | public LayerVisibleCellRenderer() {
|
---|
964 | this.cb = new LayerVisibleCheckBox();
|
---|
965 | }
|
---|
966 |
|
---|
967 | @Override
|
---|
968 | public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
|
---|
969 | if (value != null) {
|
---|
970 | cb.updateStatus((Layer)value);
|
---|
971 | }
|
---|
972 | return cb;
|
---|
973 | }
|
---|
974 | }
|
---|
975 |
|
---|
976 | private static class LayerVisibleCellEditor extends DefaultCellEditor {
|
---|
977 | private final LayerVisibleCheckBox cb;
|
---|
978 | public LayerVisibleCellEditor(LayerVisibleCheckBox cb) {
|
---|
979 | super(cb);
|
---|
980 | this.cb = cb;
|
---|
981 | }
|
---|
982 |
|
---|
983 | @Override
|
---|
984 | public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
|
---|
985 | cb.updateStatus((Layer)value);
|
---|
986 | return cb;
|
---|
987 | }
|
---|
988 | }
|
---|
989 |
|
---|
990 | private class LayerNameCellRenderer extends DefaultTableCellRenderer {
|
---|
991 |
|
---|
992 | protected boolean isActiveLayer(Layer layer) {
|
---|
993 | if (!Main.isDisplayingMapView()) return false;
|
---|
994 | return Main.map.mapView.getActiveLayer() == layer;
|
---|
995 | }
|
---|
996 |
|
---|
997 | @Override
|
---|
998 | public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
|
---|
999 | if (value == null)
|
---|
1000 | return this;
|
---|
1001 | Layer layer = (Layer)value;
|
---|
1002 | JLabel label = (JLabel)super.getTableCellRendererComponent(table,
|
---|
1003 | layer.getName(), isSelected, hasFocus, row, column);
|
---|
1004 | if (isActiveLayer(layer)) {
|
---|
1005 | label.setFont(label.getFont().deriveFont(Font.BOLD));
|
---|
1006 | }
|
---|
1007 | if(Main.pref.getBoolean("dialog.layer.colorname", true)) {
|
---|
1008 | Color c = layer.getColor(false);
|
---|
1009 | if(c != null) {
|
---|
1010 | Color oc = null;
|
---|
1011 | for(Layer l : model.getLayers()) {
|
---|
1012 | oc = l.getColor(false);
|
---|
1013 | if(oc != null) {
|
---|
1014 | if(oc.equals(c)) {
|
---|
1015 | oc = null;
|
---|
1016 | } else {
|
---|
1017 | break;
|
---|
1018 | }
|
---|
1019 | }
|
---|
1020 | }
|
---|
1021 | /* not more than one color, don't use coloring */
|
---|
1022 | if(oc == null) {
|
---|
1023 | c = null;
|
---|
1024 | }
|
---|
1025 | }
|
---|
1026 | if(c == null) {
|
---|
1027 | c = Main.pref.getUIColor(isSelected ? "Table.selectionForeground" : "Table.foreground");
|
---|
1028 | }
|
---|
1029 | label.setForeground(c);
|
---|
1030 | }
|
---|
1031 | label.setIcon(layer.getIcon());
|
---|
1032 | label.setToolTipText(layer.getToolTipText());
|
---|
1033 | return label;
|
---|
1034 | }
|
---|
1035 | }
|
---|
1036 |
|
---|
1037 | private static class LayerNameCellEditor extends DefaultCellEditor {
|
---|
1038 | public LayerNameCellEditor(DisableShortcutsOnFocusGainedTextField tf) {
|
---|
1039 | super(tf);
|
---|
1040 | }
|
---|
1041 |
|
---|
1042 | @Override
|
---|
1043 | public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
|
---|
1044 | JosmTextField tf = (JosmTextField) super.getTableCellEditorComponent(table, value, isSelected, row, column);
|
---|
1045 | tf.setText(value == null ? "" : ((Layer) value).getName());
|
---|
1046 | return tf;
|
---|
1047 | }
|
---|
1048 | }
|
---|
1049 |
|
---|
1050 | class PopupMenuHandler extends PopupMenuLauncher {
|
---|
1051 | @Override
|
---|
1052 | public void showMenu(MouseEvent evt) {
|
---|
1053 | Layer layer = getModel().getLayer(layerList.getSelectedRow());
|
---|
1054 | menu = new LayerListPopup(getModel().getSelectedLayers(), layer);
|
---|
1055 | super.showMenu(evt);
|
---|
1056 | }
|
---|
1057 | }
|
---|
1058 |
|
---|
1059 | /**
|
---|
1060 | * The action to move up the currently selected entries in the list.
|
---|
1061 | */
|
---|
1062 | class MoveUpAction extends AbstractAction implements IEnabledStateUpdating{
|
---|
1063 | public MoveUpAction() {
|
---|
1064 | putValue(NAME, tr("Move up"));
|
---|
1065 | putValue(SMALL_ICON, ImageProvider.get("dialogs", "up"));
|
---|
1066 | putValue(SHORT_DESCRIPTION, tr("Move the selected layer one row up."));
|
---|
1067 | updateEnabledState();
|
---|
1068 | }
|
---|
1069 |
|
---|
1070 | @Override
|
---|
1071 | public void updateEnabledState() {
|
---|
1072 | setEnabled(model.canMoveUp());
|
---|
1073 | }
|
---|
1074 |
|
---|
1075 | @Override
|
---|
1076 | public void actionPerformed(ActionEvent e) {
|
---|
1077 | model.moveUp();
|
---|
1078 | }
|
---|
1079 | }
|
---|
1080 |
|
---|
1081 | /**
|
---|
1082 | * The action to move down the currently selected entries in the list.
|
---|
1083 | */
|
---|
1084 | class MoveDownAction extends AbstractAction implements IEnabledStateUpdating {
|
---|
1085 | public MoveDownAction() {
|
---|
1086 | putValue(NAME, tr("Move down"));
|
---|
1087 | putValue(SMALL_ICON, ImageProvider.get("dialogs", "down"));
|
---|
1088 | putValue(SHORT_DESCRIPTION, tr("Move the selected layer one row down."));
|
---|
1089 | updateEnabledState();
|
---|
1090 | }
|
---|
1091 |
|
---|
1092 | @Override
|
---|
1093 | public void updateEnabledState() {
|
---|
1094 | setEnabled(model.canMoveDown());
|
---|
1095 | }
|
---|
1096 |
|
---|
1097 | @Override
|
---|
1098 | public void actionPerformed(ActionEvent e) {
|
---|
1099 | model.moveDown();
|
---|
1100 | }
|
---|
1101 | }
|
---|
1102 |
|
---|
1103 | /**
|
---|
1104 | * Observer interface to be implemented by views using {@link LayerListModel}.
|
---|
1105 | */
|
---|
1106 | public interface LayerListModelListener {
|
---|
1107 |
|
---|
1108 | /**
|
---|
1109 | * Fired when a layer is made visible.
|
---|
1110 | * @param index the layer index
|
---|
1111 | * @param layer the layer
|
---|
1112 | */
|
---|
1113 | public void makeVisible(int index, Layer layer);
|
---|
1114 |
|
---|
1115 |
|
---|
1116 | /**
|
---|
1117 | * Fired when something has changed in the layer list model.
|
---|
1118 | */
|
---|
1119 | public void refresh();
|
---|
1120 | }
|
---|
1121 |
|
---|
1122 | /**
|
---|
1123 | * The layer list model. The model manages a list of layers and provides methods for
|
---|
1124 | * moving layers up and down, for toggling their visibility, and for activating a layer.
|
---|
1125 | *
|
---|
1126 | * The model is a {@link TableModel} and it provides a {@link ListSelectionModel}. It expects
|
---|
1127 | * to be configured with a {@link DefaultListSelectionModel}. The selection model is used
|
---|
1128 | * to update the selection state of views depending on messages sent to the model.
|
---|
1129 | *
|
---|
1130 | * The model manages a list of {@link LayerListModelListener} which are mainly notified if
|
---|
1131 | * the model requires views to make a specific list entry visible.
|
---|
1132 | *
|
---|
1133 | * It also listens to {@link PropertyChangeEvent}s of every {@link Layer} it manages, in particular to
|
---|
1134 | * the properties {@link Layer#VISIBLE_PROP} and {@link Layer#NAME_PROP}.
|
---|
1135 | */
|
---|
1136 | public final class LayerListModel extends AbstractTableModel implements MapView.LayerChangeListener, PropertyChangeListener {
|
---|
1137 | /** manages list selection state*/
|
---|
1138 | private DefaultListSelectionModel selectionModel;
|
---|
1139 | private CopyOnWriteArrayList<LayerListModelListener> listeners;
|
---|
1140 |
|
---|
1141 | /**
|
---|
1142 | * constructor
|
---|
1143 | *
|
---|
1144 | * @param selectionModel the list selection model
|
---|
1145 | */
|
---|
1146 | private LayerListModel(DefaultListSelectionModel selectionModel) {
|
---|
1147 | this.selectionModel = selectionModel;
|
---|
1148 | listeners = new CopyOnWriteArrayList<>();
|
---|
1149 | }
|
---|
1150 |
|
---|
1151 | /**
|
---|
1152 | * Adds a listener to this model
|
---|
1153 | *
|
---|
1154 | * @param listener the listener
|
---|
1155 | */
|
---|
1156 | public void addLayerListModelListener(LayerListModelListener listener) {
|
---|
1157 | if (listener != null) {
|
---|
1158 | listeners.addIfAbsent(listener);
|
---|
1159 | }
|
---|
1160 | }
|
---|
1161 |
|
---|
1162 | /**
|
---|
1163 | * removes a listener from this model
|
---|
1164 | * @param listener the listener
|
---|
1165 | *
|
---|
1166 | */
|
---|
1167 | public void removeLayerListModelListener(LayerListModelListener listener) {
|
---|
1168 | listeners.remove(listener);
|
---|
1169 | }
|
---|
1170 |
|
---|
1171 | /**
|
---|
1172 | * Fires a make visible event to listeners
|
---|
1173 | *
|
---|
1174 | * @param index the index of the row to make visible
|
---|
1175 | * @param layer the layer at this index
|
---|
1176 | * @see LayerListModelListener#makeVisible(int, Layer)
|
---|
1177 | */
|
---|
1178 | protected void fireMakeVisible(int index, Layer layer) {
|
---|
1179 | for (LayerListModelListener listener : listeners) {
|
---|
1180 | listener.makeVisible(index, layer);
|
---|
1181 | }
|
---|
1182 | }
|
---|
1183 |
|
---|
1184 | /**
|
---|
1185 | * Fires a refresh event to listeners of this model
|
---|
1186 | *
|
---|
1187 | * @see LayerListModelListener#refresh()
|
---|
1188 | */
|
---|
1189 | protected void fireRefresh() {
|
---|
1190 | for (LayerListModelListener listener : listeners) {
|
---|
1191 | listener.refresh();
|
---|
1192 | }
|
---|
1193 | }
|
---|
1194 |
|
---|
1195 | /**
|
---|
1196 | * Populates the model with the current layers managed by {@link MapView}.
|
---|
1197 | */
|
---|
1198 | public void populate() {
|
---|
1199 | for (Layer layer: getLayers()) {
|
---|
1200 | // make sure the model is registered exactly once
|
---|
1201 | layer.removePropertyChangeListener(this);
|
---|
1202 | layer.addPropertyChangeListener(this);
|
---|
1203 | }
|
---|
1204 | fireTableDataChanged();
|
---|
1205 | }
|
---|
1206 |
|
---|
1207 | /**
|
---|
1208 | * Marks <code>layer</code> as selected layer. Ignored, if layer is null.
|
---|
1209 | *
|
---|
1210 | * @param layer the layer.
|
---|
1211 | */
|
---|
1212 | public void setSelectedLayer(Layer layer) {
|
---|
1213 | if (layer == null)
|
---|
1214 | return;
|
---|
1215 | int idx = getLayers().indexOf(layer);
|
---|
1216 | if (idx >= 0) {
|
---|
1217 | selectionModel.setSelectionInterval(idx, idx);
|
---|
1218 | }
|
---|
1219 | ensureSelectedIsVisible();
|
---|
1220 | }
|
---|
1221 |
|
---|
1222 | /**
|
---|
1223 | * Replies the list of currently selected layers. Never null, but may be empty.
|
---|
1224 | *
|
---|
1225 | * @return the list of currently selected layers. Never null, but may be empty.
|
---|
1226 | */
|
---|
1227 | public List<Layer> getSelectedLayers() {
|
---|
1228 | List<Layer> selected = new ArrayList<>();
|
---|
1229 | List<Layer> layers = getLayers();
|
---|
1230 | for (int i=0; i<layers.size(); i++) {
|
---|
1231 | if (selectionModel.isSelectedIndex(i)) {
|
---|
1232 | selected.add(layers.get(i));
|
---|
1233 | }
|
---|
1234 | }
|
---|
1235 | return selected;
|
---|
1236 | }
|
---|
1237 |
|
---|
1238 | /**
|
---|
1239 | * Replies a the list of indices of the selected rows. Never null,
|
---|
1240 | * but may be empty.
|
---|
1241 | *
|
---|
1242 | * @return the list of indices of the selected rows. Never null,
|
---|
1243 | * but may be empty.
|
---|
1244 | */
|
---|
1245 | public List<Integer> getSelectedRows() {
|
---|
1246 | List<Integer> selected = new ArrayList<>();
|
---|
1247 | for (int i=0; i<getLayers().size();i++) {
|
---|
1248 | if (selectionModel.isSelectedIndex(i)) {
|
---|
1249 | selected.add(i);
|
---|
1250 | }
|
---|
1251 | }
|
---|
1252 | return selected;
|
---|
1253 | }
|
---|
1254 |
|
---|
1255 | /**
|
---|
1256 | * Invoked if a layer managed by {@link MapView} is removed
|
---|
1257 | *
|
---|
1258 | * @param layer the layer which is removed
|
---|
1259 | */
|
---|
1260 | protected void onRemoveLayer(Layer layer) {
|
---|
1261 | if (layer == null)
|
---|
1262 | return;
|
---|
1263 | layer.removePropertyChangeListener(this);
|
---|
1264 | final int size = getRowCount();
|
---|
1265 | final List<Integer> rows = getSelectedRows();
|
---|
1266 | GuiHelper.runInEDTAndWait(new Runnable() {
|
---|
1267 | @Override
|
---|
1268 | public void run() {
|
---|
1269 | if (rows.isEmpty() && size > 0) {
|
---|
1270 | selectionModel.setSelectionInterval(size-1, size-1);
|
---|
1271 | }
|
---|
1272 | fireTableDataChanged();
|
---|
1273 | fireRefresh();
|
---|
1274 | ensureActiveSelected();
|
---|
1275 | }
|
---|
1276 | });
|
---|
1277 | }
|
---|
1278 |
|
---|
1279 | /**
|
---|
1280 | * Invoked when a layer managed by {@link MapView} is added
|
---|
1281 | *
|
---|
1282 | * @param layer the layer
|
---|
1283 | */
|
---|
1284 | protected void onAddLayer(Layer layer) {
|
---|
1285 | if (layer == null) return;
|
---|
1286 | layer.addPropertyChangeListener(this);
|
---|
1287 | fireTableDataChanged();
|
---|
1288 | int idx = getLayers().indexOf(layer);
|
---|
1289 | layerList.setRowHeight(idx, Math.max(16, layer.getIcon().getIconHeight()));
|
---|
1290 | selectionModel.setSelectionInterval(idx, idx);
|
---|
1291 | ensureSelectedIsVisible();
|
---|
1292 | }
|
---|
1293 |
|
---|
1294 | /**
|
---|
1295 | * Replies the first layer. Null if no layers are present
|
---|
1296 | *
|
---|
1297 | * @return the first layer. Null if no layers are present
|
---|
1298 | */
|
---|
1299 | public Layer getFirstLayer() {
|
---|
1300 | if (getRowCount() == 0) return null;
|
---|
1301 | return getLayers().get(0);
|
---|
1302 | }
|
---|
1303 |
|
---|
1304 | /**
|
---|
1305 | * Replies the layer at position <code>index</code>
|
---|
1306 | *
|
---|
1307 | * @param index the index
|
---|
1308 | * @return the layer at position <code>index</code>. Null,
|
---|
1309 | * if index is out of range.
|
---|
1310 | */
|
---|
1311 | public Layer getLayer(int index) {
|
---|
1312 | if (index < 0 || index >= getRowCount())
|
---|
1313 | return null;
|
---|
1314 | return getLayers().get(index);
|
---|
1315 | }
|
---|
1316 |
|
---|
1317 | /**
|
---|
1318 | * Replies true if the currently selected layers can move up by one position
|
---|
1319 | *
|
---|
1320 | * @return true if the currently selected layers can move up by one position
|
---|
1321 | */
|
---|
1322 | public boolean canMoveUp() {
|
---|
1323 | List<Integer> sel = getSelectedRows();
|
---|
1324 | return !sel.isEmpty() && sel.get(0) > 0;
|
---|
1325 | }
|
---|
1326 |
|
---|
1327 | /**
|
---|
1328 | * Move up the currently selected layers by one position
|
---|
1329 | *
|
---|
1330 | */
|
---|
1331 | public void moveUp() {
|
---|
1332 | if (!canMoveUp()) return;
|
---|
1333 | List<Integer> sel = getSelectedRows();
|
---|
1334 | List<Layer> layers = getLayers();
|
---|
1335 | for (int row : sel) {
|
---|
1336 | Layer l1 = layers.get(row);
|
---|
1337 | Layer l2 = layers.get(row-1);
|
---|
1338 | Main.map.mapView.moveLayer(l2, row);
|
---|
1339 | Main.map.mapView.moveLayer(l1, row-1);
|
---|
1340 | }
|
---|
1341 | fireTableDataChanged();
|
---|
1342 | selectionModel.clearSelection();
|
---|
1343 | for (int row : sel) {
|
---|
1344 | selectionModel.addSelectionInterval(row-1, row-1);
|
---|
1345 | }
|
---|
1346 | ensureSelectedIsVisible();
|
---|
1347 | }
|
---|
1348 |
|
---|
1349 | /**
|
---|
1350 | * Replies true if the currently selected layers can move down by one position
|
---|
1351 | *
|
---|
1352 | * @return true if the currently selected layers can move down by one position
|
---|
1353 | */
|
---|
1354 | public boolean canMoveDown() {
|
---|
1355 | List<Integer> sel = getSelectedRows();
|
---|
1356 | return !sel.isEmpty() && sel.get(sel.size()-1) < getLayers().size()-1;
|
---|
1357 | }
|
---|
1358 |
|
---|
1359 | /**
|
---|
1360 | * Move down the currently selected layers by one position
|
---|
1361 | *
|
---|
1362 | */
|
---|
1363 | public void moveDown() {
|
---|
1364 | if (!canMoveDown()) return;
|
---|
1365 | List<Integer> sel = getSelectedRows();
|
---|
1366 | Collections.reverse(sel);
|
---|
1367 | List<Layer> layers = getLayers();
|
---|
1368 | for (int row : sel) {
|
---|
1369 | Layer l1 = layers.get(row);
|
---|
1370 | Layer l2 = layers.get(row+1);
|
---|
1371 | Main.map.mapView.moveLayer(l1, row+1);
|
---|
1372 | Main.map.mapView.moveLayer(l2, row);
|
---|
1373 | }
|
---|
1374 | fireTableDataChanged();
|
---|
1375 | selectionModel.clearSelection();
|
---|
1376 | for (int row : sel) {
|
---|
1377 | selectionModel.addSelectionInterval(row+1, row+1);
|
---|
1378 | }
|
---|
1379 | ensureSelectedIsVisible();
|
---|
1380 | }
|
---|
1381 |
|
---|
1382 | /**
|
---|
1383 | * Make sure the first of the selected layers is visible in the
|
---|
1384 | * views of this model.
|
---|
1385 | *
|
---|
1386 | */
|
---|
1387 | protected void ensureSelectedIsVisible() {
|
---|
1388 | int index = selectionModel.getMinSelectionIndex();
|
---|
1389 | if (index < 0) return;
|
---|
1390 | List<Layer> layers = getLayers();
|
---|
1391 | if (index >= layers.size()) return;
|
---|
1392 | Layer layer = layers.get(index);
|
---|
1393 | fireMakeVisible(index, layer);
|
---|
1394 | }
|
---|
1395 |
|
---|
1396 | /**
|
---|
1397 | * Replies a list of layers which are possible merge targets
|
---|
1398 | * for <code>source</code>
|
---|
1399 | *
|
---|
1400 | * @param source the source layer
|
---|
1401 | * @return a list of layers which are possible merge targets
|
---|
1402 | * for <code>source</code>. Never null, but can be empty.
|
---|
1403 | */
|
---|
1404 | public List<Layer> getPossibleMergeTargets(Layer source) {
|
---|
1405 | List<Layer> targets = new ArrayList<>();
|
---|
1406 | if (source == null)
|
---|
1407 | return targets;
|
---|
1408 | for (Layer target : getLayers()) {
|
---|
1409 | if (source == target) {
|
---|
1410 | continue;
|
---|
1411 | }
|
---|
1412 | if (target.isMergable(source) && source.isMergable(target)) {
|
---|
1413 | targets.add(target);
|
---|
1414 | }
|
---|
1415 | }
|
---|
1416 | return targets;
|
---|
1417 | }
|
---|
1418 |
|
---|
1419 | /**
|
---|
1420 | * Replies the list of layers currently managed by {@link MapView}.
|
---|
1421 | * Never null, but can be empty.
|
---|
1422 | *
|
---|
1423 | * @return the list of layers currently managed by {@link MapView}.
|
---|
1424 | * Never null, but can be empty.
|
---|
1425 | */
|
---|
1426 | public List<Layer> getLayers() {
|
---|
1427 | if (!Main.isDisplayingMapView())
|
---|
1428 | return Collections.<Layer>emptyList();
|
---|
1429 | return Main.map.mapView.getAllLayersAsList();
|
---|
1430 | }
|
---|
1431 |
|
---|
1432 | /**
|
---|
1433 | * Ensures that at least one layer is selected in the layer dialog
|
---|
1434 | *
|
---|
1435 | */
|
---|
1436 | protected void ensureActiveSelected() {
|
---|
1437 | List<Layer> layers = getLayers();
|
---|
1438 | if (layers.isEmpty())
|
---|
1439 | return;
|
---|
1440 | final Layer activeLayer = getActiveLayer();
|
---|
1441 | if (activeLayer != null) {
|
---|
1442 | // there's an active layer - select it and make it visible
|
---|
1443 | int idx = layers.indexOf(activeLayer);
|
---|
1444 | selectionModel.setSelectionInterval(idx, idx);
|
---|
1445 | ensureSelectedIsVisible();
|
---|
1446 | } else {
|
---|
1447 | // no active layer - select the first one and make it visible
|
---|
1448 | selectionModel.setSelectionInterval(0, 0);
|
---|
1449 | ensureSelectedIsVisible();
|
---|
1450 | }
|
---|
1451 | }
|
---|
1452 |
|
---|
1453 | /**
|
---|
1454 | * Replies the active layer. null, if no active layer is available
|
---|
1455 | *
|
---|
1456 | * @return the active layer. null, if no active layer is available
|
---|
1457 | */
|
---|
1458 | protected Layer getActiveLayer() {
|
---|
1459 | if (!Main.isDisplayingMapView()) return null;
|
---|
1460 | return Main.map.mapView.getActiveLayer();
|
---|
1461 | }
|
---|
1462 |
|
---|
1463 | /* ------------------------------------------------------------------------------ */
|
---|
1464 | /* Interface TableModel */
|
---|
1465 | /* ------------------------------------------------------------------------------ */
|
---|
1466 |
|
---|
1467 | @Override
|
---|
1468 | public int getRowCount() {
|
---|
1469 | List<Layer> layers = getLayers();
|
---|
1470 | if (layers == null) return 0;
|
---|
1471 | return layers.size();
|
---|
1472 | }
|
---|
1473 |
|
---|
1474 | @Override
|
---|
1475 | public int getColumnCount() {
|
---|
1476 | return 3;
|
---|
1477 | }
|
---|
1478 |
|
---|
1479 | @Override
|
---|
1480 | public Object getValueAt(int row, int col) {
|
---|
1481 | List<Layer> layers = getLayers();
|
---|
1482 | if (row >= 0 && row < layers.size()) {
|
---|
1483 | switch (col) {
|
---|
1484 | case 0: return layers.get(row) == getActiveLayer();
|
---|
1485 | case 1: return layers.get(row);
|
---|
1486 | case 2: return layers.get(row);
|
---|
1487 | default: throw new RuntimeException();
|
---|
1488 | }
|
---|
1489 | }
|
---|
1490 | return null;
|
---|
1491 | }
|
---|
1492 |
|
---|
1493 | @Override
|
---|
1494 | public boolean isCellEditable(int row, int col) {
|
---|
1495 | if (col == 0 && getActiveLayer() == getLayers().get(row))
|
---|
1496 | return false;
|
---|
1497 | return true;
|
---|
1498 | }
|
---|
1499 |
|
---|
1500 | @Override
|
---|
1501 | public void setValueAt(Object value, int row, int col) {
|
---|
1502 | List<Layer> layers = getLayers();
|
---|
1503 | if (row < layers.size()) {
|
---|
1504 | Layer l = layers.get(row);
|
---|
1505 | switch (col) {
|
---|
1506 | case 0:
|
---|
1507 | Main.map.mapView.setActiveLayer(l);
|
---|
1508 | l.setVisible(true);
|
---|
1509 | break;
|
---|
1510 | case 1:
|
---|
1511 | l.setVisible((Boolean) value);
|
---|
1512 | break;
|
---|
1513 | case 2:
|
---|
1514 | l.setName((String) value);
|
---|
1515 | break;
|
---|
1516 | default: throw new RuntimeException();
|
---|
1517 | }
|
---|
1518 | fireTableCellUpdated(row, col);
|
---|
1519 | }
|
---|
1520 | }
|
---|
1521 |
|
---|
1522 | /* ------------------------------------------------------------------------------ */
|
---|
1523 | /* Interface LayerChangeListener */
|
---|
1524 | /* ------------------------------------------------------------------------------ */
|
---|
1525 | @Override
|
---|
1526 | public void activeLayerChange(final Layer oldLayer, final Layer newLayer) {
|
---|
1527 | GuiHelper.runInEDTAndWait(new Runnable() {
|
---|
1528 | @Override
|
---|
1529 | public void run() {
|
---|
1530 | if (oldLayer != null) {
|
---|
1531 | int idx = getLayers().indexOf(oldLayer);
|
---|
1532 | if (idx >= 0) {
|
---|
1533 | fireTableRowsUpdated(idx,idx);
|
---|
1534 | }
|
---|
1535 | }
|
---|
1536 |
|
---|
1537 | if (newLayer != null) {
|
---|
1538 | int idx = getLayers().indexOf(newLayer);
|
---|
1539 | if (idx >= 0) {
|
---|
1540 | fireTableRowsUpdated(idx,idx);
|
---|
1541 | }
|
---|
1542 | }
|
---|
1543 | ensureActiveSelected();
|
---|
1544 | }
|
---|
1545 | });
|
---|
1546 | }
|
---|
1547 |
|
---|
1548 | @Override
|
---|
1549 | public void layerAdded(Layer newLayer) {
|
---|
1550 | onAddLayer(newLayer);
|
---|
1551 | }
|
---|
1552 |
|
---|
1553 | @Override
|
---|
1554 | public void layerRemoved(final Layer oldLayer) {
|
---|
1555 | onRemoveLayer(oldLayer);
|
---|
1556 | }
|
---|
1557 |
|
---|
1558 | /* ------------------------------------------------------------------------------ */
|
---|
1559 | /* Interface PropertyChangeListener */
|
---|
1560 | /* ------------------------------------------------------------------------------ */
|
---|
1561 | @Override
|
---|
1562 | public void propertyChange(PropertyChangeEvent evt) {
|
---|
1563 | if (evt.getSource() instanceof Layer) {
|
---|
1564 | Layer layer = (Layer)evt.getSource();
|
---|
1565 | final int idx = getLayers().indexOf(layer);
|
---|
1566 | if (idx < 0) return;
|
---|
1567 | fireRefresh();
|
---|
1568 | }
|
---|
1569 | }
|
---|
1570 | }
|
---|
1571 |
|
---|
1572 | static class LayerList extends JTable {
|
---|
1573 | public LayerList(TableModel dataModel) {
|
---|
1574 | super(dataModel);
|
---|
1575 | }
|
---|
1576 |
|
---|
1577 | public void scrollToVisible(int row, int col) {
|
---|
1578 | if (!(getParent() instanceof JViewport))
|
---|
1579 | return;
|
---|
1580 | JViewport viewport = (JViewport) getParent();
|
---|
1581 | Rectangle rect = getCellRect(row, col, true);
|
---|
1582 | Point pt = viewport.getViewPosition();
|
---|
1583 | rect.setLocation(rect.x - pt.x, rect.y - pt.y);
|
---|
1584 | viewport.scrollRectToVisible(rect);
|
---|
1585 | }
|
---|
1586 | }
|
---|
1587 |
|
---|
1588 | /**
|
---|
1589 | * Creates a {@link ShowHideLayerAction} in the
|
---|
1590 | * context of this {@link LayerListDialog}.
|
---|
1591 | *
|
---|
1592 | * @return the action
|
---|
1593 | */
|
---|
1594 | public ShowHideLayerAction createShowHideLayerAction() {
|
---|
1595 | return new ShowHideLayerAction();
|
---|
1596 | }
|
---|
1597 |
|
---|
1598 | /**
|
---|
1599 | * Creates a {@link DeleteLayerAction} in the
|
---|
1600 | * context of this {@link LayerListDialog}.
|
---|
1601 | *
|
---|
1602 | * @return the action
|
---|
1603 | */
|
---|
1604 | public DeleteLayerAction createDeleteLayerAction() {
|
---|
1605 | return new DeleteLayerAction();
|
---|
1606 | }
|
---|
1607 |
|
---|
1608 | /**
|
---|
1609 | * Creates a {@link ActivateLayerAction} for <code>layer</code> in the
|
---|
1610 | * context of this {@link LayerListDialog}.
|
---|
1611 | *
|
---|
1612 | * @param layer the layer
|
---|
1613 | * @return the action
|
---|
1614 | */
|
---|
1615 | public ActivateLayerAction createActivateLayerAction(Layer layer) {
|
---|
1616 | return new ActivateLayerAction(layer);
|
---|
1617 | }
|
---|
1618 |
|
---|
1619 | /**
|
---|
1620 | * Creates a {@link MergeLayerAction} for <code>layer</code> in the
|
---|
1621 | * context of this {@link LayerListDialog}.
|
---|
1622 | *
|
---|
1623 | * @param layer the layer
|
---|
1624 | * @return the action
|
---|
1625 | */
|
---|
1626 | public MergeAction createMergeLayerAction(Layer layer) {
|
---|
1627 | return new MergeAction(layer);
|
---|
1628 | }
|
---|
1629 |
|
---|
1630 | /**
|
---|
1631 | * Returns the layer at given index, or {@code null}.
|
---|
1632 | * @param index the index
|
---|
1633 | * @return the layer at given index, or {@code null} if index out of range
|
---|
1634 | */
|
---|
1635 | public static Layer getLayerForIndex(int index) {
|
---|
1636 |
|
---|
1637 | if (!Main.isDisplayingMapView())
|
---|
1638 | return null;
|
---|
1639 |
|
---|
1640 | List<Layer> layers = Main.map.mapView.getAllLayersAsList();
|
---|
1641 |
|
---|
1642 | if (index < layers.size() && index >= 0)
|
---|
1643 | return layers.get(index);
|
---|
1644 | else
|
---|
1645 | return null;
|
---|
1646 | }
|
---|
1647 |
|
---|
1648 | /**
|
---|
1649 | * Returns a list of info on all layers of a given class.
|
---|
1650 | * @param layerClass The layer class. This is not {@code Class<? extends Layer>} on purpose,
|
---|
1651 | * to allow asking for layers implementing some interface
|
---|
1652 | * @return list of info on all layers assignable from {@code layerClass}
|
---|
1653 | */
|
---|
1654 | public static List<MultikeyInfo> getLayerInfoByClass(Class<?> layerClass) {
|
---|
1655 |
|
---|
1656 | List<MultikeyInfo> result = new ArrayList<>();
|
---|
1657 |
|
---|
1658 | if (!Main.isDisplayingMapView())
|
---|
1659 | return result;
|
---|
1660 |
|
---|
1661 | List<Layer> layers = Main.map.mapView.getAllLayersAsList();
|
---|
1662 |
|
---|
1663 | int index = 0;
|
---|
1664 | for (Layer l: layers) {
|
---|
1665 | if (layerClass.isAssignableFrom(l.getClass())) {
|
---|
1666 | result.add(new MultikeyInfo(index, l.getName()));
|
---|
1667 | }
|
---|
1668 | index++;
|
---|
1669 | }
|
---|
1670 |
|
---|
1671 | return result;
|
---|
1672 | }
|
---|
1673 |
|
---|
1674 | /**
|
---|
1675 | * Determines if a layer is valid (contained in layer list).
|
---|
1676 | * @param l the layer
|
---|
1677 | * @return {@code true} if layer {@code l} is contained in current layer list
|
---|
1678 | */
|
---|
1679 | public static boolean isLayerValid(Layer l) {
|
---|
1680 |
|
---|
1681 | if (l == null || !Main.isDisplayingMapView())
|
---|
1682 | return false;
|
---|
1683 |
|
---|
1684 | return Main.map.mapView.getAllLayersAsList().contains(l);
|
---|
1685 | }
|
---|
1686 |
|
---|
1687 | /**
|
---|
1688 | * Returns info about layer.
|
---|
1689 | * @param l the layer
|
---|
1690 | * @return info about layer {@code l}
|
---|
1691 | */
|
---|
1692 | public static MultikeyInfo getLayerInfo(Layer l) {
|
---|
1693 |
|
---|
1694 | if (l == null || !Main.isDisplayingMapView())
|
---|
1695 | return null;
|
---|
1696 |
|
---|
1697 | int index = Main.map.mapView.getAllLayersAsList().indexOf(l);
|
---|
1698 | if (index < 0)
|
---|
1699 | return null;
|
---|
1700 |
|
---|
1701 | return new MultikeyInfo(index, l.getName());
|
---|
1702 | }
|
---|
1703 | }
|
---|