1   package junit.swingui;
2   
3   import java.awt.*;
4   
5   import javax.swing.*;
6   
7   /***
8    * A panel with test run counters
9    */
10  public class CounterPanel extends JPanel {
11  	private JTextField fNumberOfErrors;
12  	private JTextField fNumberOfFailures;
13  	private JTextField fNumberOfRuns;
14  	private Icon fFailureIcon= TestRunner.getIconResource(getClass(), "icons/failure.gif");
15  	private Icon fErrorIcon= TestRunner.getIconResource(getClass(), "icons/error.gif");
16  
17  	private int fTotal;
18  
19  	public CounterPanel() {
20  		super(new GridBagLayout());
21  		fNumberOfErrors= createOutputField(5);
22  		fNumberOfFailures= createOutputField(5);
23  		fNumberOfRuns= createOutputField(9);
24  
25        addToGrid(new JLabel("Runs:", JLabel.CENTER),
26            0, 0, 1, 1, 0.0, 0.0,
27            GridBagConstraints.CENTER, GridBagConstraints.NONE,
28            new Insets(0, 0, 0, 0));
29       addToGrid(fNumberOfRuns,
30            1, 0, 1, 1, 0.33, 0.0,
31            GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
32            new Insets(0, 8, 0, 0));
33  
34       addToGrid(new JLabel("Errors:", fErrorIcon, SwingConstants.LEFT),
35            2, 0, 1, 1, 0.0, 0.0,
36            GridBagConstraints.CENTER, GridBagConstraints.NONE,
37            new Insets(0, 8, 0, 0));
38        addToGrid(fNumberOfErrors,
39            3, 0, 1, 1, 0.33, 0.0,
40            GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
41            new Insets(0, 8, 0, 0));
42  
43        addToGrid(new JLabel("Failures:", fFailureIcon, SwingConstants.LEFT),
44            4, 0, 1, 1, 0.0, 0.0,
45            GridBagConstraints.CENTER, GridBagConstraints.NONE,
46            new Insets(0, 8, 0, 0));
47        addToGrid(fNumberOfFailures,
48            5, 0, 1, 1, 0.33, 0.0,
49            GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
50            new Insets(0, 8, 0, 0));
51  	}
52  
53  	private JTextField createOutputField(int width) {
54  		JTextField field= new JTextField("0", width);
55  		// force a fixed layout to avoid accidental hiding on relayout
56  		field.setMinimumSize(field.getPreferredSize());
57  		field.setMaximumSize(field.getPreferredSize());
58  		field.setHorizontalAlignment(JTextField.LEFT);
59  		field.setFont(StatusLine.BOLD_FONT);
60  		field.setEditable(false);
61  		field.setBorder(BorderFactory.createEmptyBorder());
62  		return field;
63  	}
64  
65  	public void addToGrid(Component comp,
66  	    	int gridx, int gridy, int gridwidth, int gridheight,
67  			double weightx, double weighty,
68  			int anchor, int fill,
69  			Insets insets) {
70  
71  		GridBagConstraints constraints= new GridBagConstraints();
72  		constraints.gridx= gridx;
73  		constraints.gridy= gridy;
74  		constraints.gridwidth= gridwidth;
75  		constraints.gridheight= gridheight;
76  		constraints.weightx= weightx;
77  		constraints.weighty= weighty;
78  		constraints.anchor= anchor;
79  		constraints.fill= fill;
80  		constraints.insets= insets;
81  		add(comp, constraints);
82  	}
83  
84  	public void reset() {
85  		setLabelValue(fNumberOfErrors, 0);
86  		setLabelValue(fNumberOfFailures, 0);
87  		setLabelValue(fNumberOfRuns, 0);
88  		fTotal= 0;
89  	}
90  
91  	public void setTotal(int value) {
92  		fTotal= value;
93  	}
94  
95  	public void setRunValue(int value) {
96  		fNumberOfRuns.setText(Integer.toString(value) + "/" + fTotal);
97  	}
98  
99  	public void setErrorValue(int value) {
100 		setLabelValue(fNumberOfErrors, value);
101 	}
102 
103 	public void setFailureValue(int value) {
104 		setLabelValue(fNumberOfFailures, value);
105 	}
106 
107 	private void setLabelValue(JTextField label, int value) {
108 		label.setText(Integer.toString(value));
109 	}
110 }