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