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