1 /*******************************************************************************
2 * Copyright (c) 2000, 2017 IBM Corporation and others.
4 * This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License 2.0
6 * which accompanies this distribution, and is available at
7 * https://www.eclipse.org/legal/epl-2.0/
9 * SPDX-License-Identifier: EPL-2.0
12 * IBM Corporation - initial API and implementation
13 *******************************************************************************/
14 package org.eclipse.swt.custom;
16 import org.eclipse.swt.*;
17 import org.eclipse.swt.events.*;
18 import org.eclipse.swt.graphics.*;
19 import org.eclipse.swt.widgets.*;
22 * A control for showing progress feedback for a long running operation.
24 * @deprecated As of Eclipse 2.1, use ProgressBar with the style SWT.INDETERMINATE
27 * <dt><b>Styles:</b><dd>VERTICAL, HORIZONTAL, BORDER
30 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
33 public class AnimatedProgress extends Canvas {
35 static final int SLEEP = 70;
36 static final int DEFAULT_WIDTH = 160;
37 static final int DEFAULT_HEIGHT = 18;
38 boolean active = false;
39 boolean showStripes = false;
41 int orientation = SWT.HORIZONTAL;
42 boolean showBorder = false;
45 * Constructs a new instance of this class given its parent
46 * and a style value describing its behavior and appearance.
48 * The style value is either one of the style constants defined in
49 * class <code>SWT</code> which is applicable to instances of this
50 * class, or must be built by <em>bitwise OR</em>'ing together
51 * (that is, using the <code>int</code> "|" operator) two or more
52 * of those <code>SWT</code> style constants. The class description
53 * lists the style constants that are applicable to the class.
54 * Style bits are also inherited from superclasses.
57 * @param parent a widget which will be the parent of the new instance (cannot be null)
58 * @param style the style of widget to construct
60 * @exception IllegalArgumentException <ul>
61 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
63 * @exception SWTException <ul>
64 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
72 public AnimatedProgress(Composite parent, int style) {
73 super(parent, checkStyle(style));
75 if ((style & SWT.VERTICAL) != 0) {
76 orientation = SWT.VERTICAL;
78 showBorder = (style & SWT.BORDER) != 0;
80 addControlListener(ControlListener.controlResizedAdapter(e -> redraw()));
81 addPaintListener(e -> paint(e));
82 addDisposeListener(e -> stop());
84 private static int checkStyle (int style) {
89 * Stop the animation if it is not already stopped and
90 * reset the presentation to a blank appearance.
92 * @exception SWTException <ul>
93 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
94 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
97 public synchronized void clear(){
104 public Point computeSize(int wHint, int hHint, boolean changed) {
107 if (orientation == SWT.HORIZONTAL) {
108 size = new Point(DEFAULT_WIDTH, DEFAULT_HEIGHT);
110 size = new Point(DEFAULT_HEIGHT, DEFAULT_WIDTH);
112 if (wHint != SWT.DEFAULT) size.x = wHint;
113 if (hHint != SWT.DEFAULT) size.y = hHint;
117 private void drawBevelRect(GC gc, int x, int y, int w, int h, Color topleft, Color bottomright) {
118 gc.setForeground(topleft);
119 gc.drawLine(x, y, x+w-1, y);
120 gc.drawLine(x, y, x, y+h-1);
122 gc.setForeground(bottomright);
123 gc.drawLine(x+w, y, x+w, y+h);
124 gc.drawLine(x, y+h, x+w, y+h);
126 void paint(PaintEvent event) {
128 Display disp= getDisplay();
130 Rectangle rect= getClientArea();
131 gc.fillRectangle(rect);
133 drawBevelRect(gc, rect.x, rect.y, rect.width-1, rect.height-1,
134 disp.getSystemColor(SWT.COLOR_WIDGET_NORMAL_SHADOW),
135 disp.getSystemColor(SWT.COLOR_WIDGET_HIGHLIGHT_SHADOW));
140 void paintStripes(GC gc) {
142 if (!showStripes) return;
144 Rectangle rect= getClientArea();
145 // Subtracted border painted by paint.
146 rect = new Rectangle(rect.x+2, rect.y+2, rect.width-4, rect.height-4);
149 gc.setClipping(rect);
150 Color color = getDisplay().getSystemColor(SWT.COLOR_LIST_SELECTION);
151 gc.setBackground(color);
152 gc.fillRectangle(rect);
153 gc.setForeground(this.getBackground());
155 int foregroundValue = value == 0 ? step - 2 : value - 2;
156 if (orientation == SWT.HORIZONTAL) {
159 int h = rect.height + 2;
160 for (int i= 0; i < w; i+= step) {
161 int x = i + foregroundValue;
162 gc.drawLine(x, y, x, h);
166 int w = rect.width + 2;
169 for (int i= 0; i < h; i+= step) {
170 int y = i + foregroundValue;
171 gc.drawLine(x, y, w, y);
176 value = (value + 2) % step;
180 * Start the animation.
182 * @exception SWTException <ul>
183 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
184 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
187 public synchronized void start() {
194 final Display display = getDisplay();
195 final Runnable [] timer = new Runnable [1];
198 GC gc = new GC(AnimatedProgress.this);
201 display.timerExec (SLEEP, timer [0]);
203 display.timerExec (SLEEP, timer [0]);
206 * Stop the animation. Freeze the presentation at its current appearance.
208 public synchronized void stop() {