]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.tests.modelled.ui/src/org/simantics/tests/modelled/ui/STSProgressBar.java
Merge commit '0580ea8b675c868685993b0780c9ecc31010f681' into master
[simantics/platform.git] / bundles / org.simantics.tests.modelled.ui / src / org / simantics / tests / modelled / ui / STSProgressBar.java
1 /*******************************************************************************\r
2  * Copyright (c) 2000, 2010 IBM Corporation and others.\r
3  * All rights reserved. This program and the accompanying materials\r
4  * are made available under the terms of the Eclipse Public License v1.0\r
5  * which accompanies this distribution, and is available at\r
6  * http://www.eclipse.org/legal/epl-v10.html\r
7  *\r
8  * Contributors:\r
9  *     IBM Corporation - initial API and implementation\r
10  *     Stephan Michels, stephan@apache.org - 104944 [JUnit] Unnecessary code in JUnitProgressBar\r
11  *******************************************************************************/\r
12 package org.simantics.tests.modelled.ui;\r
13 \r
14 import org.eclipse.swt.SWT;\r
15 import org.eclipse.swt.events.ControlAdapter;\r
16 import org.eclipse.swt.events.ControlEvent;\r
17 import org.eclipse.swt.events.DisposeEvent;\r
18 import org.eclipse.swt.events.DisposeListener;\r
19 import org.eclipse.swt.events.PaintEvent;\r
20 import org.eclipse.swt.events.PaintListener;\r
21 import org.eclipse.swt.graphics.Color;\r
22 import org.eclipse.swt.graphics.GC;\r
23 import org.eclipse.swt.graphics.Point;\r
24 import org.eclipse.swt.graphics.Rectangle;\r
25 import org.eclipse.swt.widgets.Canvas;\r
26 import org.eclipse.swt.widgets.Composite;\r
27 import org.eclipse.swt.widgets.Display;\r
28 \r
29 /**\r
30  * A progress bar with a red/green indication for success or failure.\r
31  */\r
32 public class STSProgressBar extends Canvas {\r
33     private static final int DEFAULT_WIDTH = 160;\r
34     private static final int DEFAULT_HEIGHT = 18;\r
35 \r
36     private int fCurrentTickCount= 0;\r
37     private int fMaxTickCount= 0;\r
38     private int fColorBarWidth= 0;\r
39     private Color fOKColor;\r
40     private Color fFailureColor;\r
41     private Color fStoppedColor;\r
42     private boolean fError;\r
43     private boolean fStopped= false;\r
44 \r
45     public STSProgressBar(Composite parent) {\r
46         super(parent, SWT.NONE);\r
47 \r
48         addControlListener(new ControlAdapter() {\r
49             @Override\r
50             public void controlResized(ControlEvent e) {\r
51                 fColorBarWidth= scale(fCurrentTickCount);\r
52                 redraw();\r
53             }\r
54         });\r
55         addPaintListener(new PaintListener() {\r
56             public void paintControl(PaintEvent e) {\r
57                 paint(e);\r
58             }\r
59         });\r
60         addDisposeListener(new DisposeListener() {\r
61             public void widgetDisposed(DisposeEvent e) {\r
62                 fFailureColor.dispose();\r
63                 fOKColor.dispose();\r
64                 fStoppedColor.dispose();\r
65             }\r
66         });\r
67         Display display= parent.getDisplay();\r
68         fFailureColor= new Color(display, 159, 63, 63);\r
69         fOKColor= new Color(display, 95, 191, 95);\r
70         fStoppedColor= new Color(display, 120, 120, 120);\r
71     }\r
72 \r
73     public void setMaximum(int max) {\r
74         fMaxTickCount= max;\r
75     }\r
76 \r
77     public void reset() {\r
78         fError= false;\r
79         fStopped= false;\r
80         fCurrentTickCount= 0;\r
81         fMaxTickCount= 0;\r
82         fColorBarWidth= 0;\r
83         redraw();\r
84     }\r
85 \r
86     public void reset(boolean hasErrors, boolean stopped, int ticksDone, int maximum) {\r
87         boolean noChange= fError == hasErrors && fStopped == stopped && fCurrentTickCount == ticksDone && fMaxTickCount == maximum;\r
88         fError= hasErrors;\r
89         fStopped= stopped;\r
90         fCurrentTickCount= ticksDone;\r
91         fMaxTickCount= maximum;\r
92         fColorBarWidth= scale(ticksDone);\r
93         if (! noChange)\r
94             redraw();\r
95     }\r
96 \r
97     private void paintStep(int startX, int endX) {\r
98         GC gc = new GC(this);\r
99         setStatusColor(gc);\r
100         Rectangle rect= getClientArea();\r
101         startX= Math.max(1, startX);\r
102         gc.fillRectangle(startX, 1, endX-startX, rect.height-2);\r
103         gc.dispose();\r
104     }\r
105 \r
106     private void setStatusColor(GC gc) {\r
107         if (fStopped)\r
108             gc.setBackground(fStoppedColor);\r
109         else if (fError)\r
110             gc.setBackground(fFailureColor);\r
111         else\r
112             gc.setBackground(fOKColor);\r
113     }\r
114 \r
115     public void stopped() {\r
116         fStopped= true;\r
117         redraw();\r
118     }\r
119 \r
120     private int scale(int value) {\r
121         if (fMaxTickCount > 0) {\r
122             Rectangle r= getClientArea();\r
123             if (r.width != 0)\r
124                 return Math.max(0, value*(r.width-2)/fMaxTickCount);\r
125         }\r
126         return value;\r
127     }\r
128 \r
129     private void drawBevelRect(GC gc, int x, int y, int w, int h, Color topleft, Color bottomright) {\r
130         gc.setForeground(topleft);\r
131         gc.drawLine(x, y, x+w-1, y);\r
132         gc.drawLine(x, y, x, y+h-1);\r
133 \r
134         gc.setForeground(bottomright);\r
135         gc.drawLine(x+w, y, x+w, y+h);\r
136         gc.drawLine(x, y+h, x+w, y+h);\r
137     }\r
138 \r
139     private void paint(PaintEvent event) {\r
140         GC gc = event.gc;\r
141         Display disp= getDisplay();\r
142 \r
143         Rectangle rect= getClientArea();\r
144         gc.fillRectangle(rect);\r
145         drawBevelRect(gc, rect.x, rect.y, rect.width-1, rect.height-1,\r
146             disp.getSystemColor(SWT.COLOR_WIDGET_NORMAL_SHADOW),\r
147             disp.getSystemColor(SWT.COLOR_WIDGET_HIGHLIGHT_SHADOW));\r
148 \r
149         setStatusColor(gc);\r
150         fColorBarWidth= Math.min(rect.width-2, fColorBarWidth);\r
151         gc.fillRectangle(1, 1, fColorBarWidth, rect.height-2);\r
152     }\r
153 \r
154     @Override\r
155     public Point computeSize(int wHint, int hHint, boolean changed) {\r
156         checkWidget();\r
157         Point size= new Point(DEFAULT_WIDTH, DEFAULT_HEIGHT);\r
158         if (wHint != SWT.DEFAULT) size.x= wHint;\r
159         if (hHint != SWT.DEFAULT) size.y= hHint;\r
160         return size;\r
161     }\r
162 \r
163     public void step(int failures) {\r
164         fCurrentTickCount++;\r
165         int x= fColorBarWidth;\r
166 \r
167         fColorBarWidth= scale(fCurrentTickCount);\r
168 \r
169         if (!fError && failures > 0) {\r
170             fError= true;\r
171             x= 1;\r
172         }\r
173         if (fCurrentTickCount == fMaxTickCount)\r
174             fColorBarWidth= getClientArea().width-1;\r
175         paintStep(x, fColorBarWidth);\r
176     }\r
177 \r
178     public void refresh(boolean hasErrors) {\r
179         fError= hasErrors;\r
180         redraw();\r
181     }\r
182 \r
183 }\r