Changeset 14549 in josm for trunk/src/org


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

see #16073 - support WMS_ENDPOINT

File:
1 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) {
Note: See TracChangeset for help on using the changeset viewer.