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