source: josm/trunk/src/org/openstreetmap/josm/actions/ReorderImageryLayersAction.java @ 13891

Last change on this file since 13891 was 13891, checked in by Don-vip, 5 months ago

fix #11556 - switch imagery layer pressing tilde (~) or square (²) key

  • Property svn:eol-style set to native
File size: 2.1 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.actions;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.event.ActionEvent;
7import java.awt.event.KeyEvent;
8import java.util.List;
9import java.util.stream.Collectors;
10import java.util.stream.Stream;
11
12import org.openstreetmap.josm.gui.MainApplication;
13import org.openstreetmap.josm.gui.layer.ImageryLayer;
14import org.openstreetmap.josm.tools.Shortcut;
15
16/**
17 * Reorders all non-overlay imagery layers in a cyclic manner.
18 * @since 13891
19 */
20public class ReorderImageryLayersAction extends JosmAction {
21
22    private static final int VK_SQUARE = 0x10000b2;
23
24    /**
25     * Constructs a new {@code ReorderImageryLayersAction}.
26     */
27    public ReorderImageryLayersAction() {
28        // TODO: find a suitable icon
29        super(tr("Reorder imagery layers"), "dialogs/layerlist", tr("Reorders non-overlay imagery layers."),
30            Shortcut.registerShortcut("imagery:reorder", tr("Reorder imagery layers"), KeyEvent.VK_DEAD_TILDE, Shortcut.DIRECT), true);
31        // On AZERTY keyboard layour the key displays the character '²'
32        MainApplication.registerActionShortcut(this,
33            Shortcut.registerShortcut("imagery:reorderbis", tr("Reorder imagery layers"), VK_SQUARE, Shortcut.DIRECT));
34    }
35
36    @Override
37    public void actionPerformed(ActionEvent e) {
38        List<ImageryLayer> layers = getNonOverlayImageryLayers().collect(Collectors.toList());
39        int size = layers.size();
40        if (size > 1) {
41            // Move the first non-overlay layer at the position of the last non-overlay layer
42            getLayerManager().moveLayer(layers.get(0),
43                    getLayerManager().getLayers().indexOf(layers.get(size - 1)));
44        }
45    }
46
47    @Override
48    protected void updateEnabledState() {
49        setEnabled(getNonOverlayImageryLayers().count() > 1);
50    }
51
52    private Stream<ImageryLayer> getNonOverlayImageryLayers() {
53        return getLayerManager().getLayersOfType(ImageryLayer.class).stream()
54                .filter(l -> !l.getInfo().isOverlay());
55    }
56}
Note: See TracBrowser for help on using the repository browser.