d56db29590508af94128845b3df8034559e2eb62
[svjatoslav_commons.git] / src / main / java / eu / svjatoslav / commons / gui / dialog / ExceptionDialog.java
1 /*
2  * Svjatoslav Commons - shared library of common functionality. Author: Svjatoslav Agejenko.
3  * This project is released under Creative Commons Zero (CC0) license.
4  */
5 package eu.svjatoslav.commons.gui.dialog;
6
7 import javax.swing.*;
8 import java.awt.*;
9
10 import static javax.swing.BoxLayout.Y_AXIS;
11
12 public class ExceptionDialog {
13
14     public ExceptionDialog(final Exception exception) {
15         showException(exception);
16     }
17
18     /**
19      * This method is for testing
20      *
21      * @param args commandline arguments
22      */
23     public static void main(final String[] args) {
24
25         final Throwable cause = new Throwable("details.....");
26
27         final Exception exception = new Exception("test", cause);
28
29         new ExceptionDialog(exception);
30     }
31
32     /**
33      * @param exception exception to show
34      */
35     public void showException(final Exception exception) {
36         final JPanel contentPanel = new JPanel(new BorderLayout());
37         contentPanel.add(getTopPanel(exception), BorderLayout.NORTH);
38         contentPanel.add(getStackTraceView(exception.getCause()), BorderLayout.CENTER);
39
40         final JFrame frame = new JFrame("Exception occurred!");
41         frame.getContentPane().add(contentPanel);
42         frame.setSize(800, 600);
43         frame.setVisible(true);
44         frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
45
46         centerFrameOnScreen(frame);
47     }
48
49     private void centerFrameOnScreen(JFrame frame) {
50         final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
51         frame.setLocation(
52                 (screenSize.width - frame.getWidth()) / 2,
53                 (screenSize.height - frame.getHeight()) / 2);
54     }
55
56     private TextArea getStackTraceView(Throwable cause) {
57         final StringBuilder buffer = new StringBuilder();
58
59         if (cause != null) {
60             // if cause is available, show original stack trace
61             buffer.append("Stack trace:\n");
62             final StackTraceElement[] stackTrace = cause.getStackTrace();
63             enlistStackTraceElements(buffer, stackTrace);
64         } else {
65             // otherwise show at least current stack trace
66             buffer.append("Stack trace from original cause is not available.\n" +
67                     "Showing current stack trace instead:\n");
68             enlistStackTraceElements(buffer, new Exception("Stack trace").getStackTrace());
69         }
70
71         return new TextArea(buffer.toString());
72     }
73
74     private void enlistStackTraceElements(StringBuilder buffer, StackTraceElement[] stackTrace) {
75         for (final StackTraceElement stackTraceElement : stackTrace)
76             buffer.append(stackTraceElement.toString()).append("\n");
77     }
78
79     private JPanel getTopPanel(Exception exception) {
80
81         final JPanel topPanel = new JPanel();
82         topPanel.setLayout(new BoxLayout(topPanel, Y_AXIS));
83
84         // add exception type
85         topPanel.add(
86                 new JLabel("Exception type: " + exception.getClass().getCanonicalName()));
87
88         // add error message
89         topPanel.add(new JLabel("Error message: " + exception.getMessage()));
90
91         // add cause message
92         Throwable cause = exception.getCause();
93         if (cause != null && cause.getMessage() != null)
94             topPanel.add(new JLabel("Cause: " + cause.getMessage()));
95         return topPanel;
96     }
97
98 }