KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > suberic > util > swing > ExceptionDisplayPanel


1 package net.suberic.util.swing;
2
3 import javax.swing.*;
4 import java.awt.*;
5 import java.awt.event.*;
6 import java.io.*;
7
8 /**
9  * 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   private static int S_INDENT = 10;
15
16   // the Exception whose stack trace will be displayed.
17
Exception JavaDoc mException;
18
19   // the Button
20
JButton mButton;
21
22   /**
23    * Creates the ExceptionDisplayPanel using the given text for the
24    * button and the given exception.
25    */

26   public ExceptionDisplayPanel(String JavaDoc pButtonText, Exception JavaDoc pException) {
27     super();
28
29     this.setLayout(new CardLayout());
30
31     mException = pException;
32
33     mButton = new JButton(pButtonText);
34
35     Box buttonBox = Box.createHorizontalBox();
36     buttonBox.add(Box.createHorizontalGlue());
37     buttonBox.add("BUTTON", mButton);
38     buttonBox.add(Box.createHorizontalGlue());
39
40     this.add("BUTTON", buttonBox);
41
42     mButton.addActionListener(new AbstractAction() {
43
44         public void actionPerformed(ActionEvent ae) {
45           showStackTrace();
46         }
47       });
48   }
49
50   /**
51    * Expands the display to show the stack trace for the exception.
52    */

53   public void showStackTrace() {
54     // first make the stack trace.
55
StringWriter exceptionWriter = new StringWriter();
56     mException.printStackTrace(new PrintWriter(exceptionWriter));
57     String JavaDoc exceptionString = exceptionWriter.toString();
58
59     // now make the display location.
60
//JTextArea jta = new JTextArea(exceptionString);
61
//jta.setEditable(false);
62
//JScrollPane jsp = new JScrollPane(jta);
63

64     /*
65     jsp.setMaximumSize(new Dimension(this.getSize().width, Integer.MAX_VALUE));
66     jsp.setPreferredSize(new Dimension(Math.min(jsp.getPreferredSize().width, jsp.getMaximumSize().width), jsp.getPreferredSize().height));
67     */

68     //jsp.setMaximumSize(new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE));
69
//jsp.setPreferredSize(new Dimension(jsp.getPreferredSize().width, jsp.getPreferredSize().height));
70
//this.add("EXCEPTION", jsp);
71

72     Window windowAncestor = SwingUtilities.getWindowAncestor(this);
73     if (windowAncestor instanceof JDialog) {
74       JDialog dialog = (JDialog) windowAncestor;
75       Window owner = dialog.getOwner();
76       if (owner != null)
77         windowAncestor = owner;
78     }
79     Component jsp = createMessageComponent(exceptionString,windowAncestor );
80     this.add("EXCEPTION", jsp);
81
82
83     ((CardLayout) getLayout()).show(this, "EXCEPTION");
84     Dimension currentMinimum = getMinimumSize();
85     this.setMinimumSize(new Dimension(Math.max(currentMinimum.width, 150), Math.max(currentMinimum.height, 100)));
86
87     JInternalFrame parentIntFrame = null;
88     try {
89       parentIntFrame = (JInternalFrame) SwingUtilities.getAncestorOfClass(Class.forName("javax.swing.JInternalFrame"), this);
90     } catch (Exception JavaDoc e) {
91     }
92     if (parentIntFrame != null) {
93       // make sure we don't resize to be bigger than the JDesktopPane.
94
JDesktopPane jdp = parentIntFrame.getDesktopPane();
95       System.err.println("got jdp.");
96       if (jdp != null) {
97         System.err.println("jdp is not null.");
98         Point iFrameLocation = parentIntFrame.getLocation();
99         Dimension jdpSize = jdp.getSize();
100         System.err.println("iFrameLocation = " + iFrameLocation + ", jdpSize = " + jdpSize + ", parentIntFrame.getMinimumSize() = " + parentIntFrame.getMinimumSize());
101         parentIntFrame.setMaximumSize(new Dimension(Math.max(parentIntFrame.getMinimumSize().width, jdpSize.width - iFrameLocation.x), Math.max(parentIntFrame.getMinimumSize().height, jdpSize.height - iFrameLocation.y)));
102         parentIntFrame.setPreferredSize(new Dimension(Math.max(parentIntFrame.getMinimumSize().width, jdpSize.width - iFrameLocation.x), Math.max(parentIntFrame.getMinimumSize().height, jdpSize.height - iFrameLocation.y)));
103         System.err.println("parentIntFrame.maximumSize = " + parentIntFrame.getMaximumSize());
104       }
105       parentIntFrame.pack();
106       //parentIntFrame.resize();
107
} else {
108       Window parentWindow = SwingUtilities.getWindowAncestor(this);
109       if (parentWindow != null) {
110         if (parentWindow instanceof JDialog) {
111           JDialog parentDialog = (JDialog) parentWindow;
112           parentDialog.setSize(parentDialog.getPreferredSize());
113           Window owner = parentDialog.getOwner();
114           if (owner != null) {
115             Point ownerPoint = owner.getLocationOnScreen();
116             Dimension ownerSize = owner.getSize();
117           }
118         }
119         parentWindow.pack();
120         //parentWindow.resize();
121
}
122     }
123   }
124
125   /**
126    * Calculates a Dimension which defines a reasonably sized dialog window.
127    */

128   public Dimension calculateDisplaySize(Component parentComponent, Component displayComponent) {
129     //Point parentLocation = parentComponent.getLocationOnScreen();
130
Dimension parentSize = parentComponent.getSize();
131     // width and height should be mo more than 80%
132
int maxWidth = Math.max(30, (int) (parentSize.width * 0.8));
133     int maxHeight = Math.max(30, (int) (parentSize.height * 0.8));
134
135     Dimension displayPrefSize = displayComponent.getPreferredSize();
136
137     int newWidth = Math.min(maxWidth, displayPrefSize.width);
138     int newHeight = Math.min(maxHeight, displayPrefSize.height);
139     return new Dimension(newWidth +5, newHeight+5);
140   }
141
142   /**
143    * Returns either a properly-sized JLabel, or a JLabel inside of a
144    * properly-sized JScrollPane.
145    */

146   public Component createMessageComponent(String JavaDoc message, Component parentComponent) {
147     Component labelComponent = createLabel(message);
148     Dimension displaySize = calculateDisplaySize(parentComponent, labelComponent);
149     JScrollPane jsp = new JScrollPane(labelComponent);
150     // add on space for the scrollbar.
151
JScrollBar jsb = jsp.getVerticalScrollBar();
152     if (jsb != null) {
153       displaySize = new Dimension(displaySize.width + jsb.getPreferredSize().width, displaySize.height);
154     } else {
155       jsb = new JScrollBar(JScrollBar.VERTICAL);
156       displaySize = new Dimension(displaySize.width + jsb.getPreferredSize().width, displaySize.height);
157
158     }
159     jsp.setPreferredSize(displaySize);
160     return jsp;
161   }
162
163   /**
164    * Breaks up a String into proper JLabels.
165    */

166   public Component createLabel(String JavaDoc s) {
167     Container c = Box.createVerticalBox();
168
169     addMessageComponents(c, s, 160);
170
171     return c;
172   }
173
174   private void addMessageComponents(Container c, String JavaDoc s, int maxll) {
175     // taken from BasicOptionPaneUI
176
int nl = -1;
177     int nll = 0;
178
179     if ((nl = s.indexOf("\r\n")) >= 0) {
180       nll = 2;
181     } else if ((nl = s.indexOf('\n')) >= 0) {
182       nll = 1;
183     }
184
185     if (nl >= 0) {
186       // break up newlines
187
if (nl == 0) {
188         JPanel breakPanel = new JPanel() {
189             public Dimension getPreferredSize() {
190               Font f = getFont();
191
192               if (f != null) {
193                 return new Dimension(1, f.getSize() + 2);
194               }
195               return new Dimension(0, 0);
196             }
197           };
198         breakPanel.setName("OptionPane.break");
199         c.add(breakPanel);
200       } else {
201         addMessageComponents(c, s.substring(0, nl), maxll);
202       }
203       addMessageComponents(c, s.substring(nl + nll), maxll);
204
205       /*
206     } else if (len > maxll) {
207       Container c = Box.createVerticalBox();
208       c.setName("OptionPane.verticalBox");
209       burstStringInto(c, s, maxll);
210       addMessageComponents(container, cons, c, maxll, true );
211       */

212     } else {
213       JLabel label;
214       label = new JLabel(s, JLabel.LEADING );
215       label.setName("OptionPane.label");
216       c.add(label);
217     }
218   }
219
220   /**
221    * Recursively creates new JLabel instances to represent <code>d</code>.
222    * Each JLabel instance is added to <code>c</code>.
223    */

224   private void burstStringInto(Container c, String JavaDoc d, int maxll) {
225     // Primitive line wrapping
226
int len = d.length();
227     if (len <= 0)
228       return;
229     if (len > maxll) {
230       int p = d.lastIndexOf(' ', maxll);
231       if (p <= 0)
232         p = d.indexOf(' ', maxll);
233       if (p > 0 && p < len) {
234         burstStringInto(c, d.substring(0, p), maxll);
235         burstStringInto(c, d.substring(p + 1), maxll);
236         return;
237       }
238     }
239     JLabel label = new JLabel(d, JLabel.LEFT);
240     label.setName("OptionPane.label");
241     c.add(label);
242   }
243
244 }
245
246
Popular Tags