| 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.event.ActionEvent;
|
|---|
| 7 | import java.awt.event.KeyEvent;
|
|---|
| 8 | import java.util.List;
|
|---|
| 9 | import java.util.stream.Collectors;
|
|---|
| 10 |
|
|---|
| 11 | import org.openstreetmap.josm.actions.JosmAction;
|
|---|
| 12 | import org.openstreetmap.josm.gui.MainApplication;
|
|---|
| 13 | import org.openstreetmap.josm.gui.layer.ImageryLayer;
|
|---|
| 14 | import org.openstreetmap.josm.gui.layer.Layer;
|
|---|
| 15 | import org.openstreetmap.josm.gui.layer.MainLayerManager;
|
|---|
| 16 | import org.openstreetmap.josm.tools.ImageProvider;
|
|---|
| 17 | import org.openstreetmap.josm.tools.Shortcut;
|
|---|
| 18 |
|
|---|
| 19 | /**
|
|---|
| 20 | * Allow users to cycle between adjacent layers easily
|
|---|
| 21 | *
|
|---|
| 22 | * @author Taylor Smock
|
|---|
| 23 | * @since 15923
|
|---|
| 24 | */
|
|---|
| 25 | public class CycleLayerUpAction extends JosmAction {
|
|---|
| 26 | private static final long serialVersionUID = 1L;
|
|---|
| 27 | private static final Shortcut cycleUp =
|
|---|
| 28 | Shortcut.registerShortcut("core:cyclelayerup", tr("Cycle layer up"), KeyEvent.VK_OPEN_BRACKET, Shortcut.SHIFT);
|
|---|
| 29 |
|
|---|
| 30 | /**
|
|---|
| 31 | * Create a CycleLayerDownAction that cycles through layers that are in the model
|
|---|
| 32 | */
|
|---|
| 33 | public CycleLayerUpAction() {
|
|---|
| 34 | super(tr("Cycle layer up"), "dialogs/next", tr("Cycle through data layers in an upward direction"),
|
|---|
| 35 | cycleUp, true, "cycle-layer-up", false);
|
|---|
| 36 | new ImageProvider("dialogs", "next").getResource().attachImageIcon(this, true);
|
|---|
| 37 | putValue(SHORT_DESCRIPTION, tr("Cycle through visible layers."));
|
|---|
| 38 | putValue(NAME, tr("Cycle layers"));
|
|---|
| 39 | }
|
|---|
| 40 |
|
|---|
| 41 | @Override
|
|---|
| 42 | public void actionPerformed(ActionEvent e) {
|
|---|
| 43 | MainLayerManager manager = MainApplication.getLayerManager();
|
|---|
| 44 | List<Layer> managerLayers = manager.getLayers().stream().filter(layer -> !(layer instanceof ImageryLayer))
|
|---|
| 45 | .collect(Collectors.toList());
|
|---|
| 46 | if (managerLayers.isEmpty()) {
|
|---|
| 47 | return;
|
|---|
| 48 | }
|
|---|
| 49 | int index = managerLayers.indexOf(manager.getActiveLayer());
|
|---|
| 50 | int sublist = index < managerLayers.size() ? index + 1 : index;
|
|---|
| 51 | if (index >= managerLayers.size() - 1) {
|
|---|
| 52 | sublist = 0;
|
|---|
| 53 | }
|
|---|
| 54 | List<Layer> layers = managerLayers.subList(sublist, managerLayers.size());
|
|---|
| 55 | manager.setActiveLayer(layers.stream().filter(Layer::isVisible).filter(tlayer -> !(tlayer instanceof ImageryLayer))
|
|---|
| 56 | .findFirst().orElse(manager.getActiveLayer()));
|
|---|
| 57 | }
|
|---|
| 58 | }
|
|---|