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