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