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