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