Mercurial > projects > dwt-addons
annotate dwtx/ui/internal/forms/widgets/BusyIndicator.d @ 77:26c6c9dfd13c
ui.forms compile, just FormTextModel with xml reimpl left todo
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sat, 24 May 2008 06:59:31 +0200 |
parents | e193036d82c9 |
children | 7ffeace6c47f |
rev | line source |
---|---|
75 | 1 /******************************************************************************* |
2 * Copyright (c) 2006, 2007 IBM Corporation and others. | |
3 * All rights reserved. This program and the accompanying materials | |
4 * are made available under the terms of the Eclipse Public License v1.0 | |
5 * which accompanies this distribution, and is available at | |
6 * http://www.eclipse.org/legal/epl-v10.html | |
7 * | |
8 * Contributors: | |
9 * IBM Corporation - initial API and implementation | |
10 * Stefan Mucke - fix for Bug 156456 | |
11 * Port to the D programming language: | |
12 * Frank Benoit <benoit@tionex.de> | |
13 *******************************************************************************/ | |
14 module dwtx.ui.internal.forms.widgets.BusyIndicator; | |
15 | |
16 import dwtx.ui.internal.forms.widgets.FormUtil; | |
17 | |
18 import dwt.DWT; | |
19 import dwt.events.PaintEvent; | |
20 import dwt.events.PaintListener; | |
21 import dwt.graphics.GC; | |
22 import dwt.graphics.Image; | |
23 import dwt.graphics.ImageData; | |
24 import dwt.graphics.Point; | |
25 import dwt.graphics.Rectangle; | |
26 import dwt.widgets.Canvas; | |
27 import dwt.widgets.Composite; | |
28 import dwt.widgets.Display; | |
29 import dwtx.jface.resource.ImageDescriptor; | |
30 | |
31 import dwt.dwthelper.utils; | |
32 import dwt.dwthelper.Runnable; | |
33 | |
34 import tango.util.Convert; | |
35 import tango.core.Thread; | |
36 | |
37 public final class BusyIndicator : Canvas { | |
38 | |
77
26c6c9dfd13c
ui.forms compile, just FormTextModel with xml reimpl left todo
Frank Benoit <benoit@tionex.de>
parents:
76
diff
changeset
|
39 alias Canvas.computeSize computeSize; |
26c6c9dfd13c
ui.forms compile, just FormTextModel with xml reimpl left todo
Frank Benoit <benoit@tionex.de>
parents:
76
diff
changeset
|
40 |
75 | 41 class BusyThread : Thread { |
42 Rectangle bounds; | |
43 Display display; | |
44 GC offScreenImageGC; | |
45 Image offScreenImage; | |
46 Image timage; | |
47 bool stop; | |
48 | |
49 private this(Rectangle bounds, Display display, GC offScreenImageGC, Image offScreenImage) { | |
50 this.bounds = bounds; | |
51 this.display = display; | |
52 this.offScreenImageGC = offScreenImageGC; | |
53 this.offScreenImage = offScreenImage; | |
54 } | |
55 | |
56 public void run() { | |
57 try { | |
58 /* | |
59 * Create an off-screen image to draw on, and fill it with | |
60 * the shell background. | |
61 */ | |
62 FormUtil.setAntialias(offScreenImageGC, DWT.ON); | |
63 display.syncExec(dgRunnable( { | |
64 if (!isDisposed()) | |
65 drawBackground(offScreenImageGC, 0, 0, | |
66 bounds.width, | |
67 bounds.height); | |
68 })); | |
69 if (isDisposed()) | |
70 return; | |
71 | |
72 /* | |
73 * Create the first image and draw it on the off-screen | |
74 * image. | |
75 */ | |
76 int imageDataIndex = 0; | |
77 ImageData imageData; | |
78 synchronized (this.outer) { | |
79 timage = getImage(imageDataIndex); | |
80 imageData = timage.getImageData(); | |
81 offScreenImageGC.drawImage(timage, 0, 0, | |
82 imageData.width, imageData.height, imageData.x, | |
83 imageData.y, imageData.width, imageData.height); | |
84 } | |
85 | |
86 /* | |
87 * Now loop through the images, creating and drawing | |
88 * each one on the off-screen image before drawing it on | |
89 * the shell. | |
90 */ | |
91 while (!stop && !isDisposed() && timage !is null) { | |
92 | |
93 /* | |
94 * Fill with the background color before | |
95 * drawing. | |
96 */ | |
97 display.syncExec(dgRunnable( (ImageData fimageData){ | |
98 if (!isDisposed()) { | |
99 drawBackground(offScreenImageGC, fimageData.x, | |
100 fimageData.y, fimageData.width, | |
101 fimageData.height); | |
102 } | |
103 }, imageData )); | |
104 | |
105 synchronized (this.outer) { | |
106 imageDataIndex = (imageDataIndex + 1) % IMAGE_COUNT; | |
107 timage = getImage(imageDataIndex); | |
108 imageData = timage.getImageData(); | |
109 offScreenImageGC.drawImage(timage, 0, 0, | |
110 imageData.width, imageData.height, | |
111 imageData.x, imageData.y, imageData.width, | |
112 imageData.height); | |
113 } | |
114 | |
115 /* Draw the off-screen image to the shell. */ | |
116 animationImage = offScreenImage; | |
117 display.syncExec(dgRunnable( { | |
118 if (!isDisposed()) | |
119 redraw(); | |
120 })); | |
121 /* | |
122 * Sleep for the specified delay time | |
123 */ | |
124 try { | |
125 Thread.sleep(MILLISECONDS_OF_DELAY/1000.0); | |
126 } catch (InterruptedException e) { | |
76 | 127 ExceptionPrintStackTrace(e); |
75 | 128 } |
129 | |
130 | |
131 } | |
132 } catch (Exception e) { | |
133 } finally { | |
134 display.syncExec(dgRunnable( { | |
135 if (offScreenImage !is null | |
136 && !offScreenImage.isDisposed()) | |
137 offScreenImage.dispose(); | |
138 if (offScreenImageGC !is null | |
139 && !offScreenImageGC.isDisposed()) | |
140 offScreenImageGC.dispose(); | |
141 })); | |
142 clearImages(); | |
143 } | |
144 if (busyThread is null) | |
145 display.syncExec(dgRunnable( { | |
146 animationImage = null; | |
147 if (!isDisposed()) | |
148 redraw(); | |
149 })); | |
150 } | |
151 | |
152 public void setStop(bool stop) { | |
153 this.stop = stop; | |
154 } | |
155 } | |
156 | |
157 private static const int MARGIN = 0; | |
158 private static const int IMAGE_COUNT = 8; | |
159 private static const int MILLISECONDS_OF_DELAY = 180; | |
160 private Image[] imageCache; | |
161 protected Image image; | |
162 | |
163 protected Image animationImage; | |
164 | |
165 protected BusyThread busyThread; | |
166 | |
167 /** | |
168 * BusyWidget constructor comment. | |
169 * | |
170 * @param parent | |
171 * dwt.widgets.Composite | |
172 * @param style | |
173 * int | |
174 */ | |
175 public this(Composite parent, int style) { | |
176 super(parent, style); | |
177 | |
178 addPaintListener(new class PaintListener { | |
179 public void paintControl(PaintEvent event) { | |
180 onPaint(event); | |
181 } | |
182 }); | |
183 } | |
184 | |
185 public Point computeSize(int wHint, int hHint, bool changed) { | |
186 Point size = new Point(0, 0); | |
187 if (image !is null) { | |
188 Rectangle ibounds = image.getBounds(); | |
189 size.x = ibounds.width; | |
190 size.y = ibounds.height; | |
191 } | |
192 if (isBusy()) { | |
193 Rectangle bounds = getImage(0).getBounds(); | |
194 size.x = Math.max(size.x, bounds.width); | |
195 size.y = Math.max(size.y, bounds.height); | |
196 } | |
197 size.x += MARGIN + MARGIN; | |
198 size.y += MARGIN + MARGIN; | |
199 return size; | |
200 } | |
201 | |
202 /* (non-Javadoc) | |
203 * @see dwt.widgets.Control#forceFocus() | |
204 */ | |
205 public bool forceFocus() { | |
206 return false; | |
207 } | |
208 | |
209 /** | |
210 * Creates a thread to animate the image. | |
211 */ | |
212 protected synchronized void createBusyThread() { | |
213 if (busyThread !is null) | |
214 return; | |
215 | |
216 Rectangle bounds = getImage(0).getBounds(); | |
217 Display display = getDisplay(); | |
218 Image offScreenImage = new Image(display, bounds.width, bounds.height); | |
219 GC offScreenImageGC = new GC(offScreenImage); | |
220 busyThread = new BusyThread(bounds, display, offScreenImageGC, offScreenImage); | |
76 | 221 busyThread.priority((Thread.PRIORITY_MIN + Thread.PRIORITY_MAX )/2 + 2); |
222 busyThread.isDaemon(true); | |
75 | 223 busyThread.start(); |
224 } | |
225 | |
226 public void dispose() { | |
227 if (busyThread !is null) { | |
228 busyThread.setStop(true); | |
229 busyThread = null; | |
230 } | |
231 super.dispose(); | |
232 } | |
233 | |
234 /** | |
235 * Return the image or <code>null</code>. | |
236 */ | |
237 public Image getImage() { | |
238 return image; | |
239 } | |
240 | |
241 /** | |
242 * Returns true if it is currently busy. | |
243 * | |
244 * @return bool | |
245 */ | |
246 public bool isBusy() { | |
247 return (busyThread !is null); | |
248 } | |
249 | |
250 /* | |
251 * Process the paint event | |
252 */ | |
253 protected void onPaint(PaintEvent event) { | |
254 if (animationImage !is null && animationImage.isDisposed()) { | |
255 animationImage = null; | |
256 } | |
257 Rectangle rect = getClientArea(); | |
258 if (rect.width is 0 || rect.height is 0) | |
259 return; | |
260 | |
261 GC gc = event.gc; | |
262 Image activeImage = animationImage !is null ? animationImage : image; | |
263 if (activeImage !is null) { | |
264 Rectangle ibounds = activeImage.getBounds(); | |
265 gc.drawImage(activeImage, rect.width / 2 - ibounds.width / 2, | |
266 rect.height / 2 - ibounds.height / 2); | |
267 } | |
268 } | |
269 | |
270 /** | |
271 * Sets the indicators busy count up (true) or down (false) one. | |
272 * | |
273 * @param busy | |
274 * bool | |
275 */ | |
276 public synchronized void setBusy(bool busy) { | |
277 if (busy) { | |
278 if (busyThread is null) | |
279 createBusyThread(); | |
280 } else { | |
281 if (busyThread !is null) { | |
282 busyThread.setStop(true); | |
283 busyThread = null; | |
284 } | |
285 } | |
286 } | |
287 | |
288 /** | |
289 * Set the image. The value <code>null</code> clears it. | |
290 */ | |
291 public void setImage(Image image) { | |
292 if (image !is this.image && !isDisposed()) { | |
293 this.image = image; | |
294 redraw(); | |
295 } | |
296 } | |
297 | |
298 | |
299 private ImageDescriptor createImageDescriptor(String relativePath) { | |
300 //DWT_TODO | |
301 // Bundle bundle = Platform.getBundle("dwtx.ui.forms"); //$NON-NLS-1$ | |
302 // URL url = FileLocator.find(bundle, new Path(relativePath),null); | |
303 // if (url is null) return null; | |
304 // try { | |
305 // url = FileLocator.resolve(url); | |
306 // return ImageDescriptor.createFromURL(url); | |
307 // } catch (IOException e) { | |
308 // return null; | |
309 // } | |
310 return null; | |
311 } | |
312 | |
313 private synchronized Image getImage(int index) { | |
314 if (imageCache is null) { | |
315 imageCache = new Image[IMAGE_COUNT]; | |
316 } | |
317 if (imageCache[index] is null){ | |
318 ImageDescriptor descriptor = createImageDescriptor("$nl$/icons/progress/ani/" ~ to!(String)(index + 1) ~ ".png"); //$NON-NLS-1$ //$NON-NLS-2$ | |
319 imageCache[index] = descriptor.createImage(); | |
320 } | |
321 return imageCache[index]; | |
322 } | |
323 | |
324 private synchronized void clearImages() { | |
325 if (busyThread !is null) | |
326 return; | |
327 if (imageCache !is null) { | |
328 for (int index = 0; index < IMAGE_COUNT; index++) { | |
329 if (imageCache[index] !is null && !imageCache[index].isDisposed()) { | |
330 imageCache[index].dispose(); | |
331 imageCache[index] = null; | |
332 } | |
333 } | |
334 } | |
335 } | |
336 | |
337 } |