Documentation update from hudson build on Sat 29 Mar 2008 02:57:09 PM CDT
[phpeclipse.git] / net.sourceforge.phpeclipse.webbrowser / src / net / sourceforge / phpeclipse / webbrowser / internal / BusyIndicator.java
1 /**
2  * Copyright (c) 2003 IBM Corporation and others.
3  * All rights reserved. � This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  �*
8  * Contributors:
9  *    IBM - Initial API and implementation
10  */
11 package net.sourceforge.phpeclipse.webbrowser.internal;
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 /**
24  * An animated image to show busy status of the Web browser.
25  */
26 public class BusyIndicator extends Canvas {
27         protected Image[] images;
28
29         protected Image image;
30
31         protected Thread busyThread;
32
33         protected boolean stop;
34
35         /**
36          * BusyWidget constructor comment.
37          * 
38          * @param parent
39          *            org.eclipse.swt.widgets.Composite
40          * @param style
41          *            int
42          */
43         public BusyIndicator(Composite parent, int style) {
44                 super(parent, style);
45
46                 images = ImageResource.getBusyImages();
47
48                 addPaintListener(new PaintListener() {
49                         public void paintControl(PaintEvent event) {
50                                 onPaint(event);
51                         }
52                 });
53
54                 image = images[0];
55         }
56
57         public Point computeSize(int wHint, int hHint, boolean changed) {
58                 return new Point(25, 25);
59         }
60
61         /**
62          * Creates a thread to animate the image.
63          */
64         protected synchronized void createBusyThread() {
65                 if (busyThread != null)
66                         return;
67
68                 stop = false;
69                 busyThread = new Thread() {
70                         protected int count;
71
72                         public void run() {
73                                 try {
74                                         count = 1;
75                                         while (!stop) {
76                                                 Display.getDefault().syncExec(new Runnable() {
77                                                         public void run() {
78                                                                 if (!stop) {
79                                                                         if (count < 13)
80                                                                                 setImage(images[count]);
81                                                                         count++;
82                                                                         if (count > 12)
83                                                                                 count = 1;
84                                                                 }
85                                                         }
86                                                 });
87                                                 try {
88                                                         sleep(125);
89                                                 } catch (Exception e) {
90                                                 }
91                                         }
92                                         if (busyThread == null)
93                                                 Display.getDefault().syncExec(new Thread() {
94                                                         public void run() {
95                                                                 setImage(images[0]);
96                                                         }
97                                                 });
98                                 } catch (Exception e) {
99                                         Trace.trace(Trace.WARNING, "Busy error", e);
100                                 }
101                         }
102                 };
103
104                 busyThread.setPriority(Thread.NORM_PRIORITY + 2);
105                 busyThread.setDaemon(true);
106                 busyThread.start();
107         }
108
109         public void dispose() {
110                 stop = true;
111                 busyThread = null;
112                 super.dispose();
113         }
114
115         /**
116          * Return the image or <code>null</code>.
117          */
118         public Image getImage() {
119                 return image;
120         }
121
122         /**
123          * Returns true if it is currently busy.
124          * 
125          * @return boolean
126          */
127         public boolean isBusy() {
128                 return (busyThread != null);
129         }
130
131         /*
132          * Process the paint event
133          */
134         protected void onPaint(PaintEvent event) {
135                 Rectangle rect = getClientArea();
136                 if (rect.width == 0 || rect.height == 0)
137                         return;
138
139                 GC gc = event.gc;
140                 if (image != null)
141                         gc.drawImage(image, 2, 2);
142         }
143
144         /**
145          * Sets the indicators busy count up (true) or down (false) one.
146          * 
147          * @param busy
148          *            boolean
149          */
150         public synchronized void setBusy(boolean busy) {
151                 if (busy) {
152                         if (busyThread == null)
153                                 createBusyThread();
154                 } else {
155                         if (busyThread != null) {
156                                 stop = true;
157                                 busyThread = null;
158                         }
159                 }
160         }
161
162         /**
163          * Set the image. The value <code>null</code> clears it.
164          */
165         public void setImage(Image image) {
166                 if (image != this.image && !isDisposed()) {
167                         this.image = image;
168                         redraw();
169                 }
170         }
171 }