1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.preferences;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.Component;
|
---|
7 | import java.awt.Cursor;
|
---|
8 | import java.awt.GridBagConstraints;
|
---|
9 | import java.awt.GridBagLayout;
|
---|
10 | import java.awt.HeadlessException;
|
---|
11 | import java.awt.event.ActionEvent;
|
---|
12 | import java.awt.event.ActionListener;
|
---|
13 | import java.awt.event.KeyAdapter;
|
---|
14 | import java.awt.event.KeyEvent;
|
---|
15 | import java.io.BufferedReader;
|
---|
16 | import java.io.IOException;
|
---|
17 | import java.io.InputStream;
|
---|
18 | import java.io.InputStreamReader;
|
---|
19 | import java.io.StringReader;
|
---|
20 | import java.net.MalformedURLException;
|
---|
21 | import java.net.URL;
|
---|
22 | import java.net.URLConnection;
|
---|
23 | import java.util.HashSet;
|
---|
24 | import java.util.Iterator;
|
---|
25 | import java.util.LinkedList;
|
---|
26 | import java.util.List;
|
---|
27 | import java.util.Set;
|
---|
28 |
|
---|
29 | import javax.swing.JButton;
|
---|
30 | import javax.swing.JFrame;
|
---|
31 | import javax.swing.JLabel;
|
---|
32 | import javax.swing.JOptionPane;
|
---|
33 | import javax.swing.JPanel;
|
---|
34 | import javax.swing.JScrollPane;
|
---|
35 | import javax.swing.JTabbedPane;
|
---|
36 | import javax.swing.JTextArea;
|
---|
37 | import javax.swing.JTextField;
|
---|
38 | import javax.swing.JTree;
|
---|
39 | import javax.swing.event.ChangeEvent;
|
---|
40 | import javax.swing.event.ChangeListener;
|
---|
41 | import javax.swing.event.TreeSelectionEvent;
|
---|
42 | import javax.swing.event.TreeSelectionListener;
|
---|
43 | import javax.swing.tree.DefaultMutableTreeNode;
|
---|
44 | import javax.swing.tree.DefaultTreeCellRenderer;
|
---|
45 | import javax.swing.tree.DefaultTreeModel;
|
---|
46 | import javax.swing.tree.MutableTreeNode;
|
---|
47 | import javax.swing.tree.TreePath;
|
---|
48 | import javax.xml.parsers.DocumentBuilder;
|
---|
49 | import javax.xml.parsers.DocumentBuilderFactory;
|
---|
50 | import javax.xml.parsers.ParserConfigurationException;
|
---|
51 |
|
---|
52 | import org.openstreetmap.josm.data.Bounds;
|
---|
53 | import org.openstreetmap.josm.data.projection.Projection;
|
---|
54 | import org.openstreetmap.josm.data.projection.ProjectionSubPrefs;
|
---|
55 | import org.openstreetmap.josm.data.projection.Projections;
|
---|
56 | import org.openstreetmap.josm.gui.bbox.SlippyMapBBoxChooser;
|
---|
57 | import org.openstreetmap.josm.tools.GBC;
|
---|
58 | import org.w3c.dom.Document;
|
---|
59 | import org.w3c.dom.Element;
|
---|
60 | import org.w3c.dom.Node;
|
---|
61 | import org.w3c.dom.NodeList;
|
---|
62 | import org.xml.sax.EntityResolver;
|
---|
63 | import org.xml.sax.InputSource;
|
---|
64 | import org.xml.sax.SAXException;
|
---|
65 |
|
---|
66 |
|
---|
67 | public class AddWMSLayerPanel extends JPanel {
|
---|
68 | private List<LayerDetails> selectedLayers;
|
---|
69 | private URL serviceUrl;
|
---|
70 | private LayerDetails selectedLayer;
|
---|
71 |
|
---|
72 | private JTextField menuName;
|
---|
73 | private JTextArea resultingLayerField;
|
---|
74 | private MutableTreeNode treeRootNode;
|
---|
75 | private DefaultTreeModel treeData;
|
---|
76 | private JTree layerTree;
|
---|
77 | private JButton showBoundsButton;
|
---|
78 |
|
---|
79 | private boolean previouslyShownUnsupportedCrsError = false;
|
---|
80 | private JTextArea tmsURL;
|
---|
81 |
|
---|
82 | public AddWMSLayerPanel() {
|
---|
83 | JPanel imageryAddPanel = new JPanel(new GridBagLayout());
|
---|
84 | imageryAddPanel.add(new JLabel(tr("Menu Name")), GBC.std().insets(0,0,5,0));
|
---|
85 | menuName = new JTextField(40);
|
---|
86 | menuName.setText(tr("Unnamed Imagery Layer"));
|
---|
87 | imageryAddPanel.add(menuName, GBC.eop().insets(5,0,0,0).fill(GridBagConstraints.HORIZONTAL));
|
---|
88 |
|
---|
89 | final JTabbedPane tabbedPane = new JTabbedPane();
|
---|
90 |
|
---|
91 | final JPanel wmsFetchPanel = new JPanel(new GridBagLayout());
|
---|
92 | tabbedPane.addTab(tr("WMS"), wmsFetchPanel);
|
---|
93 | imageryAddPanel.add(tabbedPane, GBC.eop().insets(5,0,0,0).fill(GridBagConstraints.HORIZONTAL));
|
---|
94 |
|
---|
95 | final JTextArea serviceUrlText = new JTextArea(3, 40);
|
---|
96 | serviceUrlText.setLineWrap(true);
|
---|
97 | serviceUrlText.setText("http://sample.com/wms?");
|
---|
98 | wmsFetchPanel.add(new JLabel(tr("Service URL")), GBC.std().insets(0,0,5,0));
|
---|
99 | JScrollPane scrollPane = new JScrollPane(serviceUrlText,
|
---|
100 | JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
|
---|
101 | JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
|
---|
102 | wmsFetchPanel.add(scrollPane, GBC.eop().insets(5,0,0,0));
|
---|
103 | JButton getLayersButton = new JButton(tr("Get Layers"));
|
---|
104 | getLayersButton.addActionListener(new ActionListener() {
|
---|
105 | @Override
|
---|
106 | public void actionPerformed(ActionEvent e) {
|
---|
107 | Cursor beforeCursor = getCursor();
|
---|
108 | try {
|
---|
109 | setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
|
---|
110 | attemptGetCapabilities(serviceUrlText.getText());
|
---|
111 | } finally {
|
---|
112 | setCursor(beforeCursor);
|
---|
113 | }
|
---|
114 | }
|
---|
115 | });
|
---|
116 | wmsFetchPanel.add(getLayersButton, GBC.eop().anchor(GridBagConstraints.EAST));
|
---|
117 |
|
---|
118 | treeRootNode = new DefaultMutableTreeNode();
|
---|
119 | treeData = new DefaultTreeModel(treeRootNode);
|
---|
120 | layerTree = new JTree(treeData);
|
---|
121 | layerTree.setCellRenderer(new LayerTreeCellRenderer());
|
---|
122 | layerTree.addTreeSelectionListener(new TreeSelectionListener() {
|
---|
123 |
|
---|
124 | @Override
|
---|
125 | public void valueChanged(TreeSelectionEvent e) {
|
---|
126 | TreePath[] selectionRows = layerTree.getSelectionPaths();
|
---|
127 | if(selectionRows == null) {
|
---|
128 | showBoundsButton.setEnabled(false);
|
---|
129 | selectedLayer = null;
|
---|
130 | return;
|
---|
131 | }
|
---|
132 |
|
---|
133 | selectedLayers = new LinkedList<LayerDetails>();
|
---|
134 | for (TreePath i : selectionRows) {
|
---|
135 | Object userObject = ((DefaultMutableTreeNode) i.getLastPathComponent()).getUserObject();
|
---|
136 | if(userObject instanceof LayerDetails) {
|
---|
137 | LayerDetails detail = (LayerDetails) userObject;
|
---|
138 | if(!detail.isSupported()) {
|
---|
139 | layerTree.removeSelectionPath(i);
|
---|
140 | if(!previouslyShownUnsupportedCrsError) {
|
---|
141 | JOptionPane.showMessageDialog(null, tr("That layer does not support any of JOSM's projections,\n" +
|
---|
142 | "so you can not use it. This message will not show again."),
|
---|
143 | tr("WMS Error"), JOptionPane.ERROR_MESSAGE);
|
---|
144 | previouslyShownUnsupportedCrsError = true;
|
---|
145 | }
|
---|
146 | } else if(detail.ident != null) {
|
---|
147 | selectedLayers.add(detail);
|
---|
148 | }
|
---|
149 | }
|
---|
150 | }
|
---|
151 |
|
---|
152 | if (!selectedLayers.isEmpty()) {
|
---|
153 | resultingLayerField.setText(buildGetMapUrl());
|
---|
154 |
|
---|
155 | if(selectedLayers.size() == 1) {
|
---|
156 | showBoundsButton.setEnabled(true);
|
---|
157 | selectedLayer = selectedLayers.get(0);
|
---|
158 | }
|
---|
159 | } else {
|
---|
160 | showBoundsButton.setEnabled(false);
|
---|
161 | selectedLayer = null;
|
---|
162 | }
|
---|
163 | }
|
---|
164 | });
|
---|
165 | wmsFetchPanel.add(new JScrollPane(layerTree), GBC.eop().insets(5,0,0,0).fill(GridBagConstraints.HORIZONTAL));
|
---|
166 |
|
---|
167 | JPanel layerManipulationButtons = new JPanel();
|
---|
168 | showBoundsButton = new JButton(tr("Show Bounds"));
|
---|
169 | showBoundsButton.setEnabled(false);
|
---|
170 | showBoundsButton.addActionListener(new ActionListener() {
|
---|
171 | @Override
|
---|
172 | public void actionPerformed(ActionEvent e) {
|
---|
173 | if(selectedLayer.bounds != null) {
|
---|
174 | SlippyMapBBoxChooser mapPanel = new SlippyMapBBoxChooser();
|
---|
175 | mapPanel.setBoundingBox(selectedLayer.bounds);
|
---|
176 | JOptionPane.showMessageDialog(null, mapPanel, tr("Show Bounds"), JOptionPane.PLAIN_MESSAGE);
|
---|
177 | } else {
|
---|
178 | JOptionPane.showMessageDialog(null, tr("No bounding box was found for this layer."),
|
---|
179 | tr("WMS Error"), JOptionPane.ERROR_MESSAGE);
|
---|
180 | }
|
---|
181 | }
|
---|
182 | });
|
---|
183 | layerManipulationButtons.add(showBoundsButton);
|
---|
184 |
|
---|
185 | wmsFetchPanel.add(layerManipulationButtons, GBC.eol().insets(0,0,5,0));
|
---|
186 |
|
---|
187 | final JPanel tmsView = new JPanel(new GridBagLayout());
|
---|
188 | tmsView.add(new JLabel(tr("TMS URL")), GBC.std().insets(0,0,5,0));
|
---|
189 | tmsURL = new JTextArea(3, 40);
|
---|
190 | tmsURL.setLineWrap(true);
|
---|
191 | tmsURL.setText("http://sample.com/tms/{zoom}/{x}/{y}.jpg");
|
---|
192 | tmsURL.addKeyListener(new KeyAdapter() {
|
---|
193 | @Override
|
---|
194 | public void keyReleased(KeyEvent e) {
|
---|
195 | resultingLayerField.setText(buildTMSUrl());
|
---|
196 | }
|
---|
197 | });
|
---|
198 | JScrollPane tmsUrlScrollPane = new JScrollPane(tmsURL,
|
---|
199 | JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
|
---|
200 | JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
|
---|
201 | tmsView.add(tmsUrlScrollPane, GBC.eop().insets(5,0,0,0).fill(GridBagConstraints.HORIZONTAL));
|
---|
202 | tabbedPane.addTab(tr("TMS"), tmsView);
|
---|
203 |
|
---|
204 | imageryAddPanel.add(new JLabel(tr("Imagery URL")), GBC.std().insets(0,0,5,0));
|
---|
205 | resultingLayerField = new JTextArea(3, 40);
|
---|
206 | resultingLayerField.setLineWrap(true);
|
---|
207 | imageryAddPanel.add(new JScrollPane(resultingLayerField, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER), GBC.eop().insets(5,0,0,0).fill(GridBagConstraints.HORIZONTAL));
|
---|
208 |
|
---|
209 | tabbedPane.addChangeListener(new ChangeListener() {
|
---|
210 | @Override
|
---|
211 | public void stateChanged(ChangeEvent e) {
|
---|
212 | Component sel = tabbedPane.getSelectedComponent();
|
---|
213 | if(tmsView == sel) {
|
---|
214 | resultingLayerField.setText(buildTMSUrl());
|
---|
215 | } else if(wmsFetchPanel == sel) {
|
---|
216 | if(serviceUrl != null) {
|
---|
217 | resultingLayerField.setText(buildGetMapUrl());
|
---|
218 | }
|
---|
219 | }
|
---|
220 | }
|
---|
221 | });
|
---|
222 |
|
---|
223 | add(imageryAddPanel);
|
---|
224 | }
|
---|
225 |
|
---|
226 | private String buildTMSUrl() {
|
---|
227 | StringBuilder a = new StringBuilder("tms:");
|
---|
228 | a.append(tmsURL.getText());
|
---|
229 | return a.toString();
|
---|
230 | }
|
---|
231 |
|
---|
232 | private String buildRootUrl() {
|
---|
233 | StringBuilder a = new StringBuilder(serviceUrl.getProtocol());
|
---|
234 | a.append("://");
|
---|
235 | a.append(serviceUrl.getHost());
|
---|
236 | if(serviceUrl.getPort() != -1) {
|
---|
237 | a.append(":");
|
---|
238 | a.append(serviceUrl.getPort());
|
---|
239 | }
|
---|
240 | a.append(serviceUrl.getPath());
|
---|
241 | a.append("?");
|
---|
242 | if(serviceUrl.getQuery() != null) {
|
---|
243 | a.append(serviceUrl.getQuery());
|
---|
244 | if (!serviceUrl.getQuery().isEmpty() && !serviceUrl.getQuery().endsWith("&")) {
|
---|
245 | a.append("&");
|
---|
246 | }
|
---|
247 | }
|
---|
248 | return a.toString();
|
---|
249 | }
|
---|
250 |
|
---|
251 | private String buildGetMapUrl() {
|
---|
252 | StringBuilder a = new StringBuilder();
|
---|
253 | a.append(buildRootUrl());
|
---|
254 | a.append("FORMAT=image/jpeg&VERSION=1.1.1&SERVICE=WMS&REQUEST=GetMap&Layers=");
|
---|
255 | a.append(commaSepLayerList());
|
---|
256 | a.append("&");
|
---|
257 |
|
---|
258 | return a.toString();
|
---|
259 | }
|
---|
260 |
|
---|
261 | private String commaSepLayerList() {
|
---|
262 | StringBuilder b = new StringBuilder();
|
---|
263 |
|
---|
264 | Iterator<LayerDetails> iterator = selectedLayers.iterator();
|
---|
265 | while (iterator.hasNext()) {
|
---|
266 | LayerDetails layerDetails = iterator.next();
|
---|
267 | b.append(layerDetails.ident);
|
---|
268 | if(iterator.hasNext()) {
|
---|
269 | b.append(",");
|
---|
270 | }
|
---|
271 | }
|
---|
272 |
|
---|
273 | return b.toString();
|
---|
274 | }
|
---|
275 |
|
---|
276 | private void showError(String incomingData, Exception e) {
|
---|
277 | JOptionPane.showMessageDialog(this, tr("Could not parse WMS layer list."),
|
---|
278 | tr("WMS Error"), JOptionPane.ERROR_MESSAGE);
|
---|
279 | System.err.println("Could not parse WMS layer list. Incoming data:");
|
---|
280 | System.err.println(incomingData);
|
---|
281 | e.printStackTrace();
|
---|
282 | }
|
---|
283 |
|
---|
284 | private void attemptGetCapabilities(String serviceUrlStr) {
|
---|
285 | URL getCapabilitiesUrl = null;
|
---|
286 | try {
|
---|
287 | if (!serviceUrlStr.trim().contains("capabilities")) {
|
---|
288 | // If the url doesn't already have GetCapabilities, add it in
|
---|
289 | getCapabilitiesUrl = new URL(serviceUrlStr + "VERSION=1.1.1&SERVICE=WMS&REQUEST=GetCapabilities");
|
---|
290 | } else {
|
---|
291 | // Otherwise assume it's a good URL and let the subsequent error
|
---|
292 | // handling systems deal with problems
|
---|
293 | getCapabilitiesUrl = new URL(serviceUrlStr);
|
---|
294 | }
|
---|
295 | serviceUrl = new URL(serviceUrlStr);
|
---|
296 | } catch (HeadlessException e) {
|
---|
297 | return;
|
---|
298 | } catch (MalformedURLException e) {
|
---|
299 | JOptionPane.showMessageDialog(this, tr("Invalid service URL."),
|
---|
300 | tr("WMS Error"), JOptionPane.ERROR_MESSAGE);
|
---|
301 | return;
|
---|
302 | }
|
---|
303 |
|
---|
304 | String incomingData;
|
---|
305 | try {
|
---|
306 | URLConnection openConnection = getCapabilitiesUrl.openConnection();
|
---|
307 | InputStream inputStream = openConnection.getInputStream();
|
---|
308 | BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
|
---|
309 | String line;
|
---|
310 | StringBuilder ba = new StringBuilder();
|
---|
311 | while((line = br.readLine()) != null) {
|
---|
312 | ba.append(line);
|
---|
313 | ba.append("\n");
|
---|
314 | }
|
---|
315 | incomingData = ba.toString();
|
---|
316 | } catch (IOException e) {
|
---|
317 | JOptionPane.showMessageDialog(this, tr("Could not retrieve WMS layer list."),
|
---|
318 | tr("WMS Error"), JOptionPane.ERROR_MESSAGE);
|
---|
319 | return;
|
---|
320 | }
|
---|
321 |
|
---|
322 | Document document;
|
---|
323 | try {
|
---|
324 | DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
|
---|
325 | builderFactory.setValidating(false);
|
---|
326 | builderFactory.setNamespaceAware(true);
|
---|
327 | DocumentBuilder builder = builderFactory.newDocumentBuilder();
|
---|
328 | builder.setEntityResolver(new EntityResolver() {
|
---|
329 | @Override
|
---|
330 | public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
|
---|
331 | System.out.println("Ignoring DTD " + publicId + ", " + systemId);
|
---|
332 | return new InputSource(new StringReader(""));
|
---|
333 | }
|
---|
334 | });
|
---|
335 | document = builder.parse(new InputSource(new StringReader(incomingData)));
|
---|
336 | } catch (ParserConfigurationException e) {
|
---|
337 | showError(incomingData, e);
|
---|
338 | return;
|
---|
339 | } catch (SAXException e) {
|
---|
340 | showError(incomingData, e);
|
---|
341 | return;
|
---|
342 | } catch (IOException e) {
|
---|
343 | showError(incomingData, e);
|
---|
344 | return;
|
---|
345 | }
|
---|
346 |
|
---|
347 | // Some WMS service URLs specify a different base URL for their GetMap service
|
---|
348 | Element child = getChild(document.getDocumentElement(), "Capability");
|
---|
349 | child = getChild(child, "Request");
|
---|
350 | child = getChild(child, "GetMap");
|
---|
351 | child = getChild(child, "DCPType");
|
---|
352 | child = getChild(child, "HTTP");
|
---|
353 | child = getChild(child, "Get");
|
---|
354 | child = getChild(child, "OnlineResource");
|
---|
355 | if (child != null) {
|
---|
356 | String baseURL = child.getAttribute("xlink:href");
|
---|
357 | if(baseURL != null) {
|
---|
358 | try {
|
---|
359 | System.out.println("GetCapabilities specifies a different service URL: " + baseURL);
|
---|
360 | serviceUrl = new URL(baseURL);
|
---|
361 | } catch (MalformedURLException e1) {
|
---|
362 | }
|
---|
363 | }
|
---|
364 | }
|
---|
365 |
|
---|
366 | try {
|
---|
367 | treeRootNode.setUserObject(getCapabilitiesUrl.getHost());
|
---|
368 | Element capabilityElem = getChild(document.getDocumentElement(), "Capability");
|
---|
369 | List<Element> children = getChildren(capabilityElem, "Layer");
|
---|
370 | List<LayerDetails> layers = parseLayers(children, new HashSet<String>());
|
---|
371 | updateTreeList(layers);
|
---|
372 | } catch(Exception e) {
|
---|
373 | showError(incomingData, e);
|
---|
374 | return;
|
---|
375 | }
|
---|
376 | }
|
---|
377 |
|
---|
378 | private void updateTreeList(List<LayerDetails> layers) {
|
---|
379 | addLayersToTreeData(treeRootNode, layers);
|
---|
380 | layerTree.expandRow(0);
|
---|
381 | }
|
---|
382 |
|
---|
383 | private void addLayersToTreeData(MutableTreeNode parent, List<LayerDetails> layers) {
|
---|
384 | for (LayerDetails layerDetails : layers) {
|
---|
385 | DefaultMutableTreeNode treeNode = new DefaultMutableTreeNode(layerDetails);
|
---|
386 | addLayersToTreeData(treeNode, layerDetails.children);
|
---|
387 | treeData.insertNodeInto(treeNode, parent, 0);
|
---|
388 | }
|
---|
389 | }
|
---|
390 |
|
---|
391 | private List<LayerDetails> parseLayers(List<Element> children, Set<String> parentCrs) {
|
---|
392 | List<LayerDetails> details = new LinkedList<LayerDetails>();
|
---|
393 | for (Element element : children) {
|
---|
394 | details.add(parseLayer(element, parentCrs));
|
---|
395 | }
|
---|
396 | return details;
|
---|
397 | }
|
---|
398 |
|
---|
399 | private LayerDetails parseLayer(Element element, Set<String> parentCrs) {
|
---|
400 | String name = getChildContent(element, "Title", null, null);
|
---|
401 | String ident = getChildContent(element, "Name", null, null);
|
---|
402 |
|
---|
403 | // The set of supported CRS/SRS for this layer
|
---|
404 | Set<String> crsList = new HashSet<String>();
|
---|
405 | // ...including this layer's already-parsed parent projections
|
---|
406 | crsList.addAll(parentCrs);
|
---|
407 |
|
---|
408 | // Parse the CRS/SRS pulled out of this layer's XML element
|
---|
409 | // I think CRS and SRS are the same at this point
|
---|
410 | List<Element> crsChildren = getChildren(element, "CRS");
|
---|
411 | crsChildren.addAll(getChildren(element, "SRS"));
|
---|
412 | for (Element child : crsChildren) {
|
---|
413 | String crs = (String) getContent(child);
|
---|
414 | if(crs != null) {
|
---|
415 | String upperCase = crs.trim().toUpperCase();
|
---|
416 | crsList.add(upperCase);
|
---|
417 | }
|
---|
418 | }
|
---|
419 |
|
---|
420 | // Check to see if any of the specified projections are supported by JOSM
|
---|
421 | boolean josmSupportsThisLayer = false;
|
---|
422 | for (String crs : crsList) {
|
---|
423 | josmSupportsThisLayer |= isProjSupported(crs);
|
---|
424 | }
|
---|
425 |
|
---|
426 | Bounds bounds = null;
|
---|
427 | Element bboxElem = getChild(element, "EX_GeographicBoundingBox");
|
---|
428 | if(bboxElem != null) {
|
---|
429 | // Attempt to use EX_GeographicBoundingBox for bounding box
|
---|
430 | double left = Double.parseDouble(getChildContent(bboxElem, "westBoundLongitude", null, null));
|
---|
431 | double top = Double.parseDouble(getChildContent(bboxElem, "northBoundLatitude", null, null));
|
---|
432 | double right = Double.parseDouble(getChildContent(bboxElem, "eastBoundLongitude", null, null));
|
---|
433 | double bot = Double.parseDouble(getChildContent(bboxElem, "southBoundLatitude", null, null));
|
---|
434 | bounds = new Bounds(bot, left, top, right);
|
---|
435 | } else {
|
---|
436 | // If that's not available, try LatLonBoundingBox
|
---|
437 | bboxElem = getChild(element, "LatLonBoundingBox");
|
---|
438 | if(bboxElem != null) {
|
---|
439 | double left = Double.parseDouble(bboxElem.getAttribute("minx"));
|
---|
440 | double top = Double.parseDouble(bboxElem.getAttribute("maxy"));
|
---|
441 | double right = Double.parseDouble(bboxElem.getAttribute("maxx"));
|
---|
442 | double bot = Double.parseDouble(bboxElem.getAttribute("miny"));
|
---|
443 | bounds = new Bounds(bot, left, top, right);
|
---|
444 | }
|
---|
445 | }
|
---|
446 |
|
---|
447 | List<Element> layerChildren = getChildren(element, "Layer");
|
---|
448 | List<LayerDetails> childLayers = parseLayers(layerChildren, crsList);
|
---|
449 |
|
---|
450 | return new LayerDetails(name, ident, crsList, josmSupportsThisLayer, bounds, childLayers);
|
---|
451 | }
|
---|
452 |
|
---|
453 | private boolean isProjSupported(String crs) {
|
---|
454 | for (Projection proj : Projections.getProjections()) {
|
---|
455 | if (proj instanceof ProjectionSubPrefs) {
|
---|
456 | if (((ProjectionSubPrefs) proj).getPreferencesFromCode(crs) == null) {
|
---|
457 | return true;
|
---|
458 | }
|
---|
459 | } else {
|
---|
460 | if (proj.toCode().equals(crs)) {
|
---|
461 | return true;
|
---|
462 | }
|
---|
463 | }
|
---|
464 | }
|
---|
465 | return false;
|
---|
466 | }
|
---|
467 |
|
---|
468 | public String getUrlName() {
|
---|
469 | return menuName.getText();
|
---|
470 | }
|
---|
471 |
|
---|
472 | public String getUrl() {
|
---|
473 | return resultingLayerField.getText();
|
---|
474 | }
|
---|
475 |
|
---|
476 | public static void main(String[] args) {
|
---|
477 | JFrame f = new JFrame("Test");
|
---|
478 | f.setContentPane(new AddWMSLayerPanel());
|
---|
479 | f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
---|
480 | f.pack();
|
---|
481 | f.setVisible(true);
|
---|
482 | }
|
---|
483 |
|
---|
484 | private static String getChildContent(Element parent, String name, String missing, String empty) {
|
---|
485 | Element child = getChild(parent, name);
|
---|
486 | if (child == null) {
|
---|
487 | return missing;
|
---|
488 | } else {
|
---|
489 | String content = (String) getContent(child);
|
---|
490 | return (content != null) ? content : empty;
|
---|
491 | }
|
---|
492 | }
|
---|
493 |
|
---|
494 | private static Object getContent(Element element) {
|
---|
495 | NodeList nl = element.getChildNodes();
|
---|
496 | StringBuffer content = new StringBuffer();
|
---|
497 | for (int i = 0; i < nl.getLength(); i++) {
|
---|
498 | Node node = nl.item(i);
|
---|
499 | switch (node.getNodeType()) {
|
---|
500 | case Node.ELEMENT_NODE:
|
---|
501 | return node;
|
---|
502 | case Node.CDATA_SECTION_NODE:
|
---|
503 | case Node.TEXT_NODE:
|
---|
504 | content.append(node.getNodeValue());
|
---|
505 | break;
|
---|
506 | }
|
---|
507 | }
|
---|
508 | return content.toString().trim();
|
---|
509 | }
|
---|
510 |
|
---|
511 | private static List<Element> getChildren(Element parent, String name) {
|
---|
512 | List<Element> retVal = new LinkedList<Element>();
|
---|
513 | for (Node child = parent.getFirstChild(); child != null; child = child.getNextSibling()) {
|
---|
514 | if (child instanceof Element && name.equals(child.getNodeName())) {
|
---|
515 | retVal.add((Element) child);
|
---|
516 | }
|
---|
517 | }
|
---|
518 | return retVal;
|
---|
519 | }
|
---|
520 |
|
---|
521 | private static Element getChild(Element parent, String name) {
|
---|
522 | if (parent == null) {
|
---|
523 | return null;
|
---|
524 | }
|
---|
525 | for (Node child = parent.getFirstChild(); child != null; child = child.getNextSibling()) {
|
---|
526 | if (child instanceof Element && name.equals(child.getNodeName())) {
|
---|
527 | return (Element) child;
|
---|
528 | }
|
---|
529 | }
|
---|
530 | return null;
|
---|
531 | }
|
---|
532 |
|
---|
533 | class LayerDetails {
|
---|
534 |
|
---|
535 | private String name;
|
---|
536 | private String ident;
|
---|
537 | private List<LayerDetails> children;
|
---|
538 | private Bounds bounds;
|
---|
539 | private boolean supported;
|
---|
540 |
|
---|
541 | public LayerDetails(String name, String ident, Set<String> crsList,
|
---|
542 | boolean supportedLayer, Bounds bounds,
|
---|
543 | List<LayerDetails> childLayers) {
|
---|
544 | this.name = name;
|
---|
545 | this.ident = ident;
|
---|
546 | this.supported = supportedLayer;
|
---|
547 | this.children = childLayers;
|
---|
548 | this.bounds = bounds;
|
---|
549 | }
|
---|
550 |
|
---|
551 | public boolean isSupported() {
|
---|
552 | return this.supported;
|
---|
553 | }
|
---|
554 |
|
---|
555 | @Override
|
---|
556 | public String toString() {
|
---|
557 | if(this.name == null || this.name.isEmpty()) {
|
---|
558 | return this.ident;
|
---|
559 | } else {
|
---|
560 | return this.name;
|
---|
561 | }
|
---|
562 | }
|
---|
563 |
|
---|
564 | }
|
---|
565 |
|
---|
566 | class LayerTreeCellRenderer extends DefaultTreeCellRenderer {
|
---|
567 | @Override
|
---|
568 | public Component getTreeCellRendererComponent(JTree tree, Object value,
|
---|
569 | boolean sel, boolean expanded, boolean leaf, int row,
|
---|
570 | boolean hasFocus) {
|
---|
571 | super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf,
|
---|
572 | row, hasFocus);
|
---|
573 | DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) value;
|
---|
574 | Object userObject = treeNode.getUserObject();
|
---|
575 | if (userObject instanceof LayerDetails) {
|
---|
576 | LayerDetails layer = (LayerDetails) userObject;
|
---|
577 | setEnabled(layer.isSupported());
|
---|
578 | }
|
---|
579 | return this;
|
---|
580 | }
|
---|
581 | }
|
---|
582 |
|
---|
583 | }
|
---|