01: package net.suberic.util.swing;
02:
03: import javax.swing.*;
04: import java.awt.*;
05: import java.awt.event.*;
06: import java.io.*;
07:
08: /**
09: * A Display panel which has a button which, on pressing, will display
10: * the stack trace for the given exception.
11: */
12: public class ExceptionDisplayPanel extends JPanel {
13:
14: // the Exception whose stack trace will be displayed.
15: Exception mException;
16:
17: // the Button
18: JButton mButton;
19:
20: /**
21: * Creates the ExceptionDisplayPanel using the given text for the
22: * button and the given exception.
23: */
24: public ExceptionDisplayPanel(String pButtonText,
25: Exception pException) {
26: super ();
27:
28: this .setLayout(new CardLayout());
29:
30: mException = pException;
31:
32: mButton = new JButton(pButtonText);
33:
34: Box buttonBox = Box.createHorizontalBox();
35: buttonBox.add(Box.createHorizontalGlue());
36: buttonBox.add("BUTTON", mButton);
37: buttonBox.add(Box.createHorizontalGlue());
38:
39: this .add("BUTTON", buttonBox);
40:
41: mButton.addActionListener(new AbstractAction() {
42:
43: public void actionPerformed(ActionEvent ae) {
44: showStackTrace();
45: }
46: });
47: }
48:
49: /**
50: * Expands the display to show the stack trace for the exception.
51: */
52: public void showStackTrace() {
53: // first make the stack trace.
54: StringWriter exceptionWriter = new StringWriter();
55: mException.printStackTrace(new PrintWriter(exceptionWriter));
56: String exceptionString = exceptionWriter.toString();
57:
58: // now make the display location.
59: JTextArea jta = new JTextArea(exceptionString);
60: jta.setEditable(false);
61: JScrollPane jsp = new JScrollPane(jta);
62:
63: this .add("EXCEPTION", jsp);
64:
65: ((CardLayout) getLayout()).show(this , "EXCEPTION");
66: Dimension currentMinimum = getMinimumSize();
67: this .setMinimumSize(new Dimension(Math.max(
68: currentMinimum.width, 150), Math.max(
69: currentMinimum.height, 100)));
70:
71: JInternalFrame parentIntFrame = null;
72: try {
73: parentIntFrame = (JInternalFrame) SwingUtilities
74: .getAncestorOfClass(Class
75: .forName("javax.swing.JInternalFrame"),
76: this );
77: } catch (Exception e) {
78: }
79: if (parentIntFrame != null) {
80: parentIntFrame.pack();
81: //parentIntFrame.resize();
82: } else {
83: Window parentWindow = SwingUtilities
84: .getWindowAncestor(this );
85: if (parentWindow != null) {
86: parentWindow.pack();
87: //parentWindow.resize();
88: }
89: }
90: }
91: }
|