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