KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > browser > BusyIndicator


1 /*******************************************************************************
2  * Copyright (c) 2003, 2005 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  *******************************************************************************/

11 package org.eclipse.ui.internal.browser;
12
13 import org.eclipse.swt.events.PaintEvent;
14 import org.eclipse.swt.events.PaintListener;
15 import org.eclipse.swt.graphics.GC;
16 import org.eclipse.swt.graphics.Image;
17 import org.eclipse.swt.graphics.Point;
18 import org.eclipse.swt.graphics.Rectangle;
19 import org.eclipse.swt.widgets.Canvas;
20 import org.eclipse.swt.widgets.Composite;
21 import org.eclipse.swt.widgets.Display;
22 /**
23  * An animated image to show busy status of the Web browser.
24  */

25 public class BusyIndicator extends Canvas {
26     protected Image[] images;
27     protected Image image;
28
29     protected Thread JavaDoc busyThread;
30     protected boolean stop;
31
32     /**
33      * BusyWidget constructor comment.
34      * @param parent org.eclipse.swt.widgets.Composite
35      * @param style int
36      */

37     public BusyIndicator(Composite parent, int style) {
38         super(parent, style);
39     
40         images = ImageResource.getBusyImages();
41     
42         addPaintListener(new PaintListener() {
43             public void paintControl(PaintEvent event) {
44                 onPaint(event);
45             }
46         });
47     
48         image = images[0];
49     }
50     
51     public Point computeSize(int wHint, int hHint, boolean changed) {
52         return new Point(25, 25);
53     }
54     
55     /**
56      * Creates a thread to animate the image.
57      */

58     protected synchronized void createBusyThread() {
59         if (busyThread != null)
60             return;
61     
62         stop = false;
63         busyThread = new Thread JavaDoc() {
64             protected int count;
65             public void run() {
66                 try {
67                     count = 1;
68                     while (!stop) {
69                         Display.getDefault().syncExec(new Runnable JavaDoc() {
70                             public void run() {
71                                 if (!stop) {
72                                     if (count < 13)
73                                         setImage(images[count]);
74                                     count++;
75                                     if (count > 12)
76                                         count = 1;
77                                 }
78                             }
79                         });
80                         try {
81                             sleep(125);
82                         } catch (Exception JavaDoc e) {
83                             // ignore
84
}
85                     }
86                     if (busyThread == null)
87                         Display.getDefault().syncExec(new Thread JavaDoc() {
88                             public void run() {
89                                 setImage(images[0]);
90                             }
91                         });
92                 } catch (Exception JavaDoc e) {
93                     Trace.trace(Trace.WARNING, "Busy error", e); //$NON-NLS-1$
94
}
95             }
96         };
97     
98         busyThread.setPriority(Thread.NORM_PRIORITY + 2);
99         busyThread.setDaemon(true);
100         busyThread.start();
101     }
102     
103     public void dispose() {
104         stop = true;
105         busyThread = null;
106         super.dispose();
107     }
108     
109     /**
110      * Return the image or <code>null</code>.
111      */

112     public Image getImage() {
113         return image;
114     }
115
116     /**
117      * Returns true if it is currently busy.
118      *
119      * @return boolean
120      */

121     public boolean isBusy() {
122         return (busyThread != null);
123     }
124
125     /*
126      * Process the paint event
127      */

128     protected void onPaint(PaintEvent event) {
129         Rectangle rect = getClientArea();
130         if (rect.width == 0 || rect.height == 0)
131             return;
132     
133         GC gc = event.gc;
134         if (image != null)
135             gc.drawImage(image, 2, 2);
136     }
137
138     /**
139      * Sets the indicators busy count up (true) or down (false) one.
140      *
141      * @param busy boolean
142      */

143     public synchronized void setBusy(boolean busy) {
144         if (busy) {
145             if (busyThread == null)
146                 createBusyThread();
147         } else {
148             if (busyThread != null) {
149                 stop = true;
150                 busyThread = null;
151             }
152         }
153     }
154
155     /**
156      * Set the image.
157      * The value <code>null</code> clears it.
158      */

159     public void setImage(Image image) {
160         if (image != this.image && !isDisposed()) {
161             this.image = image;
162             redraw();
163         }
164     }
165 }
Popular Tags