KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > junit > ui > JUnitProgressBar


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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  * Stephan Michels, stephan@apache.org - 104944 [JUnit] Unnecessary code in JUnitProgressBar
11  *******************************************************************************/

12 package org.eclipse.jdt.internal.junit.ui;
13
14 import org.eclipse.swt.SWT;
15 import org.eclipse.swt.events.ControlAdapter;
16 import org.eclipse.swt.events.ControlEvent;
17 import org.eclipse.swt.events.DisposeEvent;
18 import org.eclipse.swt.events.DisposeListener;
19 import org.eclipse.swt.events.PaintEvent;
20 import org.eclipse.swt.events.PaintListener;
21 import org.eclipse.swt.graphics.Color;
22 import org.eclipse.swt.graphics.GC;
23 import org.eclipse.swt.graphics.Point;
24 import org.eclipse.swt.graphics.Rectangle;
25 import org.eclipse.swt.widgets.Canvas;
26 import org.eclipse.swt.widgets.Composite;
27 import org.eclipse.swt.widgets.Display;
28
29 /**
30  * A progress bar with a red/green indication for success or failure.
31  */

32 public class JUnitProgressBar extends Canvas {
33     private static final int DEFAULT_WIDTH = 160;
34     private static final int DEFAULT_HEIGHT = 18;
35
36     private int fCurrentTickCount= 0;
37     private int fMaxTickCount= 0;
38     private int fColorBarWidth= 0;
39     private Color fOKColor;
40     private Color fFailureColor;
41     private Color fStoppedColor;
42     private boolean fError;
43     private boolean fStopped= false;
44     
45     public JUnitProgressBar(Composite parent) {
46         super(parent, SWT.NONE);
47         
48         addControlListener(new ControlAdapter() {
49             public void controlResized(ControlEvent e) {
50                 fColorBarWidth= scale(fCurrentTickCount);
51                 redraw();
52             }
53         });
54         addPaintListener(new PaintListener() {
55             public void paintControl(PaintEvent e) {
56                 paint(e);
57             }
58         });
59         addDisposeListener(new DisposeListener() {
60             public void widgetDisposed(DisposeEvent e) {
61                 fFailureColor.dispose();
62                 fOKColor.dispose();
63                 fStoppedColor.dispose();
64             }
65         });
66         Display display= parent.getDisplay();
67         fFailureColor= new Color(display, 159, 63, 63);
68         fOKColor= new Color(display, 95, 191, 95);
69         fStoppedColor= new Color(display, 120, 120, 120);
70     }
71
72     public void setMaximum(int max) {
73         fMaxTickCount= max;
74     }
75         
76     public void reset() {
77         fError= false;
78         fStopped= false;
79         fCurrentTickCount= 0;
80         fMaxTickCount= 0;
81         fColorBarWidth= 0;
82         redraw();
83     }
84     
85     public void reset(boolean hasErrors, boolean stopped, int ticksDone, int maximum) {
86         boolean noChange= fError == hasErrors && fStopped == stopped && fCurrentTickCount == ticksDone && fMaxTickCount == maximum;
87         fError= hasErrors;
88         fStopped= stopped;
89         fCurrentTickCount= ticksDone;
90         fMaxTickCount= maximum;
91         fColorBarWidth= scale(ticksDone);
92         if (! noChange)
93             redraw();
94     }
95     
96     private void paintStep(int startX, int endX) {
97         GC gc = new GC(this);
98         setStatusColor(gc);
99         Rectangle rect= getClientArea();
100         startX= Math.max(1, startX);
101         gc.fillRectangle(startX, 1, endX-startX, rect.height-2);
102         gc.dispose();
103     }
104
105     private void setStatusColor(GC gc) {
106         if (fStopped)
107             gc.setBackground(fStoppedColor);
108         else if (fError)
109             gc.setBackground(fFailureColor);
110         else
111             gc.setBackground(fOKColor);
112     }
113
114     public void stopped() {
115         fStopped= true;
116         redraw();
117     }
118     
119     private int scale(int value) {
120         if (fMaxTickCount > 0) {
121             Rectangle r= getClientArea();
122             if (r.width != 0)
123                 return Math.max(0, value*(r.width-2)/fMaxTickCount);
124         }
125         return value;
126     }
127     
128     private void drawBevelRect(GC gc, int x, int y, int w, int h, Color topleft, Color bottomright) {
129         gc.setForeground(topleft);
130         gc.drawLine(x, y, x+w-1, y);
131         gc.drawLine(x, y, x, y+h-1);
132         
133         gc.setForeground(bottomright);
134         gc.drawLine(x+w, y, x+w, y+h);
135         gc.drawLine(x, y+h, x+w, y+h);
136     }
137     
138     private void paint(PaintEvent event) {
139         GC gc = event.gc;
140         Display disp= getDisplay();
141             
142         Rectangle rect= getClientArea();
143         gc.fillRectangle(rect);
144         drawBevelRect(gc, rect.x, rect.y, rect.width-1, rect.height-1,
145             disp.getSystemColor(SWT.COLOR_WIDGET_NORMAL_SHADOW),
146             disp.getSystemColor(SWT.COLOR_WIDGET_HIGHLIGHT_SHADOW));
147         
148         setStatusColor(gc);
149         fColorBarWidth= Math.min(rect.width-2, fColorBarWidth);
150         gc.fillRectangle(1, 1, fColorBarWidth, rect.height-2);
151     }
152     
153     public Point computeSize(int wHint, int hHint, boolean changed) {
154         checkWidget();
155         Point size= new Point(DEFAULT_WIDTH, DEFAULT_HEIGHT);
156         if (wHint != SWT.DEFAULT) size.x= wHint;
157         if (hHint != SWT.DEFAULT) size.y= hHint;
158         return size;
159     }
160     
161     public void step(int failures) {
162         fCurrentTickCount++;
163         int x= fColorBarWidth;
164
165         fColorBarWidth= scale(fCurrentTickCount);
166
167         if (!fError && failures > 0) {
168             fError= true;
169             x= 1;
170         }
171         if (fCurrentTickCount == fMaxTickCount)
172             fColorBarWidth= getClientArea().width-1;
173         paintStep(x, fColorBarWidth);
174     }
175
176     public void refresh(boolean hasErrors) {
177         fError= hasErrors;
178         redraw();
179     }
180     
181 }
182
Popular Tags