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