improved javadoc
[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          * @param args
26          *            commandline arguments
27          */
28         public static void main(final String[] args) {
29
30                 final Throwable cause = new Throwable("details.....");
31
32                 final Exception exception = new Exception("test", cause);
33
34                 new ExceptionDialog(exception);
35         }
36
37         public ExceptionDialog(final Exception exception) {
38                 showException(exception);
39         }
40
41         /**
42          * @param exception
43          *            exception to show
44          */
45         public void showException(final Exception exception) {
46
47                 final JFrame frame = new JFrame("Exception occured!");
48                 final JPanel contentPanel = new JPanel(new BorderLayout());
49                 final Throwable cause = exception.getCause();
50
51                 // build top panel
52                 {
53                         final JPanel topPanel = new JPanel();
54                         topPanel.setLayout(new BoxLayout(topPanel, BoxLayout.Y_AXIS));
55
56                         // add exception type
57                         final JLabel exceptionType = new JLabel("Exception type: "
58                                         + exception.getClass().getCanonicalName());
59                         topPanel.add(exceptionType);
60
61                         // add error message
62                         final JLabel message = new JLabel("Error message: "
63                                         + exception.getMessage());
64                         topPanel.add(message);
65
66                         // add cause message
67                         if (cause != null)
68                                 if (cause.getMessage() != null) {
69                                         final JLabel message2 = new JLabel("Cause: "
70                                                         + cause.getMessage());
71                                         topPanel.add(message2);
72                                 }
73
74                         contentPanel.add(topPanel, BorderLayout.NORTH);
75                 }
76
77                 // build stack trace view
78                 {
79                         final StringBuffer buffer = new StringBuffer();
80
81                         // if cause is available, show original stack trace
82                         if (cause != null) {
83                                 buffer.append("Stack trace:\n");
84                                 final StackTraceElement[] stackTrace = cause.getStackTrace();
85                                 for (final StackTraceElement stackTraceElement : stackTrace)
86                                         buffer.append(stackTraceElement.toString() + "\n");
87                         } else {
88                                 // otherwise show at least current stack trace
89                                 buffer.append("Stack trace from original cause is not available.\nShowing current stack trace instead:\n");
90
91                                 for (final StackTraceElement stackTraceElement : new Exception(
92                                                 "Stack trace").getStackTrace())
93                                         buffer.append(stackTraceElement.toString() + "\n");
94                         }
95
96                         final TextArea textArea = new TextArea(buffer.toString());
97                         contentPanel.add(textArea, BorderLayout.CENTER);
98                 }
99
100                 // finalize frame
101                 frame.getContentPane().add(contentPanel);
102                 frame.setSize(800, 600);
103                 frame.setVisible(true);
104
105                 // Thread.dumpStack();
106
107         }
108
109 }