001    /*
002     @license.text@
003      */
004    package biz.hammurapi.swing;
005    
006    import java.util.ArrayList;
007    import java.util.List;
008    
009    import javax.swing.table.DefaultTableModel;
010    import javax.swing.table.TableModel;
011    import javax.swing.tree.TreeNode;
012    
013    /**
014     * @author Pavel Vlasov
015     *
016     * @version $Revision: 1.1 $
017     */
018    public class ThrowableVisualizer {
019            
020            
021            public Visualizable convert(final Throwable th) {
022                    return new Visualizable() {
023    
024                            public TreeNode toTreeNode(TreeNode parent, String title) {
025                                    return new LazyTreeNodeTableVisualizable(parent, title) {
026    
027                                            protected List loadChildren() {
028                                                    ArrayList ret = new ArrayList();
029                                                    StackTraceElement[] st = th.getStackTrace();
030                                                for (int i=0; i<st.length; i++) {
031                                                    TreeNode ev=ThrowableVisualizer.this.toTreeNode(this, "frame", st[i]);
032                                                    if (ev!=null) {
033                                                            ret.add(ev);
034                                                    }
035                                                }                                                   
036                                                    return ret;
037                                            }
038    
039                                            public TableModel toTable() {
040                                                    DefaultTableModel tm=new DefaultTableModel(th.getCause()==null ? 2 : 3, 2);
041                                                    tm.setColumnIdentifiers(new String[] {"Property", "Value"});
042                                                    tm.setValueAt("Class", 0, 0);
043                                                    tm.setValueAt(th.getClass().getName(), 0, 1);
044                                                    tm.setValueAt("Message", 1, 0);
045                                                    tm.setValueAt(th.getMessage(), 1, 1);
046                                                    
047                                                    if (th.getCause()!=null) {
048                                                            tm.setValueAt("Cause", 2, 0);
049                                                            tm.setValueAt(th.getCause(), 2, 1);                                     
050                                                    }
051                                                    
052                                                    return tm;
053                                            }
054                                            
055                                    };
056                            }
057                            
058                    };
059            }
060    
061            private TreeNode toTreeNode(final TreeNode parent, final String title, final StackTraceElement element) {
062                    return new LazyTreeNodeTableVisualizable(parent, title) {
063    
064                            protected List loadChildren() {
065                                    return new ArrayList();
066                            }
067    
068                            public TableModel toTable() {
069                                    DefaultTableModel tm=new DefaultTableModel(5, 2);
070                                    tm.setColumnIdentifiers(new String[] {"Property", "Value"});
071                                    tm.setValueAt("Class", 0, 0);
072                                    tm.setValueAt(element.getClassName(), 0, 1);
073                                    
074                                    tm.setValueAt("File", 1, 0);
075                                    tm.setValueAt(element.getFileName(), 1, 1);
076                                    
077                                    tm.setValueAt("Method", 2, 0);
078                                    tm.setValueAt(element.getMethodName(), 2, 1);
079                                    
080                                    tm.setValueAt("Line", 3, 0);
081                                    tm.setValueAt(String.valueOf(element.getLineNumber()), 3, 1);
082                                    
083                                    tm.setValueAt("Native", 4, 0);
084                                    tm.setValueAt(element.isNativeMethod() ? "yes" : "no", 4, 1);
085                                    
086                                    
087                                    return tm;
088                            }
089                            
090                    };
091            }
092    }