1. package junit.swingui;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.util.*;
  5. import javax.swing.*;
  6. import javax.swing.event.*;
  7. import junit.runner.*;
  8. /**
  9. * A test class selector. A simple dialog to pick the name of a test suite.
  10. */
  11. class TestSelector extends JDialog {
  12. private JButton fCancel;
  13. private JButton fOk;
  14. private JList fList;
  15. private JScrollPane fScrolledList;
  16. private JLabel fDescription;
  17. private String fSelectedItem;
  18. /**
  19. * Renders TestFailures in a JList
  20. */
  21. static class TestCellRenderer extends DefaultListCellRenderer {
  22. Icon fLeafIcon;
  23. Icon fSuiteIcon;
  24. public TestCellRenderer() {
  25. fLeafIcon= UIManager.getIcon("Tree.leafIcon");
  26. fSuiteIcon= UIManager.getIcon("Tree.closedIcon");
  27. }
  28. public Component getListCellRendererComponent(
  29. JList list, Object value, int modelIndex,
  30. boolean isSelected, boolean cellHasFocus) {
  31. Component c= super.getListCellRendererComponent(list, value, modelIndex, isSelected, cellHasFocus);
  32. String displayString= displayString((String)value);
  33. if (displayString.startsWith("AllTests"))
  34. setIcon(fSuiteIcon);
  35. else
  36. setIcon(fLeafIcon);
  37. setText(displayString);
  38. return c;
  39. }
  40. public static String displayString(String className) {
  41. int typeIndex= className.lastIndexOf('.');
  42. if (typeIndex < 0)
  43. return className;
  44. return className.substring(typeIndex+1) + " - " + className.substring(0, typeIndex);
  45. }
  46. public static boolean matchesKey(String s, char ch) {
  47. return ch == Character.toUpperCase(s.charAt(typeIndex(s)));
  48. }
  49. private static int typeIndex(String s) {
  50. int typeIndex= s.lastIndexOf('.');
  51. int i= 0;
  52. if (typeIndex > 0)
  53. i= typeIndex+1;
  54. return i;
  55. }
  56. }
  57. protected class DoubleClickListener extends MouseAdapter {
  58. public void mouseClicked(MouseEvent e) {
  59. if (e.getClickCount() == 2) {
  60. okSelected();
  61. }
  62. }
  63. }
  64. protected class KeySelectListener extends KeyAdapter {
  65. public void keyTyped(KeyEvent e) {
  66. keySelectTestClass(e.getKeyChar());
  67. }
  68. }
  69. public TestSelector(Frame parent, TestCollector testCollector) {
  70. super(parent, true);
  71. setSize(350, 300);
  72. setResizable(false);
  73. // setLocationRelativeTo only exists in 1.4
  74. try {
  75. setLocationRelativeTo(parent);
  76. } catch (NoSuchMethodError e) {
  77. centerWindow(this);
  78. }
  79. setTitle("Test Selector");
  80. Vector list= null;
  81. try {
  82. parent.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
  83. list= createTestList(testCollector);
  84. } finally {
  85. parent.setCursor(Cursor.getDefaultCursor());
  86. }
  87. fList= new JList(list);
  88. fList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  89. fList.setCellRenderer(new TestCellRenderer());
  90. fScrolledList= new JScrollPane(fList);
  91. fCancel= new JButton("Cancel");
  92. fDescription= new JLabel("Select the Test class:");
  93. fOk= new JButton("OK");
  94. fOk.setEnabled(false);
  95. getRootPane().setDefaultButton(fOk);
  96. defineLayout();
  97. addListeners();
  98. }
  99. public static void centerWindow(Component c) {
  100. Dimension paneSize= c.getSize();
  101. Dimension screenSize= c.getToolkit().getScreenSize();
  102. c.setLocation((screenSize.width-paneSize.width)/2, (screenSize.height-paneSize.height)/2);
  103. }
  104. private void addListeners() {
  105. fCancel.addActionListener(
  106. new ActionListener() {
  107. public void actionPerformed(ActionEvent e) {
  108. dispose();
  109. }
  110. }
  111. );
  112. fOk.addActionListener(
  113. new ActionListener() {
  114. public void actionPerformed(ActionEvent e) {
  115. okSelected();
  116. }
  117. }
  118. );
  119. fList.addMouseListener(new DoubleClickListener());
  120. fList.addKeyListener(new KeySelectListener());
  121. fList.addListSelectionListener(
  122. new ListSelectionListener() {
  123. public void valueChanged(ListSelectionEvent e) {
  124. checkEnableOK(e);
  125. }
  126. }
  127. );
  128. addWindowListener(
  129. new WindowAdapter() {
  130. public void windowClosing(WindowEvent e) {
  131. dispose();
  132. }
  133. }
  134. );
  135. }
  136. private void defineLayout() {
  137. getContentPane().setLayout(new GridBagLayout());
  138. GridBagConstraints labelConstraints = new GridBagConstraints();
  139. labelConstraints.gridx= 0; labelConstraints.gridy= 0;
  140. labelConstraints.gridwidth= 1; labelConstraints.gridheight= 1;
  141. labelConstraints.fill= GridBagConstraints.BOTH;
  142. labelConstraints.anchor= GridBagConstraints.WEST;
  143. labelConstraints.weightx= 1.0;
  144. labelConstraints.weighty= 0.0;
  145. labelConstraints.insets= new Insets(8, 8, 0, 8);
  146. getContentPane().add(fDescription, labelConstraints);
  147. GridBagConstraints listConstraints = new GridBagConstraints();
  148. listConstraints.gridx= 0; listConstraints.gridy= 1;
  149. listConstraints.gridwidth= 4; listConstraints.gridheight= 1;
  150. listConstraints.fill= GridBagConstraints.BOTH;
  151. listConstraints.anchor= GridBagConstraints.CENTER;
  152. listConstraints.weightx= 1.0;
  153. listConstraints.weighty= 1.0;
  154. listConstraints.insets= new Insets(8, 8, 8, 8);
  155. getContentPane().add(fScrolledList, listConstraints);
  156. GridBagConstraints okConstraints= new GridBagConstraints();
  157. okConstraints.gridx= 2; okConstraints.gridy= 2;
  158. okConstraints.gridwidth= 1; okConstraints.gridheight= 1;
  159. okConstraints.anchor= java.awt.GridBagConstraints.EAST;
  160. okConstraints.insets= new Insets(0, 8, 8, 8);
  161. getContentPane().add(fOk, okConstraints);
  162. GridBagConstraints cancelConstraints = new GridBagConstraints();
  163. cancelConstraints.gridx= 3; cancelConstraints.gridy= 2;
  164. cancelConstraints.gridwidth= 1; cancelConstraints.gridheight= 1;
  165. cancelConstraints.anchor= java.awt.GridBagConstraints.EAST;
  166. cancelConstraints.insets= new Insets(0, 8, 8, 8);
  167. getContentPane().add(fCancel, cancelConstraints);
  168. }
  169. public void checkEnableOK(ListSelectionEvent e) {
  170. fOk.setEnabled(fList.getSelectedIndex() != -1);
  171. }
  172. public void okSelected() {
  173. fSelectedItem= (String)fList.getSelectedValue();
  174. dispose();
  175. }
  176. public boolean isEmpty() {
  177. return fList.getModel().getSize() == 0;
  178. }
  179. public void keySelectTestClass(char ch) {
  180. ListModel model= fList.getModel();
  181. if (!Character.isJavaIdentifierStart(ch))
  182. return;
  183. for (int i= 0; i < model.getSize(); i++) {
  184. String s= (String)model.getElementAt(i);
  185. if (TestCellRenderer.matchesKey(s, Character.toUpperCase(ch))) {
  186. fList.setSelectedIndex(i);
  187. fList.ensureIndexIsVisible(i);
  188. return;
  189. }
  190. }
  191. Toolkit.getDefaultToolkit().beep();
  192. }
  193. public String getSelectedItem() {
  194. return fSelectedItem;
  195. }
  196. private Vector createTestList(TestCollector collector) {
  197. Enumeration each= collector.collectTests();
  198. Vector v= new Vector(200);
  199. Vector displayVector= new Vector(v.size());
  200. while(each.hasMoreElements()) {
  201. String s= (String)each.nextElement();
  202. v.addElement(s);
  203. displayVector.addElement(TestCellRenderer.displayString(s));
  204. }
  205. if (v.size() > 0)
  206. Sorter.sortStrings(displayVector, 0, displayVector.size()-1, new ParallelSwapper(v));
  207. return v;
  208. }
  209. private class ParallelSwapper implements Sorter.Swapper {
  210. Vector fOther;
  211. ParallelSwapper(Vector other) {
  212. fOther= other;
  213. }
  214. public void swap(Vector values, int left, int right) {
  215. Object tmp= values.elementAt(left);
  216. values.setElementAt(values.elementAt(right), left);
  217. values.setElementAt(tmp, right);
  218. Object tmp2= fOther.elementAt(left);
  219. fOther.setElementAt(fOther.elementAt(right), left);
  220. fOther.setElementAt(tmp2, right);
  221. }
  222. }
  223. }