Improved error "Unreachable code" for if statements
[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.widgets.*;
14 import org.eclipse.swt.events.*;
15 import org.eclipse.swt.graphics.*;
16 /**
17  * An animated image to show busy status of the Web browser.
18  */
19 public class BusyIndicator extends Canvas {
20         protected Image[] images;
21         protected Image image;
22
23         protected Thread busyThread;
24         protected boolean stop;
25
26         /**
27          * BusyWidget constructor comment.
28          * @param parent org.eclipse.swt.widgets.Composite
29          * @param style int
30          */
31         public BusyIndicator(Composite parent, int style) {
32                 super(parent, style);
33         
34                 images = ImageResource.getBusyImages();
35         
36                 addPaintListener(new PaintListener() {
37                         public void paintControl(PaintEvent event) {
38                                 onPaint(event);
39                         }
40                 });
41         
42                 image = images[0];
43         }
44         
45         public Point computeSize(int wHint, int hHint, boolean changed) {
46                 return new Point(25, 25);
47         }
48         
49         /**
50          * Creates a thread to animate the image.
51          */
52         protected synchronized void createBusyThread() {
53                 if (busyThread != null)
54                         return;
55         
56                 stop = false;
57                 busyThread = new Thread() {
58                         protected int count;
59                         public void run() {
60                                 try {
61                                         count = 1;
62                                         while (!stop) {
63                                                 Display.getDefault().syncExec(new Runnable() {
64                                                         public void run() {
65                                                                 if (!stop) {
66                                                                         if (count < 13)
67                                                                                 setImage(images[count]);
68                                                                         count++;
69                                                                         if (count > 12)
70                                                                                 count = 1;
71                                                                 }
72                                                         }
73                                                 });
74                                                 try {
75                                                         sleep(125);
76                                                 } catch (Exception e) { }
77                                         }
78                                         if (busyThread == null)
79                                                 Display.getDefault().syncExec(new Thread() {
80                                                         public void run() {
81                                                                 setImage(images[0]);
82                                                         }
83                                                 });
84                                 } catch (Exception e) {
85                                         Trace.trace(Trace.WARNING, "Busy error", e);
86                                 }
87                         }
88                 };
89         
90                 busyThread.setPriority(Thread.NORM_PRIORITY + 2);
91                 busyThread.setDaemon(true);
92                 busyThread.start();
93         }
94         
95         public void dispose() {
96                 stop = true;
97                 busyThread = null;
98                 super.dispose();
99         }
100         
101         /**
102          * Return the image or <code>null</code>.
103          */
104         public Image getImage() {
105                 return image;
106         }
107
108         /**
109          * Returns true if it is currently busy.
110          *
111          * @return boolean
112          */
113         public boolean isBusy() {
114                 return (busyThread != null);
115         }
116
117         /* 
118          * Process the paint event
119          */
120         protected void onPaint(PaintEvent event) {
121                 Rectangle rect = getClientArea();
122                 if (rect.width == 0 || rect.height == 0)
123                         return;
124         
125                 GC gc = event.gc;
126                 if (image != null)
127                         gc.drawImage(image, 2, 2);
128         }
129
130         /**
131          * Sets the indicators busy count up (true) or down (false) one.
132          *
133          * @param busy boolean
134          */
135         public synchronized void setBusy(boolean busy) {
136                 if (busy) {
137                         if (busyThread == null)
138                                 createBusyThread();
139                 } else {
140                         if (busyThread != null) {
141                                 stop = true;
142                                 busyThread = null;
143                         }
144                 }
145         }
146
147         /**
148          * Set the image.
149          * The value <code>null</code> clears it.
150          */
151         public void setImage(Image image) {
152                 if (image != this.image && !isDisposed()) {
153                         this.image = image;
154                         redraw();
155                 }
156         }
157 }