aboutsummaryrefslogtreecommitdiff
path: root/src/junit/swingui/CounterPanel.java
blob: cac4427476c096e3803fbba93c64539638e2982d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package junit.swingui;

import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

/**
 * A panel with test run counters
 */
public class CounterPanel extends JPanel {
	private JTextField fNumberOfErrors;
	private JTextField fNumberOfFailures;
	private JTextField fNumberOfRuns;
	private Icon fFailureIcon= TestRunner.getIconResource(getClass(), "icons/failure.gif");
	private Icon fErrorIcon= TestRunner.getIconResource(getClass(), "icons/error.gif");

	private int fTotal;

	public CounterPanel() {
		super(new GridBagLayout());
		fNumberOfErrors= createOutputField(5);
		fNumberOfFailures= createOutputField(5);
		fNumberOfRuns= createOutputField(9);

      addToGrid(new JLabel("Runs:", SwingConstants.CENTER),
          0, 0, 1, 1, 0.0, 0.0,
          GridBagConstraints.CENTER, GridBagConstraints.NONE,
          new Insets(0, 0, 0, 0));
     addToGrid(fNumberOfRuns,
          1, 0, 1, 1, 0.33, 0.0,
          GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
          new Insets(0, 8, 0, 0));

     addToGrid(new JLabel("Errors:", fErrorIcon, SwingConstants.LEFT),
          2, 0, 1, 1, 0.0, 0.0,
          GridBagConstraints.CENTER, GridBagConstraints.NONE,
          new Insets(0, 8, 0, 0));
      addToGrid(fNumberOfErrors,
          3, 0, 1, 1, 0.33, 0.0,
          GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
          new Insets(0, 8, 0, 0));

      addToGrid(new JLabel("Failures:", fFailureIcon, SwingConstants.LEFT),
          4, 0, 1, 1, 0.0, 0.0,
          GridBagConstraints.CENTER, GridBagConstraints.NONE,
          new Insets(0, 8, 0, 0));
      addToGrid(fNumberOfFailures,
          5, 0, 1, 1, 0.33, 0.0,
          GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
          new Insets(0, 8, 0, 0));
	}

	private JTextField createOutputField(int width) {
		JTextField field= new JTextField("0", width);
		// force a fixed layout to avoid accidental hiding on relayout
		field.setMinimumSize(field.getPreferredSize());
		field.setMaximumSize(field.getPreferredSize());
		field.setHorizontalAlignment(SwingConstants.LEFT);
		field.setFont(StatusLine.BOLD_FONT);
		field.setEditable(false);
		field.setBorder(BorderFactory.createEmptyBorder());
		return field;
	}

	public void addToGrid(Component comp,
	    	int gridx, int gridy, int gridwidth, int gridheight,
			double weightx, double weighty,
			int anchor, int fill,
			Insets insets) {

		GridBagConstraints constraints= new GridBagConstraints();
		constraints.gridx= gridx;
		constraints.gridy= gridy;
		constraints.gridwidth= gridwidth;
		constraints.gridheight= gridheight;
		constraints.weightx= weightx;
		constraints.weighty= weighty;
		constraints.anchor= anchor;
		constraints.fill= fill;
		constraints.insets= insets;
		add(comp, constraints);
	}

	public void reset() {
		setLabelValue(fNumberOfErrors, 0);
		setLabelValue(fNumberOfFailures, 0);
		setLabelValue(fNumberOfRuns, 0);
		fTotal= 0;
	}

	public void setTotal(int value) {
		fTotal= value;
	}

	public void setRunValue(int value) {
		fNumberOfRuns.setText(Integer.toString(value) + "/" + fTotal);
	}

	public void setErrorValue(int value) {
		setLabelValue(fNumberOfErrors, value);
	}

	public void setFailureValue(int value) {
		setLabelValue(fNumberOfFailures, value);
	}

	private void setLabelValue(JTextField label, int value) {
		label.setText(Integer.toString(value));
	}
}