001: /* ====================================================================
002: * The JRefactory License, Version 1.0
003: *
004: * Copyright (c) 2001 JRefactory. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * 3. The end-user documentation included with the redistribution,
019: * if any, must include the following acknowledgment:
020: * "This product includes software developed by the
021: * JRefactory (http://www.sourceforge.org/projects/jrefactory)."
022: * Alternately, this acknowledgment may appear in the software itself,
023: * if and wherever such third-party acknowledgments normally appear.
024: *
025: * 4. The names "JRefactory" must not be used to endorse or promote
026: * products derived from this software without prior written
027: * permission. For written permission, please contact seguin@acm.org.
028: *
029: * 5. Products derived from this software may not be called "JRefactory",
030: * nor may "JRefactory" appear in their name, without prior written
031: * permission of Chris Seguin.
032: *
033: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
034: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
035: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
036: * DISCLAIMED. IN NO EVENT SHALL THE CHRIS SEGUIN OR
037: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
038: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
039: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
040: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
041: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
042: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
043: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
044: * SUCH DAMAGE.
045: * ====================================================================
046: *
047: * This software consists of voluntary contributions made by many
048: * individuals on behalf of JRefactory. For more information on
049: * JRefactory, please see
050: * <http://www.sourceforge.org/projects/jrefactory>.
051: */
052: package org.acm.seguin.awt;
053:
054: import java.awt.Dimension;
055: import java.awt.Frame;
056: import java.awt.GridLayout;
057: import java.awt.Toolkit;
058: import java.awt.event.ActionEvent;
059: import java.awt.event.ActionListener;
060: import java.io.PrintWriter;
061: import java.io.StringWriter;
062: import java.util.Iterator;
063: import java.util.LinkedList;
064: import java.util.StringTokenizer;
065: import javax.swing.JButton;
066: import javax.swing.JDialog;
067: import javax.swing.JLabel;
068: import javax.swing.JPanel;
069: import javax.swing.JScrollPane;
070:
071: /**
072: * The exception dialog box. Displays the message in one scroll pane and the
073: * stack trace in another.
074: *
075: *@author Chris Seguin
076: *@created October 18, 2001
077: */
078: public class ExceptionDialog extends JDialog implements ActionListener {
079: /**
080: * Constructor for the ExceptionDialog object
081: *
082: *@param thrown Description of Parameter
083: */
084: public ExceptionDialog(Throwable thrown) {
085: this (thrown, null, true);
086: }
087:
088: /**
089: * Constructor for the ExceptionDialog object
090: *
091: *@param thrown Description of Parameter
092: *@param parent Description of Parameter
093: */
094: public ExceptionDialog(Throwable thrown, Frame parent) {
095: this (thrown, parent, true);
096: }
097:
098: /**
099: * Constructor for the ExceptionDialog object
100: *
101: *@param thrown Description of Parameter
102: *@param parent Description of Parameter
103: *@param modal Description of Parameter
104: */
105: public ExceptionDialog(Throwable thrown, Frame parent, boolean modal) {
106: super (parent, modal);
107: setTitle("Exception thrown");
108: setResizable(false);
109:
110: getContentPane().setLayout(null);
111:
112: JLabel sizer = new JLabel(
113: "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
114: Dimension size = sizer.getPreferredSize();
115: int scrollHeight = 6 * size.height;
116: Dimension scrollSize = new Dimension(size.width, scrollHeight);
117:
118: JScrollPane messagePane = new JScrollPane(
119: createPanel(untab(thrown.getMessage())));
120: messagePane.setPreferredSize(scrollSize);
121: messagePane.setSize(scrollSize);
122: messagePane.setLocation(10, 10);
123: getContentPane().add(messagePane);
124:
125: JScrollPane stackPane = new JScrollPane(
126: createStackTrace(thrown));
127: stackPane.setPreferredSize(scrollSize);
128: stackPane.setSize(scrollSize);
129: stackPane.setLocation(10, 20 + scrollHeight);
130: getContentPane().add(stackPane);
131:
132: JButton ok = new JButton("OK");
133: Dimension okSize = ok.getPreferredSize();
134: ok.setSize(okSize);
135: ok.setLocation((20 + size.width - okSize.width) / 2,
136: 30 + scrollHeight * 2);
137: getContentPane().add(ok);
138: ok.addActionListener(this );
139:
140: int wide = 20 + size.width;
141: int high = 90 + scrollHeight * 2;
142: setSize(wide, high);
143:
144: CenterDialog.center(this );
145: }
146:
147: /**
148: * The user has pressed the OK button
149: *
150: *@param evt the OK button event
151: */
152: public void actionPerformed(ActionEvent evt) {
153: dispose();
154: }
155:
156: /**
157: * Creates a panel based on a string
158: *
159: *@param value the string
160: *@return the panel
161: */
162: private JPanel createPanel(String value) {
163: LinkedList list = new LinkedList();
164: StringTokenizer tok = new StringTokenizer(value, "\n\r");
165: while (tok.hasMoreTokens()) {
166: list.add(tok.nextToken());
167: }
168:
169: JPanel result = new JPanel();
170: result.setLayout(new GridLayout(list.size(), 1));
171: Iterator iter = list.iterator();
172: while (iter.hasNext()) {
173: JLabel label = new JLabel((String) iter.next());
174: result.add(label);
175: }
176:
177: return result;
178: }
179:
180: /**
181: * Creates a panel containing the stack trace of the exception
182: *
183: *@param thrown the exception
184: *@return the panel
185: */
186: private JPanel createStackTrace(Throwable thrown) {
187: StringWriter stringWriter = new StringWriter();
188: PrintWriter output = new PrintWriter(stringWriter);
189: thrown.printStackTrace(output);
190: output.close();
191:
192: return createPanel(untab(stringWriter.toString()));
193: }
194:
195: /**
196: * The main program for the ExceptionDialog class. This program is used to
197: * test this dialog box.
198: *
199: *@param args The command line arguments
200: */
201: public static void main(String[] args) {
202: (new ExceptionDialog(
203: new Exception(
204: "Here is\na six line\nerror\nmesage\nfor you to\nread.\n \nDo you enjoy?")))
205: .setVisible(true);
206: }
207:
208: /**
209: * Description of the Method
210: *
211: *@param value Description of Parameter
212: *@return Description of the Returned Value
213: */
214: private String untab(String value) {
215: StringBuffer buffer = new StringBuffer();
216: int last = value.length();
217:
218: for (int ndx = 0; ndx < last; ndx++) {
219: if (value.charAt(ndx) == '\t') {
220: buffer.append(" ");
221: } else {
222: buffer.append(value.charAt(ndx));
223: }
224: }
225:
226: return buffer.toString();
227: }
228: }
229: // EOF
|