KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > junit > swingui > DefaultFailureDetailView


1 package junit.swingui;
2
3 import java.awt.Component JavaDoc;
4 import java.awt.Font JavaDoc;
5 import java.util.StringTokenizer JavaDoc;
6 import java.util.Vector JavaDoc;
7
8 import javax.swing.AbstractListModel JavaDoc;
9 import javax.swing.DefaultListCellRenderer JavaDoc;
10 import javax.swing.JList JavaDoc;
11 import javax.swing.ListSelectionModel JavaDoc;
12
13 import junit.framework.TestFailure;
14 import junit.runner.BaseTestRunner;
15 import junit.runner.FailureDetailView;
16
17 /**
18  * A view that shows a stack trace of a failure
19  */

20 public class DefaultFailureDetailView implements FailureDetailView {
21     JList JavaDoc fList;
22     
23     /**
24      * A ListModel representing the scanned failure stack trace.
25      */

26     static class StackTraceListModel extends AbstractListModel JavaDoc {
27         private Vector JavaDoc fLines= new Vector JavaDoc(20);
28         
29         public Object JavaDoc getElementAt(int index) {
30             return fLines.elementAt(index);
31         }
32
33         public int getSize() {
34             return fLines.size();
35         }
36         
37         public void setTrace(String JavaDoc trace) {
38             scan(trace);
39             fireContentsChanged(this, 0, fLines.size());
40         }
41         
42         public void clear() {
43             fLines.removeAllElements();
44             fireContentsChanged(this, 0, fLines.size());
45         }
46         
47         private void scan(String JavaDoc trace) {
48             fLines.removeAllElements();
49             StringTokenizer JavaDoc st= new StringTokenizer JavaDoc(trace, "\n\r", false);
50             while (st.hasMoreTokens())
51                 fLines.addElement(st.nextToken());
52         }
53     }
54     
55     /**
56      * Renderer for stack entries
57      */

58     static class StackEntryRenderer extends DefaultListCellRenderer JavaDoc {
59                         
60         public Component JavaDoc getListCellRendererComponent(
61                 JList JavaDoc list, Object JavaDoc value, int modelIndex,
62                 boolean isSelected, boolean cellHasFocus) {
63             String JavaDoc text= ((String JavaDoc)value).replace('\t', ' ');
64             Component JavaDoc c= super.getListCellRendererComponent(list, text, modelIndex, isSelected, cellHasFocus);
65             setText(text);
66             setToolTipText(text);
67             return c;
68         }
69     }
70     
71     /**
72      * Returns the component used to present the trace
73      */

74     public Component JavaDoc getComponent() {
75         if (fList == null) {
76             fList= new JList JavaDoc(new StackTraceListModel());
77             fList.setFont(new Font JavaDoc("Dialog", Font.PLAIN, 12));
78             fList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
79             fList.setVisibleRowCount(5);
80             fList.setCellRenderer(new StackEntryRenderer());
81         }
82         return fList;
83     }
84     
85     /**
86      * Shows a TestFailure
87      */

88     public void showFailure(TestFailure failure) {
89         getModel().setTrace(BaseTestRunner.getFilteredTrace(failure.trace()));
90     }
91     /**
92      * Clears the output.
93      */

94     public void clear() {
95         getModel().clear();
96     }
97     
98     private StackTraceListModel getModel() {
99         return (StackTraceListModel)fList.getModel();
100     }
101 }
Popular Tags