1 | // License: GPL. v2 and later. Copyright 2008-2009 by Pieren <pieren3@gmail.com> and others
|
---|
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 | public 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.showMessageDialog(Main.parent,tr("Image already loaded"));
|
---|
38 | JOptionPane pane = new JOptionPane(
|
---|
39 | tr("Image already loaded")
|
---|
40 | , JOptionPane.INFORMATION_MESSAGE);
|
---|
41 | // this below is a temporary workaround to fix the "always on top" issue
|
---|
42 | JDialog dialog = pane.createDialog(Main.parent, "");
|
---|
43 | CadastrePlugin.prepareDialog(dialog);
|
---|
44 | dialog.setVisible(true);
|
---|
45 | // till here
|
---|
46 | dontGeoreference = true;
|
---|
47 | } else if (wmsLayer.grabber.getWmsInterface().downloadCanceled){
|
---|
48 | // do nothing
|
---|
49 | } else {
|
---|
50 | // first time we grab an image for this layer
|
---|
51 | if (CacheControl.cacheEnabled) {
|
---|
52 | if (wmsLayer.grabThread.getCacheControl().loadCacheIfExist()) {
|
---|
53 | dontGeoreference = true;
|
---|
54 | Main.map.mapView.repaint();
|
---|
55 | return;
|
---|
56 | }
|
---|
57 | }
|
---|
58 | if (wmsLayer.isRaster()) {
|
---|
59 | // set raster image commune bounding box based on current view (before adjustment)
|
---|
60 | wmsLayer.grabber.getWmsInterface().retrieveCommuneBBox(wmsLayer);
|
---|
61 | wmsLayer.setRasterBounds(bounds);
|
---|
62 | // grab new images from wms server into active layer
|
---|
63 | wmsLayer.grab(bounds);
|
---|
64 | if (wmsLayer.grabber.getWmsInterface().downloadCanceled) {
|
---|
65 | wmsLayer.clearImages();
|
---|
66 | Main.map.mapView.repaint();
|
---|
67 | } else {
|
---|
68 | // next steps follow in method finish() when download is terminated
|
---|
69 | wmsLayer.joinBufferedImages();
|
---|
70 | }
|
---|
71 | } else {
|
---|
72 | /*JOptionPane.showMessageDialog(Main.parent,tr("Municipality vectorized !\n"+
|
---|
73 | "Use the normal Cadastre Grab menu."));*/
|
---|
74 | JOptionPane pane = new JOptionPane(
|
---|
75 | tr("Municipality vectorized !\nUse the normal Cadastre Grab menu.")
|
---|
76 | , JOptionPane.INFORMATION_MESSAGE);
|
---|
77 | // this below is a temporary workaround to fix the "always on top" issue
|
---|
78 | JDialog dialog = pane.createDialog(Main.parent, "");
|
---|
79 | CadastrePlugin.prepareDialog(dialog);
|
---|
80 | dialog.setVisible(true);
|
---|
81 | // till here
|
---|
82 | }
|
---|
83 | }
|
---|
84 | }
|
---|
85 | } catch (DuplicateLayerException e) {
|
---|
86 | // we tried to grab onto a duplicated layer (removed)
|
---|
87 | Main.warn("removed a duplicated layer");
|
---|
88 | } catch (WMSException e) {
|
---|
89 | errorMessage = e.getMessage();
|
---|
90 | wmsLayer.grabber.getWmsInterface().resetCookie();
|
---|
91 | }
|
---|
92 | }
|
---|
93 |
|
---|
94 | @Override
|
---|
95 | protected void cancel() {
|
---|
96 | wmsLayer.grabber.getWmsInterface().cancel();
|
---|
97 | dontGeoreference = true;
|
---|
98 | }
|
---|
99 |
|
---|
100 | @Override
|
---|
101 | protected void finish() {
|
---|
102 | }
|
---|
103 | }
|
---|
104 |
|
---|
105 | public void download(WMSLayer wmsLayer) {
|
---|
106 | MapView mv = Main.map.mapView;
|
---|
107 | Bounds bounds = new Bounds(mv.getLatLon(0, mv.getHeight()), mv.getLatLon(mv.getWidth(), 0));
|
---|
108 | dontGeoreference = false;
|
---|
109 |
|
---|
110 | //Main.worker.execute(new DownloadWMSPlanImage(wmsLayer, bounds));
|
---|
111 | Task t = new Task(wmsLayer, bounds);
|
---|
112 | this.wmsLayer = wmsLayer;
|
---|
113 | this.bounds = bounds;
|
---|
114 | task = Main.worker.submit(t, t);
|
---|
115 | if (errorMessage != null)
|
---|
116 | JOptionPane.showMessageDialog(Main.parent, errorMessage);
|
---|
117 | }
|
---|
118 |
|
---|
119 | public boolean waitFinished() {
|
---|
120 | if (task != null) {
|
---|
121 | try {
|
---|
122 | task.get();
|
---|
123 | } catch (Exception e) {
|
---|
124 | e.printStackTrace();
|
---|
125 | }
|
---|
126 | }
|
---|
127 | return dontGeoreference;
|
---|
128 | }
|
---|
129 | }
|
---|