Changed license to LGPLv3 or later.
[svjatoslav_commons.git] / src / main / java / eu / svjatoslav / commons / gui / dialog / ExceptionDialog.java
1 /*
2  * Svjatoslav Commons - shared library of common functionality.
3  * Copyright ©2012-2014, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
4  * 
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of version 3 of the GNU Lesser General Public License
7  * or later as published by the Free Software Foundation.
8  */
9
10 package eu.svjatoslav.commons.gui.dialog;
11
12 import java.awt.BorderLayout;
13 import java.awt.TextArea;
14
15 import javax.swing.BoxLayout;
16 import javax.swing.JFrame;
17 import javax.swing.JLabel;
18 import javax.swing.JPanel;
19
20 public class ExceptionDialog {
21
22         /**
23          * This method is for testing
24          */
25         public static void main(final String[] args) {
26
27                 final Throwable cause = new Throwable("details.....");
28
29                 final Exception exception = new Exception("test", cause);
30
31                 new ExceptionDialog(exception);
32         }
33
34         public ExceptionDialog(final Exception exception) {
35                 showException(exception);
36         }
37
38         /**
39          * @param exception
40          *            exception to show
41          */
42         public void showException(final Exception exception) {
43
44                 final JFrame frame = new JFrame("Exception occured!");
45                 final JPanel contentPanel = new JPanel(new BorderLayout());
46                 final Throwable cause = exception.getCause();
47
48                 // build top panel
49                 {
50                         final JPanel topPanel = new JPanel();
51                         topPanel.setLayout(new BoxLayout(topPanel, BoxLayout.Y_AXIS));
52
53                         // add exception type
54                         final JLabel exceptionType = new JLabel("Exception type: "
55                                         + exception.getClass().getCanonicalName());
56                         topPanel.add(exceptionType);
57
58                         // add error message
59                         final JLabel message = new JLabel("Error message: "
60                                         + exception.getMessage());
61                         topPanel.add(message);
62
63                         // add cause message
64                         if (cause != null)
65                                 if (cause.getMessage() != null) {
66                                         final JLabel message2 = new JLabel("Cause: "
67                                                         + cause.getMessage());
68                                         topPanel.add(message2);
69                                 }
70
71                         contentPanel.add(topPanel, BorderLayout.NORTH);
72                 }
73
74                 // build stack trace view
75                 {
76                         final StringBuffer buffer = new StringBuffer();
77
78                         // if cause is available, show original stack trace
79                         if (cause != null) {
80                                 buffer.append("Stack trace:\n");
81                                 final StackTraceElement[] stackTrace = cause.getStackTrace();
82                                 for (final StackTraceElement stackTraceElement : stackTrace)
83                                         buffer.append(stackTraceElement.toString() + "\n");
84                         } else {
85                                 // otherwise show at least current stack trace
86                                 buffer.append("Stack trace from original cause is not available.\nShowing current stack trace instead:\n");
87
88                                 for (final StackTraceElement stackTraceElement : new Exception(
89                                                 "Stack trace").getStackTrace())
90                                         buffer.append(stackTraceElement.toString() + "\n");
91                         }
92
93                         final TextArea textArea = new TextArea(buffer.toString());
94                         contentPanel.add(textArea, BorderLayout.CENTER);
95                 }
96
97                 // finalize frame
98                 frame.getContentPane().add(contentPanel);
99                 frame.setSize(800, 600);
100                 frame.setVisible(true);
101
102                 // Thread.dumpStack();
103
104         }
105
106 }