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