1 package junit.swingui; 2 3 import java.awt.Component ; 4 import java.awt.GridBagConstraints ; 5 import java.awt.GridBagLayout ; 6 import java.awt.Insets ; 7 8 import javax.swing.BorderFactory ; 9 import javax.swing.Icon ; 10 import javax.swing.JLabel ; 11 import javax.swing.JPanel ; 12 import javax.swing.JTextField ; 13 import javax.swing.SwingConstants ; 14 15 18 public class CounterPanel extends JPanel { 19 private JTextField fNumberOfErrors; 20 private JTextField fNumberOfFailures; 21 private JTextField fNumberOfRuns; 22 private Icon fFailureIcon= TestRunner.getIconResource(getClass(), "icons/failure.gif"); 23 private Icon fErrorIcon= TestRunner.getIconResource(getClass(), "icons/error.gif"); 24 25 private int fTotal; 26 27 public CounterPanel() { 28 super(new GridBagLayout ()); 29 fNumberOfErrors= createOutputField(5); 30 fNumberOfFailures= createOutputField(5); 31 fNumberOfRuns= createOutputField(9); 32 33 addToGrid(new JLabel ("Runs:", SwingConstants.CENTER), 34 0, 0, 1, 1, 0.0, 0.0, 35 GridBagConstraints.CENTER, GridBagConstraints.NONE, 36 new Insets (0, 0, 0, 0)); 37 addToGrid(fNumberOfRuns, 38 1, 0, 1, 1, 0.33, 0.0, 39 GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, 40 new Insets (0, 8, 0, 0)); 41 42 addToGrid(new JLabel ("Errors:", fErrorIcon, SwingConstants.LEFT), 43 2, 0, 1, 1, 0.0, 0.0, 44 GridBagConstraints.CENTER, GridBagConstraints.NONE, 45 new Insets (0, 8, 0, 0)); 46 addToGrid(fNumberOfErrors, 47 3, 0, 1, 1, 0.33, 0.0, 48 GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, 49 new Insets (0, 8, 0, 0)); 50 51 addToGrid(new JLabel ("Failures:", fFailureIcon, SwingConstants.LEFT), 52 4, 0, 1, 1, 0.0, 0.0, 53 GridBagConstraints.CENTER, GridBagConstraints.NONE, 54 new Insets (0, 8, 0, 0)); 55 addToGrid(fNumberOfFailures, 56 5, 0, 1, 1, 0.33, 0.0, 57 GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, 58 new Insets (0, 8, 0, 0)); 59 } 60 61 private JTextField createOutputField(int width) { 62 JTextField field= new JTextField ("0", width); 63 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 comp, 74 int gridx, int gridy, int gridwidth, int gridheight, 75 double weightx, double weighty, 76 int anchor, int fill, 77 Insets insets) { 78 79 GridBagConstraints constraints= new GridBagConstraints (); 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 label, int value) { 116 label.setText(Integer.toString(value)); 117 } 118 } | Popular Tags |