| | 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.util.ArrayList; |
| | 10 | import java.util.Collections; |
| | 11 | import java.util.List; |
| | 12 | import java.util.stream.Collectors; |
| | 13 | |
| | 14 | import javax.swing.JMenuItem; |
| | 15 | |
| | 16 | import org.openstreetmap.josm.actions.JosmAction; |
| | 17 | import org.openstreetmap.josm.gui.MainApplication; |
| | 18 | import org.openstreetmap.josm.gui.layer.ImageryLayer; |
| | 19 | import org.openstreetmap.josm.gui.layer.Layer; |
| | 20 | import org.openstreetmap.josm.gui.layer.Layer.LayerAction; |
| | 21 | import org.openstreetmap.josm.gui.layer.MainLayerManager; |
| | 22 | import org.openstreetmap.josm.tools.ImageProvider; |
| | 23 | import org.openstreetmap.josm.tools.Shortcut; |
| | 24 | |
| | 25 | /** |
| | 26 | * Allow users to cycle between adjacent layers easily |
| | 27 | * |
| | 28 | * @author Taylor Smock |
| | 29 | * @since xxx |
| | 30 | */ |
| | 31 | public class CycleLayerAction extends JosmAction implements LayerAction { |
| | 32 | private static Shortcut cycleUp = Shortcut.registerShortcut("core:cyclelayerup", tr("Cycle layers down"), |
| | 33 | KeyEvent.VK_BRACELEFT, Shortcut.SHIFT); |
| | 34 | private static Shortcut cycleDown = Shortcut.registerShortcut("core:cyclelayerdown", tr("Cycle layers up"), |
| | 35 | KeyEvent.VK_BRACERIGHT, Shortcut.SHIFT); |
| | 36 | |
| | 37 | /** |
| | 38 | * Create a CycleLayerAction that cycles through layers that are in the model |
| | 39 | */ |
| | 40 | public CycleLayerAction() { |
| | 41 | super(tr("Cycle layers"), "dialogs/next", tr("Cycle through layers"), cycleDown, true, |
| | 42 | "cycle-layer", false); |
| | 43 | MainApplication.registerActionShortcut(this, cycleUp); // cycleDown gets registered by JosmAction |
| | 44 | new ImageProvider("dialogs", "next").getResource().attachImageIcon(this, true); |
| | 45 | putValue(SHORT_DESCRIPTION, tr("Cycle through visible layers.")); |
| | 46 | putValue(NAME, tr("Cycle layers")); |
| | 47 | } |
| | 48 | |
| | 49 | @Override |
| | 50 | public void actionPerformed(ActionEvent e) { |
| | 51 | MainLayerManager manager = MainApplication.getLayerManager(); |
| | 52 | List<Layer> managerLayers = manager.getLayers().stream().filter(layer -> !(layer instanceof ImageryLayer)) |
| | 53 | .collect(Collectors.toList()); |
| | 54 | if (managerLayers.isEmpty()) |
| | 55 | return; |
| | 56 | int key = KeyEvent.getExtendedKeyCodeForChar(e.getActionCommand().charAt(0)); |
| | 57 | Shortcut shortcut = Shortcut |
| | 58 | .findShortcut(key, cycleDown.getAssignedModifier()) |
| | 59 | .orElse(null); |
| | 60 | |
| | 61 | List<Layer> layers; |
| | 62 | if (cycleDown.equals(shortcut)) { |
| | 63 | int index = managerLayers.indexOf(manager.getActiveLayer()); |
| | 64 | int sublist = index < managerLayers.size() ? index + 1 : index; |
| | 65 | if (index >= managerLayers.size() - 1) { |
| | 66 | index = 0; |
| | 67 | sublist = 0; |
| | 68 | } |
| | 69 | layers = managerLayers.subList(sublist, managerLayers.size()); |
| | 70 | } else { |
| | 71 | layers = new ArrayList<>(managerLayers); |
| | 72 | Collections.reverse(layers); |
| | 73 | int index = layers.indexOf(manager.getActiveLayer()); |
| | 74 | int sublist = index < managerLayers.size() - 1 ? index + 1 : 0; |
| | 75 | layers = layers.subList(sublist, layers.size()); |
| | 76 | } |
| | 77 | Layer layer = layers.stream().filter(Layer::isVisible).filter(tlayer -> !(tlayer instanceof ImageryLayer)) |
| | 78 | .findFirst() |
| | 79 | .orElse(manager.getActiveLayer()); |
| | 80 | |
| | 81 | if (layer != null) |
| | 82 | manager.setActiveLayer(layer); |
| | 83 | } |
| | 84 | |
| | 85 | @Override |
| | 86 | public boolean supportLayers(List<Layer> layers) { |
| | 87 | return true; |
| | 88 | } |
| | 89 | |
| | 90 | @Override |
| | 91 | public Component createMenuComponent() { |
| | 92 | return new JMenuItem(this); |
| | 93 | } |
| | 94 | |
| | 95 | @Override |
| | 96 | public void destroy() { |
| | 97 | super.destroy(); |
| | 98 | MainApplication.unregisterActionShortcut(this, cycleUp); |
| | 99 | } |
| | 100 | } |