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.event.ActionListener;
|
---|
7 | import java.io.IOException;
|
---|
8 | import java.net.MalformedURLException;
|
---|
9 | import java.util.Collection;
|
---|
10 | import java.util.List;
|
---|
11 | import java.util.stream.Collectors;
|
---|
12 |
|
---|
13 | import javax.swing.DefaultComboBoxModel;
|
---|
14 | import javax.swing.JButton;
|
---|
15 | import javax.swing.JCheckBox;
|
---|
16 | import javax.swing.JComboBox;
|
---|
17 | import javax.swing.JLabel;
|
---|
18 | import javax.swing.JOptionPane;
|
---|
19 | import javax.swing.JScrollPane;
|
---|
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.gui.bbox.SlippyMapBBoxChooser;
|
---|
25 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
---|
26 | import org.openstreetmap.josm.gui.widgets.JosmTextArea;
|
---|
27 | import org.openstreetmap.josm.io.imagery.WMSImagery;
|
---|
28 | import org.openstreetmap.josm.tools.GBC;
|
---|
29 | import org.openstreetmap.josm.tools.Logging;
|
---|
30 | import org.openstreetmap.josm.tools.Utils;
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * An imagery panel used to add WMS imagery sources.
|
---|
34 | * @since 2599
|
---|
35 | */
|
---|
36 | public class AddWMSLayerPanel extends AddImageryPanel {
|
---|
37 |
|
---|
38 | private transient WMSImagery wms;
|
---|
39 | private final JCheckBox endpoint = new JCheckBox(tr("Store WMS endpoint only, select layers at usage"));
|
---|
40 | private final JCheckBox setDefaultLayers = new JCheckBox(tr("Use selected layers as default"));
|
---|
41 | private final transient WMSLayerTree tree = new WMSLayerTree();
|
---|
42 | private final JComboBox<String> formats = new JComboBox<>();
|
---|
43 | private final JLabel wmsInstruction;
|
---|
44 | private final JosmTextArea wmsUrl = new JosmTextArea(3, 40).transferFocusOnTab();
|
---|
45 | private final JButton showBounds = new JButton(tr("Show bounds"));
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * Constructs a new {@code AddWMSLayerPanel}.
|
---|
49 | */
|
---|
50 | public AddWMSLayerPanel() {
|
---|
51 |
|
---|
52 | add(new JLabel(tr("{0} Make sure OSM has the permission to use this service", "1.")), GBC.eol());
|
---|
53 | add(new JLabel(tr("{0} Enter GetCapabilities URL", "2.")), GBC.eol());
|
---|
54 | add(rawUrl, GBC.eol().fill());
|
---|
55 | rawUrl.setLineWrap(true);
|
---|
56 | JButton getLayers = new JButton(tr("{0} Get layers", "3."));
|
---|
57 | add(getLayers, GBC.eop().fill());
|
---|
58 |
|
---|
59 | add(new JLabel(tr("{0} Select layers", "4.")), GBC.eol());
|
---|
60 |
|
---|
61 | add(endpoint, GBC.eol().fill());
|
---|
62 | setDefaultLayers.setEnabled(false);
|
---|
63 | add(setDefaultLayers, GBC.eol().fill());
|
---|
64 | add(new JScrollPane(tree.getLayerTree()), GBC.eol().fill().weight(1, 100));
|
---|
65 |
|
---|
66 | showBounds.setEnabled(false);
|
---|
67 | add(showBounds, GBC.eop().fill());
|
---|
68 |
|
---|
69 | add(new JLabel(tr("{0} Select image format", "5.")), GBC.eol());
|
---|
70 | add(formats, GBC.eol().fill());
|
---|
71 |
|
---|
72 | addCommonSettings();
|
---|
73 |
|
---|
74 | wmsInstruction = new JLabel(tr("{0} Edit generated {1} URL (optional)", "6.", "WMS"));
|
---|
75 | add(wmsInstruction, GBC.eol());
|
---|
76 | wmsInstruction.setLabelFor(wmsUrl);
|
---|
77 | add(wmsUrl, GBC.eop().fill());
|
---|
78 | wmsUrl.setLineWrap(true);
|
---|
79 |
|
---|
80 | add(new JLabel(tr("{0} Enter name for this layer", "7.")), GBC.eol());
|
---|
81 | add(name, GBC.eop().fill());
|
---|
82 |
|
---|
83 | getLayers.addActionListener(e -> {
|
---|
84 | try {
|
---|
85 | wms = new WMSImagery(rawUrl.getText(), getCommonHeaders());
|
---|
86 | tree.updateTree(wms);
|
---|
87 | Collection<String> wmsFormats = wms.getFormats();
|
---|
88 | formats.setModel(new DefaultComboBoxModel<>(wmsFormats.toArray(new String[0])));
|
---|
89 | formats.setSelectedItem(wms.getPreferredFormat());
|
---|
90 | } catch (MalformedURLException ex1) {
|
---|
91 | Logging.log(Logging.LEVEL_ERROR, ex1);
|
---|
92 | JOptionPane.showMessageDialog(getParent(), tr("Invalid service URL."),
|
---|
93 | tr("WMS Error"), JOptionPane.ERROR_MESSAGE);
|
---|
94 | } catch (IOException ex2) {
|
---|
95 | Logging.log(Logging.LEVEL_ERROR, ex2);
|
---|
96 | JOptionPane.showMessageDialog(getParent(), tr("Could not retrieve WMS layer list."),
|
---|
97 | tr("WMS Error"), JOptionPane.ERROR_MESSAGE);
|
---|
98 | } catch (WMSImagery.WMSGetCapabilitiesException ex3) {
|
---|
99 | String incomingData = ex3.getIncomingData() != null ? ex3.getIncomingData().trim() : "";
|
---|
100 | String title = tr("WMS Error");
|
---|
101 | StringBuilder message = new StringBuilder(tr("Could not parse WMS layer list."));
|
---|
102 | Logging.log(Logging.LEVEL_ERROR, "Could not parse WMS layer list. Incoming data:\n"+incomingData, ex3);
|
---|
103 | if ((incomingData.startsWith("<html>") || incomingData.startsWith("<HTML>"))
|
---|
104 | && (incomingData.endsWith("</html>") || incomingData.endsWith("</HTML>"))) {
|
---|
105 | GuiHelper.notifyUserHtmlError(this, title, message.toString(), incomingData);
|
---|
106 | } else {
|
---|
107 | if (ex3.getMessage() != null) {
|
---|
108 | message.append('\n').append(ex3.getMessage());
|
---|
109 | }
|
---|
110 | JOptionPane.showMessageDialog(getParent(), message.toString(), title, JOptionPane.ERROR_MESSAGE);
|
---|
111 | }
|
---|
112 | }
|
---|
113 | });
|
---|
114 |
|
---|
115 | ActionListener availabilityManagerAction = a -> {
|
---|
116 | setDefaultLayers.setEnabled(endpoint.isSelected());
|
---|
117 | boolean enabled = !endpoint.isSelected() || setDefaultLayers.isSelected();
|
---|
118 | tree.getLayerTree().setEnabled(enabled);
|
---|
119 | showBounds.setEnabled(enabled);
|
---|
120 | wmsInstruction.setEnabled(enabled);
|
---|
121 | formats.setEnabled(enabled);
|
---|
122 | wmsUrl.setEnabled(enabled);
|
---|
123 | if (endpoint.isSelected() && !setDefaultLayers.isSelected() && wms != null) {
|
---|
124 | name.setText(wms.buildRootUrl());
|
---|
125 | }
|
---|
126 | onLayerSelectionChanged();
|
---|
127 | };
|
---|
128 |
|
---|
129 | endpoint.addActionListener(availabilityManagerAction);
|
---|
130 | setDefaultLayers.addActionListener(availabilityManagerAction);
|
---|
131 |
|
---|
132 | tree.getLayerTree().addPropertyChangeListener("selectedLayers", evt -> onLayerSelectionChanged());
|
---|
133 |
|
---|
134 | formats.addActionListener(e -> onLayerSelectionChanged());
|
---|
135 |
|
---|
136 | showBounds.addActionListener(e -> {
|
---|
137 | if (tree.getSelectedLayers().get(0).getBounds() != null) {
|
---|
138 | SlippyMapBBoxChooser mapPanel = new SlippyMapBBoxChooser();
|
---|
139 | mapPanel.setBoundingBox(tree.getSelectedLayers().get(0).getBounds());
|
---|
140 | JOptionPane.showMessageDialog(null, mapPanel, tr("Show Bounds"), JOptionPane.PLAIN_MESSAGE);
|
---|
141 | } else {
|
---|
142 | JOptionPane.showMessageDialog(null, tr("No bounding box was found for this layer."),
|
---|
143 | tr("WMS Error"), JOptionPane.ERROR_MESSAGE);
|
---|
144 | }
|
---|
145 | });
|
---|
146 |
|
---|
147 | registerValidableComponent(endpoint);
|
---|
148 | registerValidableComponent(rawUrl);
|
---|
149 | registerValidableComponent(wmsUrl);
|
---|
150 | }
|
---|
151 |
|
---|
152 | protected final void onLayerSelectionChanged() {
|
---|
153 | if (wms != null && wms.buildRootUrl() != null) {
|
---|
154 | wmsUrl.setText(wms.buildGetMapUrl(
|
---|
155 | tree.getSelectedLayers().stream().map(x -> x.getName()).collect(Collectors.toList()),
|
---|
156 | (List<String>) null,
|
---|
157 | (String) formats.getSelectedItem(),
|
---|
158 | true // TODO: ask user about transparency
|
---|
159 | )
|
---|
160 | );
|
---|
161 | name.setText(wms.buildRootUrl() + ": " + Utils.join(", ", tree.getSelectedLayers()));
|
---|
162 | }
|
---|
163 | showBounds.setEnabled(tree.getSelectedLayers().size() == 1);
|
---|
164 | }
|
---|
165 |
|
---|
166 | @Override
|
---|
167 | public ImageryInfo getImageryInfo() {
|
---|
168 | ImageryInfo info = null;
|
---|
169 | if (endpoint.isSelected()) {
|
---|
170 | info = new ImageryInfo(getImageryName(), getImageryRawUrl());
|
---|
171 | info.setImageryType(ImageryInfo.ImageryType.WMS_ENDPOINT);
|
---|
172 | if (setDefaultLayers.isSelected()) {
|
---|
173 | info.setDefaultLayers(tree.getSelectedLayers().stream()
|
---|
174 | .map(x -> new DefaultLayer(
|
---|
175 | ImageryInfo.ImageryType.WMS_ENDPOINT,
|
---|
176 | x.getName(),
|
---|
177 | "", // TODO: allow selection of styles
|
---|
178 | ""))
|
---|
179 | .collect(Collectors.toList()));
|
---|
180 | info.setServerProjections(wms.getServerProjections(tree.getSelectedLayers()));
|
---|
181 | }
|
---|
182 | } else {
|
---|
183 | info = wms.toImageryInfo(getImageryName(), tree.getSelectedLayers(), (List<String>) null, true); // TODO: ask user about transparency
|
---|
184 | info.setUrl(getWmsUrl());
|
---|
185 | info.setImageryType(ImageryType.WMS);
|
---|
186 | }
|
---|
187 | info.setGeoreferenceValid(getCommonIsValidGeoreference());
|
---|
188 | info.setCustomHttpHeaders(getCommonHeaders());
|
---|
189 | return info;
|
---|
190 | }
|
---|
191 |
|
---|
192 | protected final String getWmsUrl() {
|
---|
193 | return sanitize(wmsUrl.getText(), ImageryInfo.ImageryType.WMS);
|
---|
194 | }
|
---|
195 |
|
---|
196 | @Override
|
---|
197 | protected boolean isImageryValid() {
|
---|
198 | if (getImageryName().isEmpty()) {
|
---|
199 | return false;
|
---|
200 | }
|
---|
201 | if (endpoint.isSelected()) {
|
---|
202 | return !getImageryRawUrl().isEmpty();
|
---|
203 | } else {
|
---|
204 | return !getWmsUrl().isEmpty();
|
---|
205 | }
|
---|
206 | }
|
---|
207 | }
|
---|