1 package junit.swingui; 2 3 import java.awt.*; 4 import java.util.Vector; 5 6 import javax.swing.*; 7 import javax.swing.tree.*; 8 import junit.framework.*; 9 10 /*** 11 * A Panel showing a test suite as a tree. 12 */ 13 class TestSuitePanel extends JPanel implements TestListener { 14 private JTree fTree; 15 private JScrollPane fScrollTree; 16 private TestTreeModel fModel; 17 18 static class TestTreeCellRenderer extends DefaultTreeCellRenderer { 19 private Icon fErrorIcon; 20 private Icon fOkIcon; 21 private Icon fFailureIcon; 22 23 TestTreeCellRenderer() { 24 super(); 25 loadIcons(); 26 } 27 28 void loadIcons() { 29 fErrorIcon= TestRunner.getIconResource(getClass(), "icons/error.gif"); 30 fOkIcon= TestRunner.getIconResource(getClass(), "icons/ok.gif"); 31 fFailureIcon= TestRunner.getIconResource(getClass(), "icons/failure.gif"); 32 } 33 34 String stripParenthesis(Object o) { 35 String text= o.toString (); 36 int pos= text.indexOf('('); 37 if (pos < 1) 38 return text; 39 return text.substring (0, pos); 40 } 41 42 public Component getTreeCellRendererComponent(JTree tree, Object value, 43 boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) { 44 45 Component c= super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus); 46 TreeModel model= tree.getModel(); 47 if (model instanceof TestTreeModel) { 48 TestTreeModel testModel= (TestTreeModel)model; 49 Test t= (Test)value; 50 String s= ""; 51 if (testModel.isFailure(t)) { 52 if (fFailureIcon != null) 53 setIcon(fFailureIcon); 54 s= " - Failed"; 55 } 56 else if (testModel.isError(t)) { 57 if (fErrorIcon != null) 58 setIcon(fErrorIcon); 59 s= " - Error"; 60 } 61 else if (testModel.wasRun(t)) { 62 if (fOkIcon != null) 63 setIcon(fOkIcon); 64 s= " - Passed"; 65 } 66 if (c instanceof JComponent) 67 ((JComponent)c).setToolTipText(getText()+s); 68 } 69 setText(stripParenthesis(value)); 70 return c; 71 } 72 } 73 74 public TestSuitePanel() { 75 super(new BorderLayout()); 76 setPreferredSize(new Dimension(300, 100)); 77 fTree= new JTree(); 78 fTree.setModel(null); 79 fTree.setRowHeight(20); 80 ToolTipManager.sharedInstance().registerComponent(fTree); 81 fTree.putClientProperty("JTree.lineStyle", "Angled"); 82 fScrollTree= new JScrollPane(fTree); 83 add(fScrollTree, BorderLayout.CENTER); 84 } 85 86 public void addError(final Test test, final Throwable t) { 87 fModel.addError(test); 88 fireTestChanged(test, true); 89 } 90 91 public void addFailure(final Test test, final AssertionFailedError t) { 92 fModel.addFailure(test); 93 fireTestChanged(test, true); 94 } 95 96 /*** 97 * A test ended. 98 */ 99 public void endTest(Test test) { 100 fModel.addRunTest(test); 101 fireTestChanged(test, false); 102 } 103 104 /*** 105 * A test started. 106 */ 107 public void startTest(Test test) { 108 } 109 110 /*** 111 * Returns the selected test or null if multiple or none is selected 112 */ 113 public Test getSelectedTest() { 114 TreePath[] paths= fTree.getSelectionPaths(); 115 if (paths != null && paths.length == 1) 116 return (Test)paths[0].getLastPathComponent(); 117 return null; 118 } 119 120 /*** 121 * Returns the Tree 122 */ 123 public JTree getTree() { 124 return fTree; 125 } 126 127 /*** 128 * Shows the test hierarchy starting at the given test 129 */ 130 public void showTestTree(Test root) { 131 fModel= new TestTreeModel(root); 132 fTree.setModel(fModel); 133 fTree.setCellRenderer(new TestTreeCellRenderer()); 134 } 135 136 private void fireTestChanged(final Test test, final boolean expand) { 137 SwingUtilities.invokeLater( 138 new Runnable() { 139 public void run() { 140 Vector vpath= new Vector(); 141 int index= fModel.findTest(test, (Test)fModel.getRoot(), vpath); 142 if (index >= 0) { 143 Object[] path= new Object[vpath.size()]; 144 vpath.copyInto(path); 145 TreePath treePath= new TreePath(path); 146 fModel.fireNodeChanged(treePath, index); 147 if (expand) { 148 Object[] fullPath= new Object[vpath.size()+1]; 149 vpath.copyInto(fullPath); 150 fullPath[vpath.size()]= fModel.getChild(treePath.getLastPathComponent(), index);; 151 TreePath fullTreePath= new TreePath(fullPath); 152 fTree.scrollPathToVisible(fullTreePath); 153 } 154 } 155 } 156 } 157 ); 158 } 159 }