Ticket #18638: 18638.review.patch

File 18638.review.patch, 10.9 KB (added by Don-vip, 3 months ago)

codestyle fixed. Beware to please the Shortcut parser

  • 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.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 CycleLayerAction extends JosmAction {
     28    private static final Shortcut cycleUp =
     29            Shortcut.registerShortcut("core:cyclelayerup", tr("Cycle layers up"), KeyEvent.VK_OPEN_BRACKET, Shortcut.SHIFT);
     30    private static final Shortcut cycleDown =
     31            Shortcut.registerShortcut("core:cyclelayerdown", tr("Cycle layers down"), KeyEvent.VK_CLOSE_BRACKET, Shortcut.SHIFT);
     32
     33    /**
     34     * Create a CycleLayerAction that cycles through layers that are in the model
     35     */
     36    public CycleLayerAction() {
     37        super(tr("Cycle layers"), "dialogs/next", tr("Cycle through layers"), cycleDown, true,
     38                "cycle-layer", false);
     39        MainApplication.registerActionShortcut(this, cycleUp); // cycleDown gets registered by JosmAction
     40        new ImageProvider("dialogs", "next").getResource().attachImageIcon(this, true);
     41        putValue(SHORT_DESCRIPTION, tr("Cycle through visible layers."));
     42        putValue(NAME, tr("Cycle layers"));
     43    }
     44
     45    @Override
     46    public void actionPerformed(ActionEvent e) {
     47        MainLayerManager manager = MainApplication.getLayerManager();
     48        List<Layer> managerLayers = manager.getLayers().stream().filter(layer -> !(layer instanceof ImageryLayer))
     49                .collect(Collectors.toList());
     50        if (managerLayers.isEmpty())
     51            return;
     52        int key = KeyEvent.getExtendedKeyCodeForChar(e.getActionCommand().charAt(0));
     53        char character = KeyEvent.getKeyText(key).charAt(0);
     54        int realKey = KeyEvent.getExtendedKeyCodeForChar(character);
     55        Shortcut shortcut = Shortcut
     56                .findShortcut(realKey, cycleDown.getAssignedModifier())
     57                .orElse(null);
     58
     59        List<Layer> layers;
     60        if (cycleUp.equals(shortcut)) {
     61            int index = managerLayers.indexOf(manager.getActiveLayer());
     62            int sublist = index < managerLayers.size() ? index + 1 : index;
     63            if (index >= managerLayers.size() - 1) {
     64                sublist = 0;
     65            }
     66            layers = managerLayers.subList(sublist, managerLayers.size());
     67        } else {
     68            layers = new ArrayList<>(managerLayers);
     69            Collections.reverse(layers);
     70            int index = layers.indexOf(manager.getActiveLayer());
     71            int sublist = index < managerLayers.size() - 1 ? index + 1 : 0;
     72            layers = layers.subList(sublist, layers.size());
     73        }
     74        manager.setActiveLayer(layers.stream().filter(Layer::isVisible).filter(tlayer -> !(tlayer instanceof ImageryLayer))
     75                .findFirst()
     76                .orElse(manager.getActiveLayer()));
     77    }
     78
     79    @Override
     80    public void destroy() {
     81        super.destroy();
     82        MainApplication.unregisterActionShortcut(this, cycleUp);
     83    }
     84}
  • test/unit/org/openstreetmap/josm/gui/dialogs/layer/CycleLayerActionTest.java

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