Ignore:
Timestamp:
2016-11-04T09:58:50+01:00 (7 years ago)
Author:
wiktorn
Message:

Move WMTS layer selection out of WMTSLayer constructor and hookUpMapView.

When user declines to choose any WMTSLayer we should just not add the layer at all instead of showing a warning & empty layer content.

This change alignes behaviour of layer between WMTS and WMS_ENDPOINT imagery sources.

This change includes some parts of Work In Progress to unify WMS and WMS_ENDPOINT sources.

Closes: #13868

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/imagery/WMTSTileSource.java

    r11189 r11216  
    1616import java.util.List;
    1717import java.util.Map;
     18import java.util.Map.Entry;
    1819import java.util.Set;
    1920import java.util.SortedSet;
     
    2324import java.util.regex.Matcher;
    2425import java.util.regex.Pattern;
     26import java.util.stream.Collectors;
    2527
    2628import javax.swing.JPanel;
     
    159161
    160162    private static final class SelectLayerDialog extends ExtendedDialog {
    161         private final transient Layer[] layers;
     163        private final transient List<Entry<String,List<Layer>>> layers;
    162164        private final JTable list;
    163165
    164166        SelectLayerDialog(Collection<Layer> layers) {
    165167            super(Main.parent, tr("Select WMTS layer"), new String[]{tr("Add layers"), tr("Cancel")});
    166             this.layers = layers.toArray(new Layer[layers.size()]);
     168            this.layers = groupLayersByName(layers);
    167169            //getLayersTable(layers, Main.getProjection())
    168170            this.list = new JTable(
     
    172174                            switch (columnIndex) {
    173175                            case 0:
    174                                 return SelectLayerDialog.this.layers[rowIndex].name;
     176                                return SelectLayerDialog.this.layers.get(rowIndex).getKey();
    175177                            case 1:
    176                                 return SelectLayerDialog.this.layers[rowIndex].tileMatrixSet.crs;
     178                                return SelectLayerDialog.this.layers.get(rowIndex).getValue()
     179                                        .stream()
     180                                        .map(x -> x.tileMatrixSet.crs)
     181                                        .collect(Collectors.joining(", "));
    177182                            case 2:
    178                                 return SelectLayerDialog.this.layers[rowIndex].tileMatrixSet.identifier;
     183                                return SelectLayerDialog.this.layers.get(rowIndex).getValue()
     184                                        .stream()
     185                                        .map(x -> x.tileMatrixSet.identifier)
     186                                        .collect(Collectors.joining(", "));
    179187                            default:
    180188                                throw new IllegalArgumentException();
     
    184192                        @Override
    185193                        public int getRowCount() {
    186                             return SelectLayerDialog.this.layers.length;
     194                            return SelectLayerDialog.this.layers.size();
    187195                        }
    188196
     
    216224        }
    217225
    218         public Layer getSelectedLayer() {
     226        private List<Entry<String, List<Layer>>> groupLayersByName(Collection<Layer> layers) {
     227            Map<String, List<Layer>> layerByName = layers.stream().collect(Collectors.groupingBy(x -> x.name));
     228            return layerByName.entrySet().stream().sorted(Map.Entry.comparingByKey()).collect(Collectors.toList());
     229        }
     230
     231        public String getSelectedLayer() {
    219232            int index = list.getSelectedRow();
    220233            if (index < 0) {
    221234                return null; //nothing selected
    222235            }
    223             return layers[index];
     236            return layers.get(index).getKey();
    224237        }
    225238    }
     
    234247    private ScaleList nativeScaleList;
    235248
     249    private String defaultLayer;
     250
    236251    /**
    237252     * Creates a tile source based on imagery info
     
    242257    public WMTSTileSource(ImageryInfo info) throws IOException {
    243258        super(info);
     259        CheckParameterUtil.ensureThat(info.getDefaultLayers().size() < 2, "At most 1 default layer for WMTS is supported");
     260
    244261        this.baseUrl = GetCapabilitiesParseHelper.normalizeCapabilitiesUrl(handleTemplate(info.getUrl()));
    245262        this.layers = getCapabilities();
     263        this.defaultLayer = info.getDefaultLayers().isEmpty() ? null : info.getDefaultLayers().iterator().next();
    246264        if (this.layers.isEmpty())
    247265            throw new IllegalArgumentException(tr("No layers defined by getCapabilities document: {0}", info.getUrl()));
    248266    }
    249267
    250     private static Layer userSelectLayer(Collection<Layer> layers) {
    251         if (layers.size() == 1)
    252             return layers.iterator().next();
    253         Layer ret = null;
     268    /**
     269     * Creates a dialog based on this tile source with all available layers and returns the name of selected layer
     270     * @return Name of selected layer
     271     */
     272    public String userSelectLayer() {
     273        Collection<String> layerNames = layers.stream().map(x -> x.name).collect(Collectors.toSet());
     274
     275        // if there is only one layer name no point in asking
     276        if (layerNames.size() == 1)
     277            return layerNames.iterator().next();
    254278
    255279        final SelectLayerDialog layerSelection = new SelectLayerDialog(layers);
    256280        if (layerSelection.showDialog().getValue() == 1) {
    257             ret = layerSelection.getSelectedLayer();
    258             // TODO: save layer information into ImageryInfo / ImageryPreferences?
    259         }
    260         if (ret == null) {
    261             // user canceled operation or did not choose any layer
    262             throw new IllegalArgumentException(tr("No layer selected"));
    263         }
    264         return ret;
     281            return layerSelection.getSelectedLayer();
     282        }
     283        return null;
    265284    }
    266285
     
    536555        // getLayers will return only layers matching the name, if the user already choose the layer
    537556        // so we will not ask the user again to chose the layer, if he just changes projection
    538         Collection<Layer> candidates = getLayers(currentLayer != null ? currentLayer.name : null, proj.toCode());
    539         if (!candidates.isEmpty()) {
    540             Layer newLayer = userSelectLayer(candidates);
     557        Collection<Layer> candidates = getLayers(currentLayer != null ? currentLayer.name : defaultLayer, proj.toCode());
     558        if (candidates.size() == 1) {
     559
     560            Layer newLayer = candidates.iterator().next();
    541561            if (newLayer != null) {
    542562                this.currentTileMatrixSet = newLayer.tileMatrixSet;
     
    548568                this.nativeScaleList = new ScaleList(scales);
    549569            }
     570        } else if (candidates.size() > 1) {
     571            Main.warn("More than one layer WMTS available: {0} for projection {1} and name {2}. Do not know which to process",
     572                    candidates.stream().map(x -> x.name + ": " + x.tileMatrixSet.identifier).collect(Collectors.joining(", ")),
     573                    proj.toCode(),
     574                    currentLayer != null ? currentLayer.name : defaultLayer
     575                    );
    550576        }
    551577        this.crsScale = getTileSize() * 0.28e-03 / proj.getMetersPerUnit();
Note: See TracChangeset for help on using the changeset viewer.