| 1 | // License: GPL. For details, see LICENSE file.
|
|---|
| 2 | package org.openstreetmap.josm.gui.dialogs.layer;
|
|---|
| 3 |
|
|---|
| 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
|---|
| 5 |
|
|---|
| 6 | import java.awt.Component;
|
|---|
| 7 | import java.awt.event.ActionEvent;
|
|---|
| 8 | import java.awt.event.KeyEvent;
|
|---|
| 9 | import java.lang.ref.WeakReference;
|
|---|
| 10 | import java.util.List;
|
|---|
| 11 |
|
|---|
| 12 | import javax.swing.AbstractAction;
|
|---|
| 13 | import javax.swing.JMenuItem;
|
|---|
| 14 |
|
|---|
| 15 | import org.openstreetmap.josm.gui.dialogs.IEnabledStateUpdating;
|
|---|
| 16 | import org.openstreetmap.josm.gui.dialogs.LayerListDialog;
|
|---|
| 17 | import org.openstreetmap.josm.gui.dialogs.LayerListDialog.LayerListModel;
|
|---|
| 18 | import org.openstreetmap.josm.gui.help.HelpUtil;
|
|---|
| 19 | import org.openstreetmap.josm.gui.layer.Layer;
|
|---|
| 20 | import org.openstreetmap.josm.gui.layer.Layer.LayerAction;
|
|---|
| 21 | import org.openstreetmap.josm.gui.util.MultikeyShortcutAction;
|
|---|
| 22 | import org.openstreetmap.josm.tools.ImageProvider;
|
|---|
| 23 | import org.openstreetmap.josm.tools.Shortcut;
|
|---|
| 24 |
|
|---|
| 25 | /**
|
|---|
| 26 | * Action which will toggle the visibility of the currently selected layers.
|
|---|
| 27 | */
|
|---|
| 28 | public 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 | }
|
|---|