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