1 package junit.swingui; 2 3 import java.awt.Component ; 4 import java.awt.Font ; 5 import java.util.StringTokenizer ; 6 import java.util.Vector ; 7 8 import javax.swing.AbstractListModel ; 9 import javax.swing.DefaultListCellRenderer ; 10 import javax.swing.JList ; 11 import javax.swing.ListSelectionModel ; 12 13 import junit.framework.TestFailure; 14 import junit.runner.BaseTestRunner; 15 import junit.runner.FailureDetailView; 16 17 20 public class DefaultFailureDetailView implements FailureDetailView { 21 JList fList; 22 23 26 static class StackTraceListModel extends AbstractListModel { 27 private Vector fLines= new Vector (20); 28 29 public Object 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 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 trace) { 48 fLines.removeAllElements(); 49 StringTokenizer st= new StringTokenizer (trace, "\n\r", false); 50 while (st.hasMoreTokens()) 51 fLines.addElement(st.nextToken()); 52 } 53 } 54 55 58 static class StackEntryRenderer extends DefaultListCellRenderer { 59 60 public Component getListCellRendererComponent( 61 JList list, Object value, int modelIndex, 62 boolean isSelected, boolean cellHasFocus) { 63 String text= ((String )value).replace('\t', ' '); 64 Component c= super.getListCellRendererComponent(list, text, modelIndex, isSelected, cellHasFocus); 65 setText(text); 66 setToolTipText(text); 67 return c; 68 } 69 } 70 71 74 public Component getComponent() { 75 if (fList == null) { 76 fList= new JList (new StackTraceListModel()); 77 fList.setFont(new Font ("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 88 public void showFailure(TestFailure failure) { 89 getModel().setTrace(BaseTestRunner.getFilteredTrace(failure.trace())); 90 } 91 94 public void clear() { 95 getModel().clear(); 96 } 97 98 private StackTraceListModel getModel() { 99 return (StackTraceListModel)fList.getModel(); 100 } 101 } | Popular Tags |