unification of bug fixes 1198893, 1404228
[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  * 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 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() {
64                         protected int count;
65                         public void run() {
66                                 try {
67                                         count = 1;
68                                         while (!stop) {
69                                                 Display.getDefault().syncExec(new Runnable() {
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 e) { }
83                                         }
84                                         if (busyThread == null)
85                                                 Display.getDefault().syncExec(new Thread() {
86                                                         public void run() {
87                                                                 setImage(images[0]);
88                                                         }
89                                                 });
90                                 } catch (Exception e) {
91                                         Trace.trace(Trace.WARNING, "Busy error", e);
92                                 }
93                         }
94                 };
95         
96                 busyThread.setPriority(Thread.NORM_PRIORITY + 2);
97                 busyThread.setDaemon(true);
98                 busyThread.start();
99         }
100         
101         public void dispose() {
102                 stop = true;
103                 busyThread = null;
104                 super.dispose();
105         }
106         
107         /**
108          * Return the image or <code>null</code>.
109          */
110         public Image getImage() {
111                 return image;
112         }
113
114         /**
115          * Returns true if it is currently busy.
116          *
117          * @return boolean
118          */
119         public boolean isBusy() {
120                 return (busyThread != null);
121         }
122
123         /* 
124          * Process the paint event
125          */
126         protected void onPaint(PaintEvent event) {
127                 Rectangle rect = getClientArea();
128                 if (rect.width == 0 || rect.height == 0)
129                         return;
130         
131                 GC gc = event.gc;
132                 if (image != null)
133                         gc.drawImage(image, 2, 2);
134         }
135
136         /**
137          * Sets the indicators busy count up (true) or down (false) one.
138          *
139          * @param busy boolean
140          */
141         public synchronized void setBusy(boolean busy) {
142                 if (busy) {
143                         if (busyThread == null)
144                                 createBusyThread();
145                 } else {
146                         if (busyThread != null) {
147                                 stop = true;
148                                 busyThread = null;
149                         }
150                 }
151         }
152
153         /**
154          * Set the image.
155          * The value <code>null</code> clears it.
156          */
157         public void setImage(Image image) {
158                 if (image != this.image && !isDisposed()) {
159                         this.image = image;
160                         redraw();
161                 }
162         }
163 }