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
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
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
30 * A progress bar with a red/green indication for success or failure.
\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
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
45 public STSProgressBar(Composite parent) {
\r
46 super(parent, SWT.NONE);
\r
48 addControlListener(new ControlAdapter() {
\r
50 public void controlResized(ControlEvent e) {
\r
51 fColorBarWidth= scale(fCurrentTickCount);
\r
55 addPaintListener(new PaintListener() {
\r
56 public void paintControl(PaintEvent e) {
\r
60 addDisposeListener(new DisposeListener() {
\r
61 public void widgetDisposed(DisposeEvent e) {
\r
62 fFailureColor.dispose();
\r
64 fStoppedColor.dispose();
\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
73 public void setMaximum(int max) {
\r
77 public void reset() {
\r
80 fCurrentTickCount= 0;
\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
90 fCurrentTickCount= ticksDone;
\r
91 fMaxTickCount= maximum;
\r
92 fColorBarWidth= scale(ticksDone);
\r
97 private void paintStep(int startX, int endX) {
\r
98 GC gc = new GC(this);
\r
100 Rectangle rect= getClientArea();
\r
101 startX= Math.max(1, startX);
\r
102 gc.fillRectangle(startX, 1, endX-startX, rect.height-2);
\r
106 private void setStatusColor(GC gc) {
\r
108 gc.setBackground(fStoppedColor);
\r
110 gc.setBackground(fFailureColor);
\r
112 gc.setBackground(fOKColor);
\r
115 public void stopped() {
\r
120 private int scale(int value) {
\r
121 if (fMaxTickCount > 0) {
\r
122 Rectangle r= getClientArea();
\r
124 return Math.max(0, value*(r.width-2)/fMaxTickCount);
\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
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
139 private void paint(PaintEvent event) {
\r
141 Display disp= getDisplay();
\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
149 setStatusColor(gc);
\r
150 fColorBarWidth= Math.min(rect.width-2, fColorBarWidth);
\r
151 gc.fillRectangle(1, 1, fColorBarWidth, rect.height-2);
\r
155 public Point computeSize(int wHint, int hHint, boolean changed) {
\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
163 public void step(int failures) {
\r
164 fCurrentTickCount++;
\r
165 int x= fColorBarWidth;
\r
167 fColorBarWidth= scale(fCurrentTickCount);
\r
169 if (!fError && failures > 0) {
\r
173 if (fCurrentTickCount == fMaxTickCount)
\r
174 fColorBarWidth= getClientArea().width-1;
\r
175 paintStep(x, fColorBarWidth);
\r
178 public void refresh(boolean hasErrors) {
\r