001: /*
002: * The contents of this file are subject to the Mozilla Public License
003: * Version 1.1 (the "License"); you may not use this file except in
004: * compliance with the License. You may obtain a copy of the License at
005: * http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
009: * License for the specific language governing rights and limitations
010: * under the License.
011: *
012: * The Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
013: *
014: * The Initial Developer of the Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
015: * Portions created by Mark A. Kobold are Copyright (C) 2000-2007. All Rights Reserved.
016: *
017: * Contributor(s):
018: * Mark A. Kobold [mkobold <at> isqlviewer <dot> com].
019: *
020: * If you didn't download this code from the following link, you should check
021: * if you aren't using an obsolete version: http://www.isqlviewer.com
022: */
023: package org.isqlviewer.model;
024:
025: import java.sql.SQLException;
026: import java.util.Vector;
027:
028: import javax.swing.tree.TreePath;
029:
030: import org.isqlviewer.swing.outline.AbstractTreeTableModel;
031: import org.isqlviewer.swing.outline.OutlineModel;
032: import org.isqlviewer.util.LocalMessages;
033:
034: /**
035: * Outline model for showing exceptions in a tree-table fashion.
036: * <p>
037: *
038: * @author Mark A. Kobold <mkobold at isqlviewer dot com>
039: * @version 1.0
040: */
041: public class ThrowableOutlineModel extends AbstractTreeTableModel {
042:
043: private static final Class[] columnTypes = { OutlineModel.class,
044: String.class, String.class };
045: private static final String RESOURCE_BUNDLE = "org.isqlviewer.model.ResourceBundle";
046:
047: private Object root = new Object();
048: private LocalMessages messages = new LocalMessages(RESOURCE_BUNDLE);
049: private Vector<Throwable> errorChain = new Vector<Throwable>();
050:
051: public ThrowableOutlineModel(Throwable error) {
052:
053: errorChain.add(error);
054: Throwable sourceError = error;
055: Throwable nextError = getCause(sourceError);
056: while (nextError != null && nextError != sourceError) {
057: errorChain.add(nextError);
058: sourceError = nextError;
059: nextError = getCause(sourceError);
060: }
061: }
062:
063: public Class getColumnClass(int column) {
064:
065: return columnTypes[column];
066: }
067:
068: public int getColumnCount() {
069:
070: return columnTypes.length;
071: }
072:
073: public String getColumnName(int column) {
074:
075: switch (column) {
076: case 0:
077: return messages
078: .format("ThrowableOutlineModel.stacktrace_column.header");
079: case 1:
080: return messages
081: .format("ThrowableOutlineModel.sqlstate_column.header");
082: case 2:
083: return messages
084: .format("ThrowableOutlineModel.sqlerror_column.header");
085: default:
086: return null;
087: }
088: }
089:
090: public Object getValueAt(Object node, int column) {
091:
092: if (node instanceof SQLException) {
093: SQLException sqlError = (SQLException) node;
094: switch (column) {
095: case 1:
096: return sqlError.getSQLState();
097: case 2:
098: return Integer.toString(sqlError.getErrorCode());
099: }
100: } else {
101: switch (column) {
102: case 1:
103: return "";
104: case 2:
105: return "";
106: }
107: }
108: return node;
109: }
110:
111: public Object getChild(Object parent, int index) {
112:
113: if (parent == root) {
114: return errorChain.elementAt(index);
115: } else if (parent instanceof Throwable) {
116: return ((Throwable) parent).getStackTrace()[index];
117: }
118: return null;
119: }
120:
121: public int getChildCount(Object parent) {
122:
123: if (parent == root) {
124: return errorChain.size();
125: } else if (parent instanceof Throwable) {
126: return ((Throwable) parent).getStackTrace().length;
127: }
128: return 0;
129: }
130:
131: public int getIndexOfChild(Object parent, Object child) {
132:
133: if (parent == root) {
134: return errorChain.indexOf(child);
135: } else if (parent instanceof Throwable) {
136: StackTraceElement[] stackTrace = ((Throwable) parent)
137: .getStackTrace();
138: for (int i = 0; i < stackTrace.length; i++) {
139: String line = stackTrace[i].toString();
140: if (line.equals(child)) {
141: return i;
142: }
143: }
144: }
145: return -1;
146: }
147:
148: public Object getRoot() {
149:
150: return root;
151: }
152:
153: public boolean isLeaf(Object node) {
154:
155: return !(node instanceof Throwable) && node != root;
156: }
157:
158: public void valueForPathChanged(TreePath path, Object newValue) {
159:
160: }
161:
162: private Throwable getCause(Throwable sourceError) {
163:
164: if (sourceError instanceof SQLException) {
165: Throwable cause = ((SQLException) sourceError)
166: .getNextException();
167: if (cause != null) {
168: return cause;
169: }
170: }
171: return sourceError.getCause();
172: }
173: }
|