KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > junit > swingui > CounterPanel


1 package junit.swingui;
2
3 import java.awt.Component JavaDoc;
4 import java.awt.GridBagConstraints JavaDoc;
5 import java.awt.GridBagLayout JavaDoc;
6 import java.awt.Insets JavaDoc;
7
8 import javax.swing.BorderFactory JavaDoc;
9 import javax.swing.Icon JavaDoc;
10 import javax.swing.JLabel JavaDoc;
11 import javax.swing.JPanel JavaDoc;
12 import javax.swing.JTextField JavaDoc;
13 import javax.swing.SwingConstants JavaDoc;
14
15 /**
16  * A panel with test run counters
17  */

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