Changeset 14549 in josm


Ignore:
Timestamp:
2018-12-10T22:37:27+01:00 (4 months ago)
Author:
Don-vip
Message:

see #16073 - support WMS_ENDPOINT

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/actions/AddImageryLayerAction.java

    r14436 r14549  
    1515import java.util.Collections;
    1616import java.util.List;
     17import java.util.function.Function;
    1718import java.util.stream.Collectors;
    1819
     
    165166
    166167    /**
     168     * Represents the user choices when selecting layers to display.
     169     * @since 14549
     170     */
     171    public static class LayerSelection {
     172        private final List<LayerDetails> layers;
     173        private final String format;
     174        private final boolean transparent;
     175
     176        /**
     177         * Constructs a new {@code LayerSelection}.
     178         * @param layers selected layers
     179         * @param format selected image format
     180         * @param transparent enable transparency?
     181         */
     182        public LayerSelection(List<LayerDetails> layers, String format, boolean transparent) {
     183            this.layers = layers;
     184            this.format = format;
     185            this.transparent = transparent;
     186        }
     187    }
     188
     189    private static LayerSelection askToSelectLayers(WMSImagery wms) {
     190        final WMSLayerTree tree = new WMSLayerTree();
     191        tree.updateTree(wms);
     192
     193        Collection<String> wmsFormats = wms.getFormats();
     194        final JComboBox<String> formats = new JComboBox<>(wmsFormats.toArray(new String[0]));
     195        formats.setSelectedItem(wms.getPreferredFormat());
     196        formats.setToolTipText(tr("Select image format for WMS layer"));
     197
     198        if (!GraphicsEnvironment.isHeadless()) {
     199            ExtendedDialog dialog = new ExtendedDialog(MainApplication.getMainFrame(),
     200                    tr("Select WMS layers"), tr("Add layers"), tr("Cancel"));
     201            final JScrollPane scrollPane = new JScrollPane(tree.getLayerTree());
     202            scrollPane.setPreferredSize(new Dimension(400, 400));
     203            final JPanel panel = new JPanel(new GridBagLayout());
     204            panel.add(scrollPane, GBC.eol().fill());
     205            panel.add(formats, GBC.eol().fill(GBC.HORIZONTAL));
     206            dialog.setContent(panel);
     207
     208            if (dialog.showDialog().getValue() != 1) {
     209                return null;
     210            }
     211        }
     212        return new LayerSelection(
     213                tree.getSelectedLayers(),
     214                (String) formats.getSelectedItem(),
     215                true); // TODO: ask the user if transparent layer is wanted
     216    }
     217
     218    /**
    167219     * Asks user to choose a WMS layer from a WMS endpoint.
    168220     * @param info the WMS endpoint.
     
    173225     */
    174226    protected static ImageryInfo getWMSLayerInfo(ImageryInfo info) throws IOException, WMSGetCapabilitiesException {
     227        return getWMSLayerInfo(info, AddImageryLayerAction::askToSelectLayers);
     228    }
     229
     230    /**
     231     * Asks user to choose a WMS layer from a WMS endpoint.
     232     * @param info the WMS endpoint.
     233     * @param choice how the user may choose the WMS layer
     234     * @return chosen WMS layer, or null
     235     * @throws IOException if any I/O error occurs while contacting the WMS endpoint
     236     * @throws WMSGetCapabilitiesException if the WMS getCapabilities request fails
     237     * @throws InvalidPathException if a Path object cannot be constructed for the capabilities cached file
     238     * @since 14549
     239     */
     240    public static ImageryInfo getWMSLayerInfo(ImageryInfo info, Function<WMSImagery, LayerSelection> choice)
     241            throws IOException, WMSGetCapabilitiesException {
    175242        try {
    176243            CheckParameterUtil.ensureThat(ImageryType.WMS_ENDPOINT == info.getImageryType(), "wms_endpoint imagery type expected");
    177244            final WMSImagery wms = new WMSImagery(info.getUrl(), info.getCustomHttpHeaders());
    178 
    179             final WMSLayerTree tree = new WMSLayerTree();
    180             tree.updateTree(wms);
    181 
    182             Collection<String> wmsFormats = wms.getFormats();
    183             final JComboBox<String> formats = new JComboBox<>(wmsFormats.toArray(new String[0]));
    184             formats.setSelectedItem(wms.getPreferredFormat());
    185             formats.setToolTipText(tr("Select image format for WMS layer"));
    186 
    187             if (!GraphicsEnvironment.isHeadless()) {
    188                 ExtendedDialog dialog = new ExtendedDialog(MainApplication.getMainFrame(),
    189                         tr("Select WMS layers"), tr("Add layers"), tr("Cancel"));
    190                 final JScrollPane scrollPane = new JScrollPane(tree.getLayerTree());
    191                 scrollPane.setPreferredSize(new Dimension(400, 400));
    192                 final JPanel panel = new JPanel(new GridBagLayout());
    193                 panel.add(scrollPane, GBC.eol().fill());
    194                 panel.add(formats, GBC.eol().fill(GBC.HORIZONTAL));
    195                 dialog.setContent(panel);
    196 
    197                 if (dialog.showDialog().getValue() != 1) {
    198                     return null;
    199                 }
     245            LayerSelection selection = choice.apply(wms);
     246            if (selection == null) {
     247                return null;
    200248            }
    201249
    202250            final String url = wms.buildGetMapUrl(
    203                     tree.getSelectedLayers().stream().map(LayerDetails::getName).collect(Collectors.toList()),
     251                    selection.layers.stream().map(LayerDetails::getName).collect(Collectors.toList()),
    204252                    (List<String>) null,
    205                     (String) formats.getSelectedItem(),
    206                     true // TODO: ask the user if transparent layer is wanted
     253                    selection.format,
     254                    selection.transparent
    207255                    );
    208256
    209             String selectedLayers = tree.getSelectedLayers().stream()
     257            String selectedLayers = selection.layers.stream()
    210258                    .map(LayerDetails::getName)
    211259                    .collect(Collectors.joining(", "));
     
    215263            ret.setImageryType(ImageryType.WMS);
    216264            ret.setName(info.getName() + selectedLayers);
    217             ret.setServerProjections(wms.getServerProjections(tree.getSelectedLayers()));
     265            ret.setServerProjections(wms.getServerProjections(selection.layers));
    218266            return ret;
    219267        } catch (MalformedURLException ex) {
  • trunk/test/unit/org/openstreetmap/josm/gui/preferences/imagery/ImageryPreferenceTestIT.java

    r14544 r14549  
    3030import org.openstreetmap.gui.jmapviewer.tilesources.TemplatedTMSTileSource;
    3131import org.openstreetmap.josm.TestUtils;
     32import org.openstreetmap.josm.actions.AddImageryLayerAction;
     33import org.openstreetmap.josm.actions.AddImageryLayerAction.LayerSelection;
    3234import org.openstreetmap.josm.data.Bounds;
    3335import org.openstreetmap.josm.data.coor.LatLon;
     
    3739import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryType;
    3840import org.openstreetmap.josm.data.imagery.ImageryLayerInfo;
     41import org.openstreetmap.josm.data.imagery.LayerDetails;
    3942import org.openstreetmap.josm.data.imagery.Shape;
    4043import org.openstreetmap.josm.data.imagery.TMSCachedTileLoaderJob;
    4144import org.openstreetmap.josm.data.imagery.TemplatedWMSTileSource;
    4245import org.openstreetmap.josm.data.imagery.TileJobOptions;
    43 import org.openstreetmap.josm.data.imagery.WMSEndpointTileSource;
    4446import org.openstreetmap.josm.data.imagery.WMTSTileSource;
    4547import org.openstreetmap.josm.data.imagery.WMTSTileSource.WMTSGetCapabilitiesException;
     
    4749import org.openstreetmap.josm.data.projection.ProjectionRegistry;
    4850import org.openstreetmap.josm.data.projection.Projections;
     51import org.openstreetmap.josm.io.imagery.WMSImagery.WMSGetCapabilitiesException;
    4952import org.openstreetmap.josm.testutils.JOSMTestRules;
    5053import org.openstreetmap.josm.tools.HttpClient;
     
    245248                checkTileUrl(info, tileSource, center, Utils.clamp(12, info.getMinZoom() + 1, info.getMaxZoom()));
    246249            }
    247         } catch (IOException | WMTSGetCapabilitiesException | IllegalArgumentException e) {
     250        } catch (IOException | RuntimeException | WMSGetCapabilitiesException | WMTSGetCapabilitiesException e) {
    248251            addError(info, info.getUrl() + " -> " + e.toString());
    249252        }
     
    264267    }
    265268
    266     private static AbstractTileSource getTileSource(ImageryInfo info) throws IOException, WMTSGetCapabilitiesException {
     269    private static AbstractTileSource getTileSource(ImageryInfo info)
     270            throws IOException, WMTSGetCapabilitiesException, WMSGetCapabilitiesException {
    267271        switch (info.getImageryType()) {
    268272            case BING:
     
    272276            case TMS:
    273277                return new TemplatedTMSTileSource(info);
     278            case WMS_ENDPOINT:
     279                info = convertWmsEndpointToWms(info); // fall-through
    274280            case WMS:
    275281                return new TemplatedWMSTileSource(info, getProjection(info));
    276             case WMS_ENDPOINT:
    277                 return new WMSEndpointTileSource(info, getProjection(info));
    278282            case WMTS:
    279283                return new WMTSTileSource(info, getProjection(info));
     
    281285                throw new UnsupportedOperationException(info.toString());
    282286        }
     287    }
     288
     289    private static ImageryInfo convertWmsEndpointToWms(ImageryInfo info) throws IOException, WMSGetCapabilitiesException {
     290        return AddImageryLayerAction.getWMSLayerInfo(
     291                info, wms -> new LayerSelection(firstLeafLayer(wms.getLayers()), wms.getPreferredFormat(), true));
     292    }
     293
     294    private static List<LayerDetails> firstLeafLayer(List<LayerDetails> layers) {
     295        for (LayerDetails layer : layers) {
     296            if (layer.getChildren().isEmpty()) {
     297                return Collections.singletonList(layer);
     298            } else {
     299                return firstLeafLayer(layer.getChildren());
     300            }
     301        }
     302        return Collections.emptyList();
    283303    }
    284304
Note: See TracChangeset for help on using the changeset viewer.