source: osm/applications/editors/josm/plugins/cadastre-fr/src/cadastre_fr/GrabThread.java@ 32060

Last change on this file since 32060 was 32060, checked in by donvip, 9 years ago

remove tabs

File size: 8.0 KB
Line 
1// License: GPL. v2 and later. Copyright 2008-2009 by Pieren <pieren3@gmail.com> and others
2package cadastre_fr;
3
4import java.awt.Color;
5import java.awt.Graphics;
6import java.awt.Point;
7import java.io.IOException;
8import java.util.ArrayList;
9import java.util.concurrent.locks.Lock;
10import java.util.concurrent.locks.ReentrantLock;
11
12import org.openstreetmap.josm.Main;
13import org.openstreetmap.josm.data.coor.EastNorth;
14import org.openstreetmap.josm.gui.MapView;
15import org.openstreetmap.josm.io.OsmTransferException;
16
17public class GrabThread extends Thread {
18
19 private boolean canceled;
20
21 private CadastreGrabber grabber;
22
23 private WMSLayer wmsLayer;
24
25 private Lock lockImagesToGrag = new ReentrantLock();
26
27 private ArrayList<EastNorthBound> imagesToGrab = new ArrayList<>();
28
29 private CacheControl cacheControl = null;
30
31 private EastNorthBound currentGrabImage;
32
33 private Lock lockCurrentGrabImage = new ReentrantLock();
34
35 /**
36 * Call directly grabber for raster images or prepare thread for vector images
37 * @param moreImages
38 */
39 public void addImages(ArrayList<EastNorthBound> moreImages) {
40 lockImagesToGrag.lock();
41 imagesToGrab.addAll(moreImages);
42 lockImagesToGrag.unlock();
43 synchronized(this) {
44 this.notify();
45 }
46 Main.info("Added " + moreImages.size() + " to the grab thread");
47 if (wmsLayer.isRaster()) {
48 waitNotification();
49 }
50 }
51
52 public int getImagesToGrabSize() {
53 lockImagesToGrag.lock();
54 int size = imagesToGrab.size();
55 lockImagesToGrag.unlock();
56 return size;
57 }
58
59 public ArrayList<EastNorthBound> getImagesToGrabCopy() {
60 ArrayList<EastNorthBound> copyList = new ArrayList<>();
61 lockImagesToGrag.lock();
62 for (EastNorthBound img : imagesToGrab) {
63 EastNorthBound imgCpy = new EastNorthBound(img.min, img.max);
64 copyList.add(imgCpy);
65 }
66 lockImagesToGrag.unlock();
67 return copyList;
68 }
69
70 public void clearImagesToGrab() {
71 lockImagesToGrag.lock();
72 imagesToGrab.clear();
73 lockImagesToGrag.unlock();
74 }
75
76 @Override
77 public void run() {
78 for (;;) {
79 while (getImagesToGrabSize() > 0) {
80 lockImagesToGrag.lock();
81 lockCurrentGrabImage.lock();
82 currentGrabImage = imagesToGrab.get(0);
83 lockCurrentGrabImage.unlock();
84 imagesToGrab.remove(0);
85 lockImagesToGrag.unlock();
86 if (canceled) {
87 break;
88 } else {
89 GeorefImage newImage;
90 try {
91 Main.map.repaint(); // paint the current grab box
92 newImage = grabber.grab(wmsLayer, currentGrabImage.min, currentGrabImage.max);
93 } catch (IOException e) {
94 Main.warn("Download action canceled by user or server did not respond");
95 setCanceled(true);
96 break;
97 } catch (OsmTransferException e) {
98 Main.error("OSM transfer failed");
99 setCanceled(true);
100 break;
101 }
102 if (grabber.getWmsInterface().downloadCanceled) {
103 Main.info("Download action canceled by user");
104 setCanceled(true);
105 break;
106 }
107 try {
108 if (CadastrePlugin.backgroundTransparent) {
109 wmsLayer.imagesLock.lock();
110 for (GeorefImage img : wmsLayer.getImages()) {
111 if (img.overlap(newImage))
112 // mask overlapping zone in already grabbed image
113 img.withdraw(newImage);
114 else
115 // mask overlapping zone in new image only when new image covers completely the
116 // existing image
117 newImage.withdraw(img);
118 }
119 wmsLayer.imagesLock.unlock();
120 }
121 wmsLayer.addImage(newImage);
122 Main.map.mapView.repaint();
123 saveToCache(newImage);
124 } catch (NullPointerException e) {
125 Main.info("Layer destroyed. Cancel grab thread");
126 setCanceled(true);
127 }
128 }
129 }
130 Main.info("grab thread list empty");
131 lockCurrentGrabImage.lock();
132 currentGrabImage = null;
133 lockCurrentGrabImage.unlock();
134 if (canceled) {
135 clearImagesToGrab();
136 canceled = false;
137 }
138 if (wmsLayer.isRaster()) {
139 notifyWaiter();
140 }
141 waitNotification(); }
142 }
143
144 public void saveToCache(GeorefImage image) {
145 if (CacheControl.cacheEnabled && !wmsLayer.isRaster()) {
146 getCacheControl().saveCache(image);
147 }
148 }
149
150 public void saveNewCache() {
151 if (CacheControl.cacheEnabled) {
152 getCacheControl().deleteCacheFile();
153 wmsLayer.imagesLock.lock();
154 for (GeorefImage image : wmsLayer.getImages())
155 getCacheControl().saveCache(image);
156 wmsLayer.imagesLock.unlock();
157 }
158 }
159
160 public void cancel() {
161 clearImagesToGrab();
162 if (cacheControl != null) {
163 while (!cacheControl.isCachePipeEmpty()) {
164 Main.info("Try to close a WMSLayer which is currently saving in cache : wait 1 sec.");
165 CadastrePlugin.safeSleep(1000);
166 }
167 }
168 }
169
170 public CacheControl getCacheControl() {
171 if (cacheControl == null)
172 cacheControl = new CacheControl(wmsLayer);
173 return cacheControl;
174 }
175
176 public GrabThread(WMSLayer wmsLayer) {
177 this.wmsLayer = wmsLayer;
178 }
179
180 public void paintBoxesToGrab(Graphics g, MapView mv) {
181 if (getImagesToGrabSize() > 0) {
182 ArrayList<EastNorthBound> imagesToGrab = getImagesToGrabCopy();
183 for (EastNorthBound img : imagesToGrab) {
184 paintBox(g, mv, img, Color.red);
185 }
186 }
187 lockCurrentGrabImage.lock();
188 if (currentGrabImage != null) {
189 paintBox(g, mv, currentGrabImage, Color.orange);
190 }
191 lockCurrentGrabImage.unlock();
192 }
193
194 private void paintBox(Graphics g, MapView mv, EastNorthBound img, Color color) {
195 Point[] croppedPoint = new Point[5];
196 croppedPoint[0] = mv.getPoint(img.min);
197 croppedPoint[1] = mv.getPoint(new EastNorth(img.min.east(), img.max.north()));
198 croppedPoint[2] = mv.getPoint(img.max);
199 croppedPoint[3] = mv.getPoint(new EastNorth(img.max.east(), img.min.north()));
200 croppedPoint[4] = croppedPoint[0];
201 for (int i=0; i<4; i++) {
202 g.setColor(color);
203 g.drawLine(croppedPoint[i].x, croppedPoint[i].y, croppedPoint[i+1].x, croppedPoint[i+1].y);
204 }
205 }
206
207 public boolean isCanceled() {
208 return canceled;
209 }
210
211 public void setCanceled(boolean canceled) {
212 this.canceled = canceled;
213 }
214
215 public CadastreGrabber getGrabber() {
216 return grabber;
217 }
218
219 public void setGrabber(CadastreGrabber grabber) {
220 this.grabber = grabber;
221 }
222
223 private synchronized void notifyWaiter() {
224 this.notify();
225 }
226
227 private synchronized void waitNotification() {
228 try {
229 wait();
230 } catch (InterruptedException e) {
231 Main.error(e);
232 }
233 }
234}
Note: See TracBrowser for help on using the repository browser.