--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2000, 2010 IBM Corporation and others.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * IBM Corporation - initial API and implementation\r
+ * Stephan Michels, stephan@apache.org - 104944 [JUnit] Unnecessary code in JUnitProgressBar\r
+ *******************************************************************************/\r
+package org.simantics.tests.modelled.ui;\r
+\r
+import org.eclipse.swt.SWT;\r
+import org.eclipse.swt.events.ControlAdapter;\r
+import org.eclipse.swt.events.ControlEvent;\r
+import org.eclipse.swt.events.DisposeEvent;\r
+import org.eclipse.swt.events.DisposeListener;\r
+import org.eclipse.swt.events.PaintEvent;\r
+import org.eclipse.swt.events.PaintListener;\r
+import org.eclipse.swt.graphics.Color;\r
+import org.eclipse.swt.graphics.GC;\r
+import org.eclipse.swt.graphics.Point;\r
+import org.eclipse.swt.graphics.Rectangle;\r
+import org.eclipse.swt.widgets.Canvas;\r
+import org.eclipse.swt.widgets.Composite;\r
+import org.eclipse.swt.widgets.Display;\r
+\r
+/**\r
+ * A progress bar with a red/green indication for success or failure.\r
+ */\r
+public class STSProgressBar extends Canvas {\r
+ private static final int DEFAULT_WIDTH = 160;\r
+ private static final int DEFAULT_HEIGHT = 18;\r
+\r
+ private int fCurrentTickCount= 0;\r
+ private int fMaxTickCount= 0;\r
+ private int fColorBarWidth= 0;\r
+ private Color fOKColor;\r
+ private Color fFailureColor;\r
+ private Color fStoppedColor;\r
+ private boolean fError;\r
+ private boolean fStopped= false;\r
+\r
+ public STSProgressBar(Composite parent) {\r
+ super(parent, SWT.NONE);\r
+\r
+ addControlListener(new ControlAdapter() {\r
+ @Override\r
+ public void controlResized(ControlEvent e) {\r
+ fColorBarWidth= scale(fCurrentTickCount);\r
+ redraw();\r
+ }\r
+ });\r
+ addPaintListener(new PaintListener() {\r
+ public void paintControl(PaintEvent e) {\r
+ paint(e);\r
+ }\r
+ });\r
+ addDisposeListener(new DisposeListener() {\r
+ public void widgetDisposed(DisposeEvent e) {\r
+ fFailureColor.dispose();\r
+ fOKColor.dispose();\r
+ fStoppedColor.dispose();\r
+ }\r
+ });\r
+ Display display= parent.getDisplay();\r
+ fFailureColor= new Color(display, 159, 63, 63);\r
+ fOKColor= new Color(display, 95, 191, 95);\r
+ fStoppedColor= new Color(display, 120, 120, 120);\r
+ }\r
+\r
+ public void setMaximum(int max) {\r
+ fMaxTickCount= max;\r
+ }\r
+\r
+ public void reset() {\r
+ fError= false;\r
+ fStopped= false;\r
+ fCurrentTickCount= 0;\r
+ fMaxTickCount= 0;\r
+ fColorBarWidth= 0;\r
+ redraw();\r
+ }\r
+\r
+ public void reset(boolean hasErrors, boolean stopped, int ticksDone, int maximum) {\r
+ boolean noChange= fError == hasErrors && fStopped == stopped && fCurrentTickCount == ticksDone && fMaxTickCount == maximum;\r
+ fError= hasErrors;\r
+ fStopped= stopped;\r
+ fCurrentTickCount= ticksDone;\r
+ fMaxTickCount= maximum;\r
+ fColorBarWidth= scale(ticksDone);\r
+ if (! noChange)\r
+ redraw();\r
+ }\r
+\r
+ private void paintStep(int startX, int endX) {\r
+ GC gc = new GC(this);\r
+ setStatusColor(gc);\r
+ Rectangle rect= getClientArea();\r
+ startX= Math.max(1, startX);\r
+ gc.fillRectangle(startX, 1, endX-startX, rect.height-2);\r
+ gc.dispose();\r
+ }\r
+\r
+ private void setStatusColor(GC gc) {\r
+ if (fStopped)\r
+ gc.setBackground(fStoppedColor);\r
+ else if (fError)\r
+ gc.setBackground(fFailureColor);\r
+ else\r
+ gc.setBackground(fOKColor);\r
+ }\r
+\r
+ public void stopped() {\r
+ fStopped= true;\r
+ redraw();\r
+ }\r
+\r
+ private int scale(int value) {\r
+ if (fMaxTickCount > 0) {\r
+ Rectangle r= getClientArea();\r
+ if (r.width != 0)\r
+ return Math.max(0, value*(r.width-2)/fMaxTickCount);\r
+ }\r
+ return value;\r
+ }\r
+\r
+ private void drawBevelRect(GC gc, int x, int y, int w, int h, Color topleft, Color bottomright) {\r
+ gc.setForeground(topleft);\r
+ gc.drawLine(x, y, x+w-1, y);\r
+ gc.drawLine(x, y, x, y+h-1);\r
+\r
+ gc.setForeground(bottomright);\r
+ gc.drawLine(x+w, y, x+w, y+h);\r
+ gc.drawLine(x, y+h, x+w, y+h);\r
+ }\r
+\r
+ private void paint(PaintEvent event) {\r
+ GC gc = event.gc;\r
+ Display disp= getDisplay();\r
+\r
+ Rectangle rect= getClientArea();\r
+ gc.fillRectangle(rect);\r
+ drawBevelRect(gc, rect.x, rect.y, rect.width-1, rect.height-1,\r
+ disp.getSystemColor(SWT.COLOR_WIDGET_NORMAL_SHADOW),\r
+ disp.getSystemColor(SWT.COLOR_WIDGET_HIGHLIGHT_SHADOW));\r
+\r
+ setStatusColor(gc);\r
+ fColorBarWidth= Math.min(rect.width-2, fColorBarWidth);\r
+ gc.fillRectangle(1, 1, fColorBarWidth, rect.height-2);\r
+ }\r
+\r
+ @Override\r
+ public Point computeSize(int wHint, int hHint, boolean changed) {\r
+ checkWidget();\r
+ Point size= new Point(DEFAULT_WIDTH, DEFAULT_HEIGHT);\r
+ if (wHint != SWT.DEFAULT) size.x= wHint;\r
+ if (hHint != SWT.DEFAULT) size.y= hHint;\r
+ return size;\r
+ }\r
+\r
+ public void step(int failures) {\r
+ fCurrentTickCount++;\r
+ int x= fColorBarWidth;\r
+\r
+ fColorBarWidth= scale(fCurrentTickCount);\r
+\r
+ if (!fError && failures > 0) {\r
+ fError= true;\r
+ x= 1;\r
+ }\r
+ if (fCurrentTickCount == fMaxTickCount)\r
+ fColorBarWidth= getClientArea().width-1;\r
+ paintStep(x, fColorBarWidth);\r
+ }\r
+\r
+ public void refresh(boolean hasErrors) {\r
+ fError= hasErrors;\r
+ redraw();\r
+ }\r
+\r
+}\r