1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.preferences.imagery;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.GridBagLayout;
|
---|
7 | import java.io.IOException;
|
---|
8 | import java.util.Collections;
|
---|
9 |
|
---|
10 | import javax.swing.JButton;
|
---|
11 | import javax.swing.JCheckBox;
|
---|
12 | import javax.swing.JLabel;
|
---|
13 | import javax.swing.JOptionPane;
|
---|
14 | import javax.swing.JPanel;
|
---|
15 |
|
---|
16 | import org.openstreetmap.josm.data.imagery.DefaultLayer;
|
---|
17 | import org.openstreetmap.josm.data.imagery.ImageryInfo;
|
---|
18 | import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryType;
|
---|
19 | import org.openstreetmap.josm.data.imagery.WMTSCapabilities;
|
---|
20 | import org.openstreetmap.josm.data.imagery.WMTSTileSource;
|
---|
21 | import org.openstreetmap.josm.data.imagery.WMTSTileSource.Layer;
|
---|
22 | import org.openstreetmap.josm.data.imagery.WMTSTileSource.WMTSGetCapabilitiesException;
|
---|
23 | import org.openstreetmap.josm.gui.layer.imagery.WMTSLayerSelection;
|
---|
24 | import org.openstreetmap.josm.tools.GBC;
|
---|
25 | import org.openstreetmap.josm.tools.Logging;
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * Panel for adding WMTS imagery sources
|
---|
29 | * @author Wiktor Niesiobędzki
|
---|
30 | * @since 8568
|
---|
31 | */
|
---|
32 | public class AddWMTSLayerPanel extends AddImageryPanel {
|
---|
33 | private final transient JPanel layerPanel = new JPanel(new GridBagLayout());
|
---|
34 | private transient WMTSLayerSelection layerTable;
|
---|
35 | private final JCheckBox setDefaultLayer = new JCheckBox(tr("Set default layer?"));
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * default constructor
|
---|
39 | */
|
---|
40 | public AddWMTSLayerPanel() {
|
---|
41 | add(new JLabel(tr("{0} Make sure OSM has the permission to use this service", "1.")), GBC.eol());
|
---|
42 | add(new JLabel(tr("{0} Enter GetCapabilities URL", "2.")), GBC.eol());
|
---|
43 | add(rawUrl, GBC.eop().fill(GBC.HORIZONTAL));
|
---|
44 | rawUrl.setLineWrap(true);
|
---|
45 | rawUrl.setAlignmentY(TOP_ALIGNMENT);
|
---|
46 | JButton getLayers = new JButton(tr("{0} Get layers", "3."));
|
---|
47 | getLayers.setEnabled(setDefaultLayer.isSelected());
|
---|
48 | setDefaultLayer.addActionListener(e -> {
|
---|
49 | getLayers.setEnabled(setDefaultLayer.isSelected());
|
---|
50 | });
|
---|
51 | add(setDefaultLayer, GBC.eop());
|
---|
52 | add(getLayers, GBC.eop().fill(GBC.HORIZONTAL));
|
---|
53 | add(new JLabel(tr("Choose default layer")), GBC.eol());
|
---|
54 | add(layerPanel, GBC.eol().fill());
|
---|
55 |
|
---|
56 | addCommonSettings();
|
---|
57 |
|
---|
58 | add(new JLabel(tr("{0} Enter name for this layer", "4.")), GBC.eol());
|
---|
59 | add(name, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
60 | registerValidableComponent(rawUrl);
|
---|
61 | registerValidableComponent(setDefaultLayer);
|
---|
62 |
|
---|
63 | getLayers.addActionListener(e -> {
|
---|
64 | try {
|
---|
65 | WMTSCapabilities capabilities = WMTSTileSource.getCapabilities(rawUrl.getText(), getCommonHeaders());
|
---|
66 | layerTable = new WMTSLayerSelection(WMTSTileSource.groupLayersByNameAndTileMatrixSet(capabilities.getLayers()));
|
---|
67 | layerTable.getTable().getSelectionModel().addListSelectionListener(lsl -> {
|
---|
68 | if (layerTable.getSelectedLayer() != null) {
|
---|
69 | name.setText(capabilities.getBaseUrl() + ": " + layerTable.getSelectedLayer().getUserTitle());
|
---|
70 | } else {
|
---|
71 | name.setText(capabilities.getBaseUrl() + ": ");
|
---|
72 | }
|
---|
73 | });
|
---|
74 | layerPanel.removeAll();
|
---|
75 | layerPanel.add(layerTable, GBC.eol().fill());
|
---|
76 | layerPanel.revalidate();
|
---|
77 | } catch (IOException | WMTSGetCapabilitiesException ex) {
|
---|
78 | JOptionPane.showMessageDialog(
|
---|
79 | getParent(),
|
---|
80 | tr("Error getting layers: {0}", ex.getMessage()),
|
---|
81 | tr("WMTS Error"),
|
---|
82 | JOptionPane.ERROR_MESSAGE);
|
---|
83 | }
|
---|
84 | });
|
---|
85 | }
|
---|
86 |
|
---|
87 | @Override
|
---|
88 | protected ImageryInfo getImageryInfo() {
|
---|
89 | ImageryInfo ret = new ImageryInfo(getImageryName(), "wmts:" + sanitize(getImageryRawUrl(), ImageryType.WMTS));
|
---|
90 | if (setDefaultLayer.isSelected()) {
|
---|
91 | if (layerTable == null) {
|
---|
92 | // did not call get capabilities
|
---|
93 | throw new IllegalArgumentException(tr("You need to get fetch layers"));
|
---|
94 | }
|
---|
95 | Layer selectedLayer = layerTable.getSelectedLayer();
|
---|
96 | ret.setDefaultLayers(
|
---|
97 | Collections.<DefaultLayer>singletonList(
|
---|
98 | new DefaultLayer(
|
---|
99 | ImageryType.WMTS,
|
---|
100 | selectedLayer.getIdentifier(),
|
---|
101 | selectedLayer.getStyle(),
|
---|
102 | selectedLayer.getTileMatrixSet().getIdentifier()
|
---|
103 | )
|
---|
104 | )
|
---|
105 | );
|
---|
106 | }
|
---|
107 | ret.setCustomHttpHeaders(getCommonHeaders());
|
---|
108 | ret.setGeoreferenceValid(getCommonIsValidGeoreference());
|
---|
109 | ret.setImageryType(ImageryType.WMTS);
|
---|
110 | try {
|
---|
111 | new WMTSTileSource(ret); // check if constructor throws an error
|
---|
112 | } catch (IOException | WMTSGetCapabilitiesException e) {
|
---|
113 | Logging.warn(e);
|
---|
114 | throw new IllegalArgumentException(e); // if so, wrap exception, so proper message will be shown to the user
|
---|
115 | }
|
---|
116 | return ret;
|
---|
117 | }
|
---|
118 |
|
---|
119 | @Override
|
---|
120 | protected boolean isImageryValid() {
|
---|
121 | return ((setDefaultLayer.isSelected() && layerTable != null && layerTable.getSelectedLayer() != null)
|
---|
122 | || !setDefaultLayer.isSelected()
|
---|
123 | ) && !getImageryName().isEmpty() && !getImageryRawUrl().isEmpty();
|
---|
124 | }
|
---|
125 | }
|
---|