Ticket #16809: v1-0003-SlippyMapBBoxChooser-also-gather-TileSources-from.patch

File v1-0003-SlippyMapBBoxChooser-also-gather-TileSources-from.patch, 6.7 KB (added by ris, 6 years ago)
  • src/org/openstreetmap/josm/gui/bbox/SlippyMapBBoxChooser.java

    From 8ee91875f60f8b2180b2356c4c0be86bd0de48e4 Mon Sep 17 00:00:00 2001
    From: Robert Scott <code@humanleg.org.uk>
    Date: Sun, 30 Sep 2018 22:51:22 +0100
    Subject: [PATCH v1 3/4] SlippyMapBBoxChooser: also gather TileSources from
     current imagery layers
    
    this should catch imagery sources that were added through means other than the imagery menu (e.g. remotecontrol)
    ---
     .../josm/gui/bbox/SlippyMapBBoxChooser.java        | 34 ++++++++++++++++++----
     .../josm/gui/dialogs/MinimapDialogTest.java        | 34 ++++++++++++++++++++++
     2 files changed, 63 insertions(+), 5 deletions(-)
    
    diff --git a/src/org/openstreetmap/josm/gui/bbox/SlippyMapBBoxChooser.java b/src/org/openstreetmap/josm/gui/bbox/SlippyMapBBoxChooser.java
    index 4b322854d..821065de6 100644
    a b import org.openstreetmap.josm.data.preferences.BooleanProperty;  
    5454import org.openstreetmap.josm.data.preferences.StringProperty;
    5555import org.openstreetmap.josm.gui.MainApplication;
    5656import org.openstreetmap.josm.gui.layer.AbstractCachedTileSourceLayer;
     57import org.openstreetmap.josm.gui.layer.ImageryLayer;
    5758import org.openstreetmap.josm.gui.layer.MainLayerManager;
    5859import org.openstreetmap.josm.gui.layer.TMSLayer;
    5960import org.openstreetmap.josm.spi.preferences.Config;
    public class SlippyMapBBoxChooser extends JMapViewer implements BBoxChooser, Cha  
    7677        List<TileSource> getTileSources();
    7778    }
    7879
    79     /**
    80      * TMS TileSource provider for the slippymap chooser
    81      */
    82     public static class TMSTileSourceProvider implements TileSourceProvider {
     80    public abstract static class AbstractImageryInfoBasedTileSourceProvider implements TileSourceProvider {
     81        public abstract List<ImageryInfo> getImageryInfos();
    8382
    8483        @Override
    8584        public List<TileSource> getTileSources() {
    8685            if (!TMSLayer.PROP_ADD_TO_SLIPPYMAP_CHOOSER.get()) return Collections.<TileSource>emptyList();
    8786            List<TileSource> sources = new ArrayList<>();
    88             for (ImageryInfo info : ImageryLayerInfo.instance.getLayers()) {
     87            for (ImageryInfo info : this.getImageryInfos()) {
    8988                try {
    9089                    TileSource source = TMSLayer.getTileSourceStatic(info);
    9190                    if (source != null) {
    public class SlippyMapBBoxChooser extends JMapViewer implements BBoxChooser, Cha  
    105104    }
    106105
    107106    /**
     107     * TileSource provider for the slippymap chooser - providing sources from imagery sources menu
     108     */
     109    public static class TMSTileSourceProvider extends AbstractImageryInfoBasedTileSourceProvider {
     110        @Override
     111        public List<ImageryInfo> getImageryInfos() {
     112            return ImageryLayerInfo.instance.getLayers();
     113        }
     114    }
     115
     116    /**
     117     * TileSource provider for the slippymap chooser - providing sources from current layers
     118     */
     119    public static class CurrentLayersTileSourceProvider extends AbstractImageryInfoBasedTileSourceProvider {
     120        @Override
     121        public List<ImageryInfo> getImageryInfos() {
     122            return MainApplication.getLayerManager().getLayers().stream().filter(
     123                layer -> layer instanceof ImageryLayer
     124            ).map(
     125                layer -> ((ImageryLayer) layer).getInfo()
     126            ).collect(Collectors.toList());
     127        }
     128    }
     129
     130    /**
    108131     * Plugins that wish to add custom tile sources to slippy map choose should call this method
    109132     * @param tileSourceProvider new tile source provider
    110133     */
    public class SlippyMapBBoxChooser extends JMapViewer implements BBoxChooser, Cha  
    116139    static {
    117140        addTileSourceProvider(() -> Arrays.<TileSource>asList(new OsmTileSource.Mapnik()));
    118141        addTileSourceProvider(new TMSTileSourceProvider());
     142        addTileSourceProvider(new CurrentLayersTileSourceProvider());
    119143    }
    120144
    121145    private static final StringProperty PROP_MAPSTYLE = new StringProperty("slippy_map_chooser.mapstyle", "Mapnik");
  • test/unit/org/openstreetmap/josm/gui/dialogs/MinimapDialogTest.java

    diff --git a/test/unit/org/openstreetmap/josm/gui/dialogs/MinimapDialogTest.java b/test/unit/org/openstreetmap/josm/gui/dialogs/MinimapDialogTest.java
    index 9d9fa63e3..8d33d3f2a 100644
    a b import org.openstreetmap.josm.TestUtils;  
    3030import org.openstreetmap.josm.data.Bounds;
    3131import org.openstreetmap.josm.data.DataSource;
    3232import org.openstreetmap.josm.data.osm.DataSet;
     33import org.openstreetmap.josm.data.imagery.ImageryInfo;
    3334import org.openstreetmap.josm.data.imagery.ImageryLayerInfo;
    3435import org.openstreetmap.josm.data.projection.ProjectionRegistry;
    3536import org.openstreetmap.josm.data.projection.Projections;
    import org.openstreetmap.josm.gui.MainApplication;  
    3738import org.openstreetmap.josm.gui.MapView;
    3839import org.openstreetmap.josm.gui.bbox.SlippyMapBBoxChooser;
    3940import org.openstreetmap.josm.gui.bbox.SourceButton;
     41import org.openstreetmap.josm.gui.layer.ImageryLayer;
    4042import org.openstreetmap.josm.gui.layer.LayerManagerTest.TestLayer;
    4143import org.openstreetmap.josm.gui.layer.OsmDataLayer;
    4244import org.openstreetmap.josm.gui.util.GuiHelper;
    public class MinimapDialogTest {  
    292294    }
    293295
    294296    /**
     297     * Tests the tile source list includes sources only present in the LayerManager
     298     * @throws Exception if any error occurs
     299     */
     300    @Test
     301    public void testTileSourcesFromCurrentLayers() throws Exception {
     302        // relevant prefs starting out empty, should choose the first (ImageryLayerInfo) source and have shown download area enabled
     303        // (not that there's a data layer for it to use)
     304
     305        // first we will remove "Green Tiles" from ImageryLayerInfo
     306        final ImageryInfo greenTilesInfo = ImageryLayerInfo.instance.getLayers().stream().filter(
     307            i -> i.getName().equals("Green Tiles")
     308        ).findAny().get();
     309        ImageryLayerInfo.instance.remove(greenTilesInfo);
     310
     311        GuiHelper.runInEDT(() -> MainApplication.getLayerManager().addLayer(ImageryLayer.create(greenTilesInfo)));
     312
     313        this.setUpMiniMap();
     314
     315        this.clickSourceMenuItemByLabel("Green Tiles");
     316        this.assertSingleSelectedSourceLabel("Green Tiles");
     317
     318        // call paint to trigger new tile fetch
     319        this.paintSlippyMap();
     320
     321        Awaitility.await().atMost(1000, MILLISECONDS).until(this.slippyMapTasksFinished);
     322
     323        this.paintSlippyMap();
     324
     325        assertEquals(0xff00ff00, paintedSlippyMap.getRGB(0, 0));
     326    }
     327
     328    /**
    295329     * Tests minimap obeys a saved "mapstyle" preference on startup.
    296330     * @throws Exception if any error occurs
    297331     */