|
3 | 3 |
|
4 | 4 | import static org.openstreetmap.josm.tools.I18n.tr; |
5 | 5 |
|
| 6 | +import java.awt.GraphicsEnvironment; |
| 7 | +import java.awt.event.ActionEvent; |
6 | 8 | import java.awt.event.ActionListener; |
7 | 9 | import java.io.IOException; |
8 | 10 | import java.net.MalformedURLException; |
|
15 | 17 | import javax.swing.JButton; |
16 | 18 | import javax.swing.JCheckBox; |
17 | 19 | import javax.swing.JComboBox; |
| 20 | +import javax.swing.JComponent; |
18 | 21 | import javax.swing.JLabel; |
19 | 22 | import javax.swing.JOptionPane; |
20 | 23 | import javax.swing.JScrollPane; |
|
23 | 26 | import org.openstreetmap.josm.data.imagery.ImageryInfo; |
24 | 27 | import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryType; |
25 | 28 | import org.openstreetmap.josm.data.imagery.LayerDetails; |
| 29 | +import org.openstreetmap.josm.gui.MainApplication; |
| 30 | +import org.openstreetmap.josm.gui.PleaseWaitRunnable; |
26 | 31 | import org.openstreetmap.josm.gui.bbox.SlippyMapBBoxChooser; |
| 32 | +import org.openstreetmap.josm.gui.progress.NullProgressMonitor; |
| 33 | +import org.openstreetmap.josm.gui.progress.ProgressMonitor; |
| 34 | +import org.openstreetmap.josm.gui.progress.swing.PleaseWaitProgressMonitor; |
27 | 35 | import org.openstreetmap.josm.gui.util.GuiHelper; |
28 | 36 | import org.openstreetmap.josm.gui.widgets.JosmTextArea; |
29 | 37 | import org.openstreetmap.josm.io.imagery.WMSImagery; |
@@ -81,37 +89,7 @@ public AddWMSLayerPanel() { |
81 | 89 | add(new JLabel(tr("{0} Enter name for this layer", "7.")), GBC.eol()); |
82 | 90 | add(name, GBC.eop().fill(GBC.HORIZONTAL)); |
83 | 91 |
|
84 | | - getLayers.addActionListener(e -> { |
85 | | - try { |
86 | | - wms = new WMSImagery(Utils.strip(rawUrl.getText()), getCommonHeaders()); |
87 | | - tree.updateTree(wms); |
88 | | - Collection<String> wmsFormats = wms.getFormats(); |
89 | | - formats.setModel(new DefaultComboBoxModel<>(wmsFormats.toArray(new String[0]))); |
90 | | - formats.setSelectedItem(wms.getPreferredFormat()); |
91 | | - } catch (MalformedURLException | InvalidPathException ex1) { |
92 | | - Logging.log(Logging.LEVEL_ERROR, ex1); |
93 | | - JOptionPane.showMessageDialog(getParent(), tr("Invalid service URL."), |
94 | | - tr("WMS Error"), JOptionPane.ERROR_MESSAGE); |
95 | | - } catch (IOException ex2) { |
96 | | - Logging.log(Logging.LEVEL_ERROR, ex2); |
97 | | - JOptionPane.showMessageDialog(getParent(), tr("Could not retrieve WMS layer list."), |
98 | | - tr("WMS Error"), JOptionPane.ERROR_MESSAGE); |
99 | | - } catch (WMSImagery.WMSGetCapabilitiesException ex3) { |
100 | | - String incomingData = ex3.getIncomingData() != null ? ex3.getIncomingData().trim() : ""; |
101 | | - String title = tr("WMS Error"); |
102 | | - StringBuilder message = new StringBuilder(tr("Could not parse WMS layer list.")); |
103 | | - Logging.log(Logging.LEVEL_ERROR, "Could not parse WMS layer list. Incoming data:\n"+incomingData, ex3); |
104 | | - if ((incomingData.startsWith("<html>") || incomingData.startsWith("<HTML>")) |
105 | | - && (incomingData.endsWith("</html>") || incomingData.endsWith("</HTML>"))) { |
106 | | - GuiHelper.notifyUserHtmlError(this, title, message.toString(), incomingData); |
107 | | - } else { |
108 | | - if (ex3.getMessage() != null) { |
109 | | - message.append('\n').append(ex3.getMessage()); |
110 | | - } |
111 | | - JOptionPane.showMessageDialog(getParent(), message.toString(), title, JOptionPane.ERROR_MESSAGE); |
112 | | - } |
113 | | - } |
114 | | - }); |
| 92 | + getLayers.addActionListener(e -> MainApplication.worker.execute(new GetCapabilitiesRunnable(e))); |
115 | 93 |
|
116 | 94 | ActionListener availabilityManagerAction = a -> { |
117 | 95 | setDefaultLayers.setEnabled(endpoint.isSelected()); |
@@ -225,4 +203,70 @@ protected boolean isImageryValid() { |
225 | 203 | return !getWmsUrl().isEmpty(); |
226 | 204 | } |
227 | 205 | } |
| 206 | + |
| 207 | + /** |
| 208 | + * Perform the get WMS layers network calls in a separate thread, mostly to allow for cancellation |
| 209 | + */ |
| 210 | + private class GetCapabilitiesRunnable extends PleaseWaitRunnable { |
| 211 | + private final ActionEvent actionEvent; |
| 212 | + |
| 213 | + GetCapabilitiesRunnable(ActionEvent actionEvent) { |
| 214 | + super(tr("Trying WMS urls"), GraphicsEnvironment.isHeadless() ? NullProgressMonitor.INSTANCE : new PleaseWaitProgressMonitor(), |
| 215 | + false); |
| 216 | + this.actionEvent = actionEvent; |
| 217 | + } |
| 218 | + |
| 219 | + @Override |
| 220 | + protected void cancel() { |
| 221 | + // We don't really have something we can do -- we use the monitor to pass the cancel state on |
| 222 | + } |
| 223 | + |
| 224 | + @Override |
| 225 | + protected void realRun() { |
| 226 | + if (actionEvent.getSource() instanceof JComponent) { |
| 227 | + GuiHelper.runInEDT(() -> ((JComponent) actionEvent.getSource()).setEnabled(false)); |
| 228 | + } |
| 229 | + ProgressMonitor monitor = getProgressMonitor(); |
| 230 | + try { |
| 231 | + wms = new WMSImagery(Utils.strip(rawUrl.getText()), getCommonHeaders(), monitor); |
| 232 | + } catch (MalformedURLException | InvalidPathException ex1) { |
| 233 | + Logging.log(Logging.LEVEL_ERROR, ex1); |
| 234 | + GuiHelper.runInEDT(() -> JOptionPane.showMessageDialog(getParent(), tr("Invalid service URL."), |
| 235 | + tr("WMS Error"), JOptionPane.ERROR_MESSAGE)); |
| 236 | + } catch (IOException ex2) { |
| 237 | + Logging.log(Logging.LEVEL_ERROR, ex2); |
| 238 | + GuiHelper.runInEDT(() -> JOptionPane.showMessageDialog(getParent(), tr("Could not retrieve WMS layer list."), |
| 239 | + tr("WMS Error"), JOptionPane.ERROR_MESSAGE)); |
| 240 | + } catch (WMSImagery.WMSGetCapabilitiesException ex3) { |
| 241 | + String incomingData = ex3.getIncomingData() != null ? ex3.getIncomingData().trim() : ""; |
| 242 | + String title = tr("WMS Error"); |
| 243 | + StringBuilder message = new StringBuilder(tr("Could not parse WMS layer list.")); |
| 244 | + Logging.log(Logging.LEVEL_ERROR, "Could not parse WMS layer list. Incoming data:\n"+incomingData, ex3); |
| 245 | + if ((incomingData.startsWith("<html>") || incomingData.startsWith("<HTML>")) |
| 246 | + && (incomingData.endsWith("</html>") || incomingData.endsWith("</HTML>"))) { |
| 247 | + GuiHelper.runInEDT(() -> GuiHelper.notifyUserHtmlError(AddWMSLayerPanel.this, title, message.toString(), incomingData)); |
| 248 | + } else { |
| 249 | + if (ex3.getMessage() != null) { |
| 250 | + message.append('\n').append(ex3.getMessage()); |
| 251 | + } |
| 252 | + GuiHelper.runInEDT(() -> JOptionPane.showMessageDialog(getParent(), message.toString(), title, JOptionPane.ERROR_MESSAGE)); |
| 253 | + } |
| 254 | + } |
| 255 | + } |
| 256 | + |
| 257 | + @Override |
| 258 | + protected void finish() { |
| 259 | + if (wms != null) { |
| 260 | + GuiHelper.runInEDT(() -> { |
| 261 | + tree.updateTree(wms); |
| 262 | + Collection<String> wmsFormats = wms.getFormats(); |
| 263 | + formats.setModel(new DefaultComboBoxModel<>(wmsFormats.toArray(new String[0]))); |
| 264 | + formats.setSelectedItem(wms.getPreferredFormat()); |
| 265 | + }); |
| 266 | + } |
| 267 | + if (actionEvent.getSource() instanceof JComponent) { |
| 268 | + GuiHelper.runInEDT(() -> ((JComponent) actionEvent.getSource()).setEnabled(true)); |
| 269 | + } |
| 270 | + } |
| 271 | + } |
228 | 272 | } |
0 commit comments