001: /*
002: * hgcommons 7
003: * Hammurapi Group Common Library
004: * Copyright (C) 2003 Hammurapi Group
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * URL: http://www.hammurapi.biz/hammurapi-biz/ef/xmenu/hammurapi-group/products/products/hgcommons/index.html
021: * e-Mail: support@hammurapi.biz
022: */
023: package biz.hammurapi.swing;
024:
025: import javax.swing.table.DefaultTableModel;
026: import javax.swing.table.TableModel;
027: import javax.swing.tree.DefaultMutableTreeNode;
028: import javax.swing.tree.MutableTreeNode;
029:
030: /**
031: * @author Pavel Vlasov
032: *
033: * @version $Revision: 1.1 $
034: */
035: public class ThrowableVisualizer {
036:
037: public Visualizable toVisualizable(final Throwable th) {
038: return new Visualizable() {
039:
040: public MutableTreeNode toTree(final String title) {
041: DefaultMutableTreeNode ret = new DefaultMutableTreeNode(
042: th) {
043: public String toString() {
044: return title + " [" + th.getClass().getName()
045: + "] " + th.getMessage();
046: }
047: };
048:
049: StackTraceElement[] st = th.getStackTrace();
050: for (int i = 0; i < st.length; i++) {
051: Visualizable ev = toVisualizable(st[i]);
052: if (ev != null) {
053: ret.add(ev.toTree(null));
054: }
055: }
056:
057: if (th.getCause() != null) {
058: Visualizable v = toVisualizable(th.getCause());
059: ret.add(v.toTree("cause"));
060: }
061:
062: return ret;
063: }
064:
065: public TableModel toTable() {
066: DefaultTableModel tm = new DefaultTableModel(th
067: .getCause() == null ? 2 : 3, 2);
068: tm.setColumnIdentifiers(new String[] { "Property",
069: "Value" });
070: tm.setValueAt("Class", 0, 0);
071: tm.setValueAt(th.getClass().getName(), 0, 1);
072: tm.setValueAt("Message", 1, 0);
073: tm.setValueAt(th.getMessage(), 1, 1);
074:
075: if (th.getCause() != null) {
076: tm.setValueAt("Cause", 2, 0);
077: tm.setValueAt(th.getCause(), 2, 1);
078: }
079:
080: return tm;
081: }
082:
083: };
084: }
085:
086: public Visualizable toVisualizable(final StackTraceElement element) {
087: return new Visualizable() {
088:
089: public MutableTreeNode toTree(final String title) {
090: DefaultMutableTreeNode ret = new DefaultMutableTreeNode(
091: element) {
092: public String toString() {
093: return element.toString();
094: }
095: };
096:
097: return ret;
098: }
099:
100: public TableModel toTable() {
101: DefaultTableModel tm = new DefaultTableModel(5, 2);
102: tm.setColumnIdentifiers(new String[] { "Property",
103: "Value" });
104: tm.setValueAt("Class", 0, 0);
105: tm.setValueAt(element.getClassName(), 0, 1);
106:
107: tm.setValueAt("File", 1, 0);
108: tm.setValueAt(element.getFileName(), 1, 1);
109:
110: tm.setValueAt("Method", 2, 0);
111: tm.setValueAt(element.getMethodName(), 2, 1);
112:
113: tm.setValueAt("Line", 3, 0);
114: tm.setValueAt(String.valueOf(element.getLineNumber()),
115: 3, 1);
116:
117: tm.setValueAt("Native", 4, 0);
118: tm.setValueAt(element.isNativeMethod() ? "yes" : "no",
119: 4, 1);
120:
121: return tm;
122: }
123:
124: };
125: }
126: }
|