source: josm/trunk/src/org/openstreetmap/josm/gui/dialogs/layer/ShowHideLayerAction.java

Last change on this file was 13130, checked in by Don-vip, 6 years ago

fix #15572 - use ImageProvider attach API for all JOSM actions to ensure proper icon size everywhere

  • Property svn:eol-style set to native
File size: 3.5 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.dialogs.layer;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.Component;
7import java.awt.event.ActionEvent;
8import java.awt.event.KeyEvent;
9import java.lang.ref.WeakReference;
10import java.util.List;
11
12import javax.swing.AbstractAction;
13import javax.swing.JMenuItem;
14
15import org.openstreetmap.josm.gui.dialogs.IEnabledStateUpdating;
16import org.openstreetmap.josm.gui.dialogs.LayerListDialog;
17import org.openstreetmap.josm.gui.dialogs.LayerListDialog.LayerListModel;
18import org.openstreetmap.josm.gui.help.HelpUtil;
19import org.openstreetmap.josm.gui.layer.Layer;
20import org.openstreetmap.josm.gui.layer.Layer.LayerAction;
21import org.openstreetmap.josm.gui.util.MultikeyShortcutAction;
22import org.openstreetmap.josm.tools.ImageProvider;
23import org.openstreetmap.josm.tools.Shortcut;
24
25/**
26 * Action which will toggle the visibility of the currently selected layers.
27 */
28public final class ShowHideLayerAction extends AbstractAction implements IEnabledStateUpdating, LayerAction, MultikeyShortcutAction {
29
30 private transient WeakReference<Layer> lastLayer;
31 private final transient Shortcut multikeyShortcut;
32 private final LayerListModel model;
33
34 /**
35 * Creates a {@link ShowHideLayerAction} which will toggle the visibility of the currently selected layers
36 * @param model layer list model
37 */
38 public ShowHideLayerAction(LayerListModel model) {
39 this.model = model;
40 putValue(NAME, tr("Show/hide"));
41 new ImageProvider("dialogs", "showhide").getResource().attachImageIcon(this, true);
42 putValue(SHORT_DESCRIPTION, tr("Toggle visible state of the selected layer."));
43 putValue("help", HelpUtil.ht("/Dialog/LayerList#ShowHideLayer"));
44 multikeyShortcut = Shortcut.registerShortcut("core_multikey:showHideLayer", tr("Multikey: {0}",
45 tr("Show/hide layer")), KeyEvent.VK_S, Shortcut.SHIFT);
46 multikeyShortcut.setAccelerator(this);
47 updateEnabledState();
48 }
49
50 @Override
51 public Shortcut getMultikeyShortcut() {
52 return multikeyShortcut;
53 }
54
55 @Override
56 public void actionPerformed(ActionEvent e) {
57 for (Layer l : model.getSelectedLayers()) {
58 l.toggleVisible();
59 }
60 }
61
62 @Override
63 public void executeMultikeyAction(int index, boolean repeat) {
64 Layer l = LayerListDialog.getLayerForIndex(index);
65 if (l != null) {
66 l.toggleVisible();
67 lastLayer = new WeakReference<>(l);
68 } else if (repeat && lastLayer != null) {
69 l = lastLayer.get();
70 if (LayerListDialog.isLayerValid(l)) {
71 l.toggleVisible();
72 }
73 }
74 }
75
76 @Override
77 public void updateEnabledState() {
78 setEnabled(!model.getSelectedLayers().isEmpty());
79 }
80
81 @Override
82 public Component createMenuComponent() {
83 return new JMenuItem(this);
84 }
85
86 @Override
87 public boolean supportLayers(List<Layer> layers) {
88 return true;
89 }
90
91 @Override
92 public boolean equals(Object obj) {
93 return obj instanceof ShowHideLayerAction;
94 }
95
96 @Override
97 public int hashCode() {
98 return getClass().hashCode();
99 }
100
101 @Override
102 public List<MultikeyInfo> getMultikeyCombinations() {
103 return LayerListDialog.getLayerInfoByClass(Layer.class);
104 }
105
106 @Override
107 public MultikeyInfo getLastMultikeyAction() {
108 if (lastLayer != null)
109 return LayerListDialog.getLayerInfo(lastLayer.get());
110 return null;
111 }
112}
Note: See TracBrowser for help on using the repository browser.