Ticket #18638: 18638.2.patch

File 18638.2.patch, 10.0 KB (added by taylor.smock, 3 months ago)

Split cycle up/cycle down actions into separate files, fix shortcut issues (single line), and some other pmd issues

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

     
     1// License: GPL. For details, see LICENSE file.
     2package org.openstreetmap.josm.gui.dialogs.layer;
     3
     4import static org.junit.Assert.assertEquals;
     5import static org.openstreetmap.josm.tools.I18n.tr;
     6
     7import org.junit.Before;
     8import org.junit.Rule;
     9import org.junit.Test;
     10import org.openstreetmap.josm.data.imagery.ImageryInfo;
     11import org.openstreetmap.josm.data.imagery.ImageryLayerInfo;
     12import org.openstreetmap.josm.data.osm.DataSet;
     13import org.openstreetmap.josm.gui.MainApplication;
     14import org.openstreetmap.josm.gui.layer.ImageryLayer;
     15import org.openstreetmap.josm.gui.layer.MainLayerManager;
     16import org.openstreetmap.josm.gui.layer.OsmDataLayer;
     17import org.openstreetmap.josm.testutils.JOSMTestRules;
     18
     19import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
     20
     21/**
     22 * Test class for {@link CycleLayerDownAction}
     23 *
     24 * @author Taylor Smock
     25 */
     26public class CycleLayerActionTest {
     27    /** Layers need a projection */
     28    @Rule
     29    @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
     30    public JOSMTestRules test = new JOSMTestRules().main().preferences().projection().fakeImagery();
     31
     32    CycleLayerDownAction cycleDown;
     33    CycleLayerUpAction cycleUp;
     34    MainLayerManager manager;
     35
     36    /**
     37     * Set up common items (make layers, etc.)
     38     */
     39    @Before
     40    public void setUp() {
     41        cycleDown = new CycleLayerDownAction();
     42        cycleUp = new CycleLayerUpAction();
     43        manager = MainApplication.getLayerManager();
     44        for (int i = 0; i < 10; i++) {
     45            manager.addLayer(new OsmDataLayer(new DataSet(), tr("Layer {0}", i), null));
     46        }
     47    }
     48
     49    /**
     50     * Test going down from the bottom
     51     */
     52    @Test
     53    public void testDownBottom() {
     54        manager.setActiveLayer(manager.getLayers().get(0));
     55        cycleDown.actionPerformed(null);
     56        assertEquals(manager.getLayers().size() - 1, manager.getLayers().indexOf(manager.getActiveLayer()));
     57    }
     58
     59    /**
     60     * Check going up from the top
     61     */
     62    @Test
     63    public void testUpTop() {
     64        manager.setActiveLayer(manager.getLayers().get(manager.getLayers().size() - 1));
     65        cycleUp.actionPerformed(null);
     66        assertEquals(0, manager.getLayers().indexOf(manager.getActiveLayer()));
     67    }
     68
     69    /**
     70     * Check going down
     71     */
     72    @Test
     73    public void testDown() {
     74        manager.setActiveLayer(manager.getLayers().get(3));
     75        cycleDown.actionPerformed(null);
     76        assertEquals(2, manager.getLayers().indexOf(manager.getActiveLayer()));
     77    }
     78
     79    /**
     80     * Check going up
     81     */
     82    @Test
     83    public void testUp() {
     84        manager.setActiveLayer(manager.getLayers().get(3));
     85        cycleUp.actionPerformed(null);
     86        assertEquals(4, manager.getLayers().indexOf(manager.getActiveLayer()));
     87    }
     88
     89    /**
     90     * Test no layers
     91     */
     92    @Test
     93    public void testNoLayers() {
     94        manager.getLayers().forEach(manager::removeLayer);
     95        cycleUp.actionPerformed(null);
     96        cycleDown.actionPerformed(null);
     97        assertEquals(0, manager.getLayers().size());
     98    }
     99
     100    /**
     101     * Test with an aerial imagery layer
     102     */
     103    @Test
     104    public void testWithAerialImagery() {
     105        final ImageryInfo magentaTilesInfo = ImageryLayerInfo.instance.getLayers().stream()
     106                .filter(i -> i.getName().equals("Magenta Tiles")).findAny().get();
     107        ImageryLayer imageryLayer = ImageryLayer.create(magentaTilesInfo);
     108        manager.addLayer(imageryLayer);
     109        manager.moveLayer(imageryLayer, 5);
     110        manager.setActiveLayer(manager.getLayers().get(4));
     111        cycleUp.actionPerformed(null);
     112        assertEquals(6, manager.getLayers().indexOf(manager.getActiveLayer()));
     113        cycleDown.actionPerformed(null);
     114        assertEquals(4, manager.getLayers().indexOf(manager.getActiveLayer()));
     115    }
     116}