Ticket #18638: 18638.patch

File 18638.patch, 6.0 KB (added by taylor.smock, 4 months ago)

Initial patch (no tests)

  • src/org/openstreetmap/josm/gui/dialogs/LayerListDialog.java

     
    5454import org.openstreetmap.josm.gui.MapView;
    5555import org.openstreetmap.josm.gui.SideButton;
    5656import org.openstreetmap.josm.gui.dialogs.layer.ActivateLayerAction;
     57import org.openstreetmap.josm.gui.dialogs.layer.CycleLayerAction;
    5758import org.openstreetmap.josm.gui.dialogs.layer.DeleteLayerAction;
    5859import org.openstreetmap.josm.gui.dialogs.layer.DuplicateAction;
    5960import org.openstreetmap.josm.gui.dialogs.layer.LayerListTransferHandler;
     
    141142    private final ActivateLayerAction activateLayerAction;
    142143    private final ShowHideLayerAction showHideLayerAction;
    143144
     145    private final CycleLayerAction cycleLayerAction;
     146
    144147    //TODO This duplicates ShowHide actions functionality
    145148    /** stores which layer index to toggle and executes the ShowHide action if the layer is present */
    146149    private final class ToggleLayerIndexVisibility extends AbstractAction {
     
    329332        // Show/Activate layer on Enter key press
    330333        InputMapUtils.addSpacebarAction(layerList, showHideLayerAction);
    331334
     335        // Cycle layer action
     336        cycleLayerAction = new CycleLayerAction();
     337
    332338        createLayout(layerList, true, Arrays.asList(
    333339                new SideButton(moveUpAction, false),
    334340                new SideButton(moveDownAction, false),
     
    388394        DISPLAY_NUMBERS.removeListener(visibilityWidthListener);
    389395        ExpertToggleAction.removeExpertModeChangeListener(visibilityWidthListener);
    390396        layerManager.removeLayerChangeListener(visibilityWidthListener);
     397        cycleLayerAction.destroy();
    391398        super.destroy();
    392399        instance = null;
    393400    }
  • src/org/openstreetmap/josm/gui/dialogs/layer/CycleLayerAction.java

     
     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.util.ArrayList;
     10import java.util.Collections;
     11import java.util.List;
     12import java.util.stream.Collectors;
     13
     14import javax.swing.JMenuItem;
     15
     16import org.openstreetmap.josm.actions.JosmAction;
     17import org.openstreetmap.josm.gui.MainApplication;
     18import org.openstreetmap.josm.gui.layer.ImageryLayer;
     19import org.openstreetmap.josm.gui.layer.Layer;
     20import org.openstreetmap.josm.gui.layer.Layer.LayerAction;
     21import org.openstreetmap.josm.gui.layer.MainLayerManager;
     22import org.openstreetmap.josm.tools.ImageProvider;
     23import 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 */
     31public 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}