Enhancements to modelled tests
[simantics/platform.git] / bundles / org.simantics.tests.modelled.ui / src / org / simantics / tests / modelled / ui / STSCounterPanel.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2013 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  *******************************************************************************/
11 package org.simantics.tests.modelled.ui;
12
13
14 import java.text.MessageFormat;
15
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.events.DisposeEvent;
18 import org.eclipse.swt.events.DisposeListener;
19 import org.eclipse.swt.graphics.Image;
20 import org.eclipse.swt.layout.GridData;
21 import org.eclipse.swt.layout.GridLayout;
22 import org.eclipse.swt.widgets.Composite;
23 import org.eclipse.swt.widgets.Label;
24 import org.eclipse.swt.widgets.Text;
25
26 /**
27  * A panel with counters for the number of Runs, Errors and Failures.
28  */
29 public class STSCounterPanel extends Composite {
30     protected Text numberOfErrors;
31     protected Text numberOfSkipped;
32     protected Text numberOfFailures;
33     protected Text numberOfRuns;
34     protected int total;
35     protected int ignoredCount;
36     protected int assumptionFailedCount;
37
38     private final Image fErrorIcon = null;
39     private final Image fFailureIcon = null;
40
41     public STSCounterPanel(Composite parent) {
42         super(parent, SWT.WRAP);
43         GridLayout gridLayout= new GridLayout();
44         gridLayout.numColumns= 9;
45         gridLayout.makeColumnsEqualWidth= false;
46         gridLayout.marginWidth= 0;
47         setLayout(gridLayout);
48
49         numberOfRuns= createLabel("Runs:", null, " 0/0  "); //$NON-NLS-1$
50         numberOfSkipped = createLabel("Skipped:", null," 0 ");
51         numberOfErrors= createLabel("Errors:", fErrorIcon, " 0 "); //$NON-NLS-1$
52         numberOfFailures= createLabel("Failures:", fFailureIcon, " 0 "); //$NON-NLS-1$
53
54         addDisposeListener(new DisposeListener() {
55             public void widgetDisposed(DisposeEvent e) {
56                 disposeIcons();
57             }
58         });
59     }
60
61     private void disposeIcons() {
62         if (fErrorIcon != null)
63             fErrorIcon.dispose();
64         if (fFailureIcon != null)
65             fFailureIcon.dispose();
66     }
67
68     private Text createLabel(String name, Image image, String init) {
69         Label label= new Label(this, SWT.NONE);
70         if (image != null) {
71             image.setBackground(label.getBackground());
72             label.setImage(image);
73         }
74         label.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING));
75
76         label = new Label(this, SWT.NONE);
77         label.setText(name);
78         label.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING));
79         //label.setFont(JFaceResources.getBannerFont());
80
81         Text value = new Text(this, SWT.READ_ONLY);
82         value.setText(init);
83         // bug: 39661 Junit test counters do not repaint correctly [JUnit]
84 //        SWTUtil.fixReadonlyTextBackground(value);
85         value.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.HORIZONTAL_ALIGN_BEGINNING));
86         return value;
87     }
88
89     public void reset() {
90         setErrorValue(0);
91         setFailureValue(0);
92         setRunValue(0, 0, 0);
93         total= 0;
94     }
95
96     public void setTotal(int value) {
97         total= value;
98     }
99
100     public int getTotal(){
101         return total;
102     }
103
104     public void setRunValue(int value, int ignoredCount, int assumptionFailureCount) {
105         String runString;
106         String runStringTooltip;
107         if (ignoredCount == 0 && assumptionFailureCount == 0) {
108             runString= Messages.format("{0}/{1}", new String[] { Integer.toString(value), Integer.toString(total) });
109             runStringTooltip= runString;
110         } else if (ignoredCount != 0 && assumptionFailureCount == 0) {
111             runString= Messages.format("Skipped", new String[] { Integer.toString(value), Integer.toString(total), Integer.toString(ignoredCount) });
112             runStringTooltip= Messages.format("Ignored", new String[] { Integer.toString(value), Integer.toString(total), Integer.toString(ignoredCount) });
113         } else if (ignoredCount == 0 && assumptionFailureCount != 0) {
114             runString= Messages.format("Skipped", new String[] { Integer.toString(value), Integer.toString(total), Integer.toString(assumptionFailureCount) });
115             runStringTooltip= Messages.format("Failed", new String[] { Integer.toString(value), Integer.toString(total), Integer.toString(assumptionFailureCount) });
116         } else {
117             runString= Messages.format("Skipped", new String[] { Integer.toString(value), Integer.toString(total), Integer.toString(ignoredCount + assumptionFailureCount) });
118             runStringTooltip= Messages.format("Failed", new String[] { Integer.toString(value), Integer.toString(total), Integer.toString(ignoredCount), Integer.toString(assumptionFailureCount) });
119         }
120         numberOfRuns.setText(runString);
121         numberOfRuns.setToolTipText(runStringTooltip);
122
123         if (ignoredCount == 0 && ignoredCount > 0  || ignoredCount != 0 && ignoredCount == 0) {
124             layout();
125         } else if (assumptionFailedCount == 0 && assumptionFailureCount > 0 || assumptionFailedCount != 0 && assumptionFailureCount == 0) {
126             layout();
127         } else {
128             numberOfRuns.redraw();
129             redraw();
130         }
131         assumptionFailedCount= assumptionFailureCount;
132     }
133
134     public void setIgnoredValue(int value) {
135         numberOfSkipped.setText(Integer.toString(value));
136         redraw();
137     }
138     
139     public void setErrorValue(int value) {
140         numberOfErrors.setText(Integer.toString(value));
141         redraw();
142     }
143
144     public void setFailureValue(int value) {
145         numberOfFailures.setText(Integer.toString(value));
146         redraw();
147     }
148     
149     private static class Messages {
150
151         public static String format(String message, Object object) {
152             return MessageFormat.format(message, new Object[] { object});
153         }
154
155         public static String format(String message, Object[] objects) {
156             return MessageFormat.format(message, objects);
157         }
158
159         private Messages() {
160             // Not for instantiation
161         }
162     }
163 }