KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > junit > swingui > TestSuitePanel


1 package junit.swingui;
2
3 import java.awt.BorderLayout JavaDoc;
4 import java.awt.Component JavaDoc;
5 import java.awt.Dimension JavaDoc;
6 import java.util.Vector JavaDoc;
7
8 import javax.swing.Icon JavaDoc;
9 import javax.swing.JComponent JavaDoc;
10 import javax.swing.JPanel JavaDoc;
11 import javax.swing.JScrollPane JavaDoc;
12 import javax.swing.JTree JavaDoc;
13 import javax.swing.SwingUtilities JavaDoc;
14 import javax.swing.ToolTipManager JavaDoc;
15 import javax.swing.tree.DefaultTreeCellRenderer JavaDoc;
16 import javax.swing.tree.TreeModel JavaDoc;
17 import javax.swing.tree.TreePath JavaDoc;
18
19 import junit.framework.AssertionFailedError;
20 import junit.framework.Test;
21 import junit.framework.TestListener;
22
23 /**
24  * A Panel showing a test suite as a tree.
25  */

26 class TestSuitePanel extends JPanel JavaDoc implements TestListener {
27     private JTree JavaDoc fTree;
28     private JScrollPane JavaDoc fScrollTree;
29     private TestTreeModel fModel;
30
31     static class TestTreeCellRenderer extends DefaultTreeCellRenderer JavaDoc {
32         private Icon JavaDoc fErrorIcon;
33         private Icon JavaDoc fOkIcon;
34         private Icon JavaDoc fFailureIcon;
35         
36         TestTreeCellRenderer() {
37                 super();
38                 loadIcons();
39         }
40         
41         void loadIcons() {
42             fErrorIcon= TestRunner.getIconResource(getClass(), "icons/error.gif");
43                 fOkIcon= TestRunner.getIconResource(getClass(), "icons/ok.gif");
44                 fFailureIcon= TestRunner.getIconResource(getClass(), "icons/failure.gif");
45         }
46         
47         String JavaDoc stripParenthesis(Object JavaDoc o) {
48             String JavaDoc text= o.toString ();
49                 int pos= text.indexOf('(');
50                 if (pos < 1)
51                     return text;
52                 return text.substring (0, pos);
53         }
54
55         public Component JavaDoc getTreeCellRendererComponent(JTree JavaDoc tree, Object JavaDoc value,
56                 boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
57             
58             Component JavaDoc c= super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
59             TreeModel JavaDoc model= tree.getModel();
60             if (model instanceof TestTreeModel) {
61                 TestTreeModel testModel= (TestTreeModel)model;
62                 Test t= (Test)value;
63                 String JavaDoc s= "";
64                 if (testModel.isFailure(t)) {
65                     if (fFailureIcon != null)
66                         setIcon(fFailureIcon);
67                     s= " - Failed";
68                 }
69                 else if (testModel.isError(t)) {
70                     if (fErrorIcon != null)
71                         setIcon(fErrorIcon);
72                     s= " - Error";
73                 }
74                 else if (testModel.wasRun(t)) {
75                     if (fOkIcon != null)
76                         setIcon(fOkIcon);
77                     s= " - Passed";
78                 }
79                 if (c instanceof JComponent JavaDoc)
80                     ((JComponent JavaDoc)c).setToolTipText(getText()+s);
81             }
82             setText(stripParenthesis(value));
83             return c;
84         }
85     }
86     
87     public TestSuitePanel() {
88         super(new BorderLayout JavaDoc());
89         setPreferredSize(new Dimension JavaDoc(300, 100));
90         fTree= new JTree JavaDoc();
91         fTree.setModel(null);
92         fTree.setRowHeight(20);
93         ToolTipManager.sharedInstance().registerComponent(fTree);
94         fTree.putClientProperty("JTree.lineStyle", "Angled");
95         fScrollTree= new JScrollPane JavaDoc(fTree);
96         add(fScrollTree, BorderLayout.CENTER);
97     }
98         
99     public void addError(final Test test, final Throwable JavaDoc t) {
100         fModel.addError(test);
101         fireTestChanged(test, true);
102     }
103     
104     public void addFailure(final Test test, final AssertionFailedError t) {
105         fModel.addFailure(test);
106         fireTestChanged(test, true);
107     }
108     
109     /**
110      * A test ended.
111      */

112     public void endTest(Test test) {
113         fModel.addRunTest(test);
114         fireTestChanged(test, false);
115     }
116
117     /**
118      * A test started.
119      */

120     public void startTest(Test test) {
121     }
122             
123     /**
124      * Returns the selected test or null if multiple or none is selected
125      */

126     public Test getSelectedTest() {
127         TreePath JavaDoc[] paths= fTree.getSelectionPaths();
128         if (paths != null && paths.length == 1)
129             return (Test)paths[0].getLastPathComponent();
130         return null;
131     }
132
133     /**
134      * Returns the Tree
135      */

136     public JTree JavaDoc getTree() {
137         return fTree;
138     }
139
140     /**
141      * Shows the test hierarchy starting at the given test
142      */

143     public void showTestTree(Test root) {
144         fModel= new TestTreeModel(root);
145         fTree.setModel(fModel);
146         fTree.setCellRenderer(new TestTreeCellRenderer());
147     }
148     
149     private void fireTestChanged(final Test test, final boolean expand) {
150         SwingUtilities.invokeLater(
151             new Runnable JavaDoc() {
152                 public void run() {
153                     Vector JavaDoc vpath= new Vector JavaDoc();
154                     int index= fModel.findTest(test, (Test)fModel.getRoot(), vpath);
155                     if (index >= 0) {
156                         Object JavaDoc[] path= new Object JavaDoc[vpath.size()];
157                         vpath.copyInto(path);
158                         TreePath JavaDoc treePath= new TreePath JavaDoc(path);
159                         fModel.fireNodeChanged(treePath, index);
160                         if (expand) {
161                             Object JavaDoc[] fullPath= new Object JavaDoc[vpath.size()+1];
162                             vpath.copyInto(fullPath);
163                             fullPath[vpath.size()]= fModel.getChild(treePath.getLastPathComponent(), index);;
164                             TreePath JavaDoc fullTreePath= new TreePath JavaDoc(fullPath);
165                             fTree.scrollPathToVisible(fullTreePath);
166                         }
167                     }
168                 }
169             }
170         );
171     }
172 }
Popular Tags