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