1 /*******************************************************************************
2  * Copyright (c) 2000, 2017 IBM Corporation and others.
3  *
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/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     IBM Corporation - initial API and implementation
13  *******************************************************************************/
14 package org.eclipse.swt.custom;
15 
16 import org.eclipse.swt.*;
17 import org.eclipse.swt.events.*;
18 import org.eclipse.swt.graphics.*;
19 import org.eclipse.swt.widgets.*;
20 
21 /**
22  * A control for showing progress feedback for a long running operation.
23  *
24  * @deprecated As of Eclipse 2.1, use ProgressBar with the style SWT.INDETERMINATE
25  *
26  * <dl>
27  * <dt><b>Styles:</b><dd>VERTICAL, HORIZONTAL, BORDER
28  * </dl>
29  *
30  * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
31  */
32 @Deprecated
33 public class AnimatedProgress extends Canvas {
34 
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;
40 	int value;
41 	int orientation = SWT.HORIZONTAL;
42 	boolean showBorder = false;
43 
44 /**
45  * Constructs a new instance of this class given its parent
46  * and a style value describing its behavior and appearance.
47  * <p>
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.
55  * </p>
56  *
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
59  *
60  * @exception IllegalArgumentException <ul>
61  *    <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
62  * </ul>
63  * @exception SWTException <ul>
64  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
65  * </ul>
66  *
67  * @see SWT#VERTICAL
68  * @see SWT#HORIZONTAL
69  * @see SWT#BORDER
70  * @see #getStyle()
71  */
AnimatedProgress(Composite parent, int style)72 public AnimatedProgress(Composite parent, int style) {
73 	super(parent, checkStyle(style));
74 
75 	if ((style & SWT.VERTICAL) != 0) {
76 		orientation = SWT.VERTICAL;
77 	}
78 	showBorder = (style & SWT.BORDER) != 0;
79 
80 	addControlListener(ControlListener.controlResizedAdapter(e -> redraw()));
81 	addPaintListener(this::paint);
82 	addDisposeListener(e -> stop());
83 }
checkStyle(int style)84 private static int checkStyle (int style) {
85 	int mask = SWT.NONE;
86 	return style & mask;
87 }
88 /**
89  * Stop the animation if it is not already stopped and
90  * reset the presentation to a blank appearance.
91  *
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>
95  * </ul>
96  */
clear()97 public synchronized void clear(){
98 	checkWidget();
99 	if (active) stop();
100 	showStripes = false;
101 	redraw();
102 }
103 @Override
computeSize(int wHint, int hHint, boolean changed)104 public Point computeSize(int wHint, int hHint, boolean changed) {
105 	checkWidget();
106 	Point size = null;
107 	if (orientation == SWT.HORIZONTAL) {
108 		size = new Point(DEFAULT_WIDTH, DEFAULT_HEIGHT);
109 	} else {
110 		size = new Point(DEFAULT_HEIGHT, DEFAULT_WIDTH);
111 	}
112 	if (wHint != SWT.DEFAULT) size.x = wHint;
113 	if (hHint != SWT.DEFAULT) size.y = hHint;
114 
115 	return size;
116 }
drawBevelRect(GC gc, int x, int y, int w, int h, Color topleft, Color bottomright)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);
121 
122 	gc.setForeground(bottomright);
123 	gc.drawLine(x+w, y, x+w, y+h);
124 	gc.drawLine(x, y+h, x+w, y+h);
125 }
paint(PaintEvent event)126 void paint(PaintEvent event) {
127 	GC gc = event.gc;
128 	Display disp= getDisplay();
129 
130 	Rectangle rect= getClientArea();
131 	gc.fillRectangle(rect);
132 	if (showBorder) {
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));
136 	}
137 
138 	paintStripes(gc);
139 }
paintStripes(GC gc)140 void paintStripes(GC gc) {
141 
142 	if (!showStripes) return;
143 
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);
147 
148 	gc.setLineWidth(2);
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());
154 	int step = 12;
155 	int foregroundValue = value == 0 ? step - 2 : value - 2;
156 	if (orientation == SWT.HORIZONTAL) {
157 		int y = rect.y - 1;
158 		int w = rect.width;
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);
163 		}
164 	} else {
165 		int x = rect.x - 1;
166 		int w = rect.width + 2;
167 		int h = rect.height;
168 
169 		for (int i= 0; i < h; i+= step) {
170 			int y = i + foregroundValue;
171 			gc.drawLine(x, y, w, y);
172 		}
173 	}
174 
175 	if (active) {
176 		value = (value + 2) % step;
177 	}
178 }
179 /**
180 * Start the animation.
181 *
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>
185 * </ul>
186 */
start()187 public synchronized void start() {
188 	checkWidget();
189 	if (active) return;
190 
191 	active = true;
192 	showStripes = true;
193 
194 	final Display display = getDisplay();
195 	final Runnable [] timer = new Runnable [1];
196 	timer [0] = () -> {
197 		if (!active) return;
198 		GC gc = new GC(AnimatedProgress.this);
199 		paintStripes(gc);
200 		gc.dispose();
201 		display.timerExec (SLEEP, timer [0]);
202 	};
203 	display.timerExec (SLEEP, timer [0]);
204 }
205 /**
206 * Stop the animation.   Freeze the presentation at its current appearance.
207 */
stop()208 public synchronized void stop() {
209 	//checkWidget();
210 	active = false;
211 }
212 }
213