View Javadoc

1   /**
2    * Copyright (C) 2007 Joern Krueger surething@users.sourceforge.net
3    * 
4    * This program is free software; you can redistribute
5    * it and/or modify it under the terms of the GNU General
6    * Public License version 2 as published by the Free Software
7    * Foundation.
8    *
9    * This program is distributed in the hope that it will be
10   * useful, but WITHOUT ANY WARRANTY; without even the implied
11   * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12   * PURPOSE.  See the GNU General Public License for more
13   * details.
14   *
15   * You should have received a copy of the GNU General Public
16   * License along with this program; if not, write to the
17   * Free Software Foundation, Inc., 59 Temple Place,
18   * Suite 330, Boston, MA  02111-1307  USA
19   */
20  package de.surethingies.ui;
21  
22  import java.awt.Dimension;
23  import java.awt.Frame;
24  import java.awt.GridBagConstraints;
25  import java.awt.GridBagLayout;
26  import java.awt.Insets;
27  import java.awt.event.WindowAdapter;
28  import java.awt.event.WindowEvent;
29  import java.io.ByteArrayOutputStream;
30  import java.io.PrintStream;
31  
32  import javax.swing.JDialog;
33  import javax.swing.JEditorPane;
34  import javax.swing.JScrollPane;
35  
36  @SuppressWarnings("serial")
37  public class ExceptionDialog extends JDialog {
38  
39      public ExceptionDialog(final Frame owner, final Exception e) {
40          super(owner, "An Exception occured...", true);
41          addWindowListener(new WindowAdapter() {
42              public void windowClosing(final WindowEvent e) {
43                  dispose();
44              }
45          });
46  
47          createUI(e);
48  
49          pack();
50          setLocationRelativeTo(owner);
51          setVisible(true);
52      }
53  
54      private void createUI(Exception e) {
55          setSize(new Dimension(640, 256));
56          setLayout(new GridBagLayout());
57          GridBagConstraints c = new GridBagConstraints();
58          c.insets = new Insets(5, 5, 5, 5);
59          c.fill = GridBagConstraints.BOTH;
60          c.gridy = 0;
61          c.gridx = 0;
62          c.weighty = 1.0;
63          c.weightx = 1.0;
64          c.anchor = GridBagConstraints.WEST;
65  
66          JEditorPane stackTrace = new JEditorPane("text/html", getStackTrace(e));
67          stackTrace.setEditable(false);
68  
69          JScrollPane scroller = new JScrollPane(stackTrace);
70          add(scroller, c);
71      }
72  
73      private String getStackTrace(Exception e) {
74          ByteArrayOutputStream out = new ByteArrayOutputStream();
75  
76          e.printStackTrace(new PrintStream(out));
77  
78          return "<html><pre>" + out.toString() + "</pre>";
79      }
80  }