1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package cadastre_fr;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.io.IOException;
|
---|
7 | import java.util.concurrent.Future;
|
---|
8 |
|
---|
9 | import javax.swing.JDialog;
|
---|
10 | import javax.swing.JOptionPane;
|
---|
11 |
|
---|
12 | import org.openstreetmap.josm.Main;
|
---|
13 | import org.openstreetmap.josm.data.Bounds;
|
---|
14 | import org.openstreetmap.josm.gui.MapView;
|
---|
15 | import org.openstreetmap.josm.gui.PleaseWaitRunnable;
|
---|
16 |
|
---|
17 | public class DownloadWMSPlanImage {
|
---|
18 |
|
---|
19 | private Future<Task> task = null;
|
---|
20 | private WMSLayer wmsLayer;
|
---|
21 | private Bounds bounds;
|
---|
22 | private static boolean dontGeoreference = false;
|
---|
23 | private static String errorMessage;
|
---|
24 |
|
---|
25 | private class Task extends PleaseWaitRunnable {
|
---|
26 | Task(WMSLayer wmsLayer, Bounds bounds) {
|
---|
27 | super(tr("Downloading {0}", wmsLayer.getName()));
|
---|
28 | }
|
---|
29 |
|
---|
30 | @Override
|
---|
31 | public void realRun() throws IOException {
|
---|
32 | progressMonitor.indeterminateSubTask(tr("Contacting cadastre WMS ..."));
|
---|
33 | errorMessage = null;
|
---|
34 | try {
|
---|
35 | if (wmsLayer.grabber.getWmsInterface().retrieveInterface(wmsLayer)) {
|
---|
36 | if (!wmsLayer.getImages().isEmpty()) {
|
---|
37 | JOptionPane pane = new JOptionPane(tr("Image already loaded"), JOptionPane.INFORMATION_MESSAGE);
|
---|
38 | // this below is a temporary workaround to fix the "always on top" issue
|
---|
39 | JDialog dialog = pane.createDialog(Main.parent, "");
|
---|
40 | CadastrePlugin.prepareDialog(dialog);
|
---|
41 | dialog.setVisible(true);
|
---|
42 | // till here
|
---|
43 | dontGeoreference = true;
|
---|
44 | } else if (wmsLayer.grabber.getWmsInterface().downloadCanceled) {
|
---|
45 | // do nothing
|
---|
46 | } else {
|
---|
47 | // first time we grab an image for this layer
|
---|
48 | if (CacheControl.cacheEnabled) {
|
---|
49 | if (wmsLayer.grabThread.getCacheControl().loadCacheIfExist()) {
|
---|
50 | dontGeoreference = true;
|
---|
51 | Main.map.mapView.repaint();
|
---|
52 | return;
|
---|
53 | }
|
---|
54 | }
|
---|
55 | if (wmsLayer.isRaster()) {
|
---|
56 | // set raster image commune bounding box based on current view (before adjustment)
|
---|
57 | wmsLayer.grabber.getWmsInterface().retrieveCommuneBBox(wmsLayer);
|
---|
58 | wmsLayer.setRasterBounds(bounds);
|
---|
59 | // grab new images from wms server into active layer
|
---|
60 | wmsLayer.grab(bounds);
|
---|
61 | if (wmsLayer.grabber.getWmsInterface().downloadCanceled) {
|
---|
62 | wmsLayer.clearImages();
|
---|
63 | Main.map.mapView.repaint();
|
---|
64 | } else {
|
---|
65 | // next steps follow in method finish() when download is terminated
|
---|
66 | wmsLayer.joinBufferedImages();
|
---|
67 | }
|
---|
68 | } else {
|
---|
69 | /*JOptionPane.showMessageDialog(Main.parent,tr("Municipality vectorized !\n"+
|
---|
70 | "Use the normal Cadastre Grab menu."));*/
|
---|
71 | JOptionPane pane = new JOptionPane(
|
---|
72 | tr("Municipality vectorized !\nUse the normal Cadastre Grab menu."),
|
---|
73 | JOptionPane.INFORMATION_MESSAGE);
|
---|
74 | // this below is a temporary workaround to fix the "always on top" issue
|
---|
75 | JDialog dialog = pane.createDialog(Main.parent, "");
|
---|
76 | CadastrePlugin.prepareDialog(dialog);
|
---|
77 | dialog.setVisible(true);
|
---|
78 | // till here
|
---|
79 | }
|
---|
80 | }
|
---|
81 | }
|
---|
82 | } catch (DuplicateLayerException e) {
|
---|
83 | // we tried to grab onto a duplicated layer (removed)
|
---|
84 | Main.warn("removed a duplicated layer");
|
---|
85 | } catch (WMSException e) {
|
---|
86 | errorMessage = e.getMessage();
|
---|
87 | wmsLayer.grabber.getWmsInterface().resetCookie();
|
---|
88 | }
|
---|
89 | }
|
---|
90 |
|
---|
91 | @Override
|
---|
92 | protected void cancel() {
|
---|
93 | wmsLayer.grabber.getWmsInterface().cancel();
|
---|
94 | dontGeoreference = true;
|
---|
95 | }
|
---|
96 |
|
---|
97 | @Override
|
---|
98 | protected void finish() {
|
---|
99 | }
|
---|
100 | }
|
---|
101 |
|
---|
102 | public void download(WMSLayer wmsLayer) {
|
---|
103 | MapView mv = Main.map.mapView;
|
---|
104 | Bounds bounds = new Bounds(mv.getLatLon(0, mv.getHeight()), mv.getLatLon(mv.getWidth(), 0));
|
---|
105 | dontGeoreference = false;
|
---|
106 |
|
---|
107 | //Main.worker.execute(new DownloadWMSPlanImage(wmsLayer, bounds));
|
---|
108 | Task t = new Task(wmsLayer, bounds);
|
---|
109 | this.wmsLayer = wmsLayer;
|
---|
110 | this.bounds = bounds;
|
---|
111 | task = Main.worker.submit(t, t);
|
---|
112 | if (errorMessage != null)
|
---|
113 | JOptionPane.showMessageDialog(Main.parent, errorMessage);
|
---|
114 | }
|
---|
115 |
|
---|
116 | public boolean waitFinished() {
|
---|
117 | if (task != null) {
|
---|
118 | try {
|
---|
119 | task.get();
|
---|
120 | } catch (Exception e) {
|
---|
121 | e.printStackTrace();
|
---|
122 | }
|
---|
123 | }
|
---|
124 | return dontGeoreference;
|
---|
125 | }
|
---|
126 | }
|
---|