KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > swt > custom > AnimatedProgress


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 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.swt.custom;
12
13 import org.eclipse.swt.*;
14 import org.eclipse.swt.graphics.*;
15 import org.eclipse.swt.widgets.*;
16 import org.eclipse.swt.events.*;
17
18 /**
19  * A control for showing progress feedback for a long running operation.
20  *
21  * @deprecated As of Eclipse 2.1, use ProgressBar with the style SWT.INDETERMINATE
22  *
23  * <dl>
24  * <dt><b>Styles:</b><dd>VERTICAL, HORIZONTAL, BORDER
25  * </dl>
26  */

27 public class AnimatedProgress extends Canvas {
28
29     static final int SLEEP = 70;
30     static final int DEFAULT_WIDTH = 160;
31     static final int DEFAULT_HEIGHT = 18;
32     boolean active = false;
33     boolean showStripes = false;
34     int value;
35     int orientation = SWT.HORIZONTAL;
36     boolean showBorder = false;
37
38 /**
39  * Constructs a new instance of this class given its parent
40  * and a style value describing its behavior and appearance.
41  * <p>
42  * The style value is either one of the style constants defined in
43  * class <code>SWT</code> which is applicable to instances of this
44  * class, or must be built by <em>bitwise OR</em>'ing together
45  * (that is, using the <code>int</code> "|" operator) two or more
46  * of those <code>SWT</code> style constants. The class description
47  * lists the style constants that are applicable to the class.
48  * Style bits are also inherited from superclasses.
49  * </p>
50  *
51  * @param parent a widget which will be the parent of the new instance (cannot be null)
52  * @param style the style of widget to construct
53  *
54  * @exception IllegalArgumentException <ul>
55  * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
56  * </ul>
57  * @exception SWTException <ul>
58  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
59  * </ul>
60  *
61  * @see SWT#VERTICAL
62  * @see SWT#HORIZONTAL
63  * @see SWT#BORDER
64  * @see #getStyle()
65  */

66 public AnimatedProgress(Composite parent, int style) {
67     super(parent, checkStyle(style));
68     
69     if ((style & SWT.VERTICAL) != 0) {
70         orientation = SWT.VERTICAL;
71     }
72     showBorder = (style & SWT.BORDER) != 0;
73     
74     addControlListener(new ControlAdapter() {
75         public void controlResized(ControlEvent e) {
76             redraw();
77         }
78     });
79     addPaintListener(new PaintListener() {
80         public void paintControl(PaintEvent e) {
81             paint(e);
82         }
83     });
84     addDisposeListener(new DisposeListener() {
85         public void widgetDisposed(DisposeEvent e){
86             stop();
87         }
88     });
89 }
90 private static int checkStyle (int style) {
91     int mask = SWT.NONE;
92     return style & mask;
93 }
94 /**
95  * Stop the animation if it is not already stopped and
96  * reset the presentation to a blank appearance.
97  *
98  * @exception SWTException <ul>
99  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
100  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
101  * </ul>
102  */

103 public synchronized void clear(){
104     checkWidget();
105     if (active) stop();
106     showStripes = false;
107     redraw();
108 }
109 public Point computeSize(int wHint, int hHint, boolean changed) {
110     checkWidget();
111     Point size = null;
112     if (orientation == SWT.HORIZONTAL) {
113         size = new Point(DEFAULT_WIDTH, DEFAULT_HEIGHT);
114     } else {
115         size = new Point(DEFAULT_HEIGHT, DEFAULT_WIDTH);
116     }
117     if (wHint != SWT.DEFAULT) size.x = wHint;
118     if (hHint != SWT.DEFAULT) size.y = hHint;
119     
120     return size;
121 }
122 private void drawBevelRect(GC gc, int x, int y, int w, int h, Color topleft, Color bottomright) {
123     gc.setForeground(topleft);
124     gc.drawLine(x, y, x+w-1, y);
125     gc.drawLine(x, y, x, y+h-1);
126         
127     gc.setForeground(bottomright);
128     gc.drawLine(x+w, y, x+w, y+h);
129     gc.drawLine(x, y+h, x+w, y+h);
130 }
131 void paint(PaintEvent event) {
132     GC gc = event.gc;
133     Display disp= getDisplay();
134             
135     Rectangle rect= getClientArea();
136     gc.fillRectangle(rect);
137     if (showBorder) {
138         drawBevelRect(gc, rect.x, rect.y, rect.width-1, rect.height-1,
139             disp.getSystemColor(SWT.COLOR_WIDGET_NORMAL_SHADOW),
140             disp.getSystemColor(SWT.COLOR_WIDGET_HIGHLIGHT_SHADOW));
141     }
142     
143     paintStripes(gc);
144 }
145 void paintStripes(GC gc) {
146     
147     if (!showStripes) return;
148     
149     Rectangle rect= getClientArea();
150     // Subtracted border painted by paint.
151
rect = new Rectangle(rect.x+2, rect.y+2, rect.width-4, rect.height-4);
152
153     gc.setLineWidth(2);
154     gc.setClipping(rect);
155     Color color = getDisplay().getSystemColor(SWT.COLOR_LIST_SELECTION);
156     gc.setBackground(color);
157     gc.fillRectangle(rect);
158     gc.setForeground(this.getBackground());
159     int step = 12;
160     int foregroundValue = value == 0 ? step - 2 : value - 2;
161     if (orientation == SWT.HORIZONTAL) {
162         int y = rect.y - 1;
163         int w = rect.width;
164         int h = rect.height + 2;
165         for (int i= 0; i < w; i+= step) {
166             int x = i + foregroundValue;
167             gc.drawLine(x, y, x, h);
168         }
169     } else {
170         int x = rect.x - 1;
171         int w = rect.width + 2;
172         int h = rect.height;
173
174         for (int i= 0; i < h; i+= step) {
175             int y = i + foregroundValue;
176             gc.drawLine(x, y, w, y);
177         }
178     }
179     
180     if (active) {
181         value = (value + 2) % step;
182     }
183 }
184 /**
185 * Start the animation.
186 *
187 * @exception SWTException <ul>
188 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
189 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
190 * </ul>
191 */

192 public synchronized void start() {
193     checkWidget();
194     if (active) return;
195
196     active = true;
197     showStripes = true;
198     
199     final Display display = getDisplay();
200     final Runnable JavaDoc [] timer = new Runnable JavaDoc [1];
201     timer [0] = new Runnable JavaDoc () {
202         public void run () {
203             if (!active) return;
204             GC gc = new GC(AnimatedProgress.this);
205             paintStripes(gc);
206             gc.dispose();
207             display.timerExec (SLEEP, timer [0]);
208         }
209     };
210     display.timerExec (SLEEP, timer [0]);
211 }
212 /**
213 * Stop the animation. Freeze the presentation at its current appearance.
214 */

215 public synchronized void stop() {
216     //checkWidget();
217
active = false;
218 }
219 }
220
Popular Tags