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