X-Git-Url: http://www2.svjatoslav.eu/gitweb/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Feu%2Fsvjatoslav%2Fcommons%2Fgui%2Fdialog%2FExceptionDialog.java;h=32974f17d00204d5121f7ce616dce2034e962d1f;hb=4620f886089bd4d16a4affa46e05dd4d4688223b;hp=24ba80e87f0e7376f0a74f4ce9cc76bccd41597e;hpb=846af2def5b489670668b978e4ea703c8dfb22b6;p=svjatoslav_commons.git diff --git a/src/main/java/eu/svjatoslav/commons/gui/dialog/ExceptionDialog.java b/src/main/java/eu/svjatoslav/commons/gui/dialog/ExceptionDialog.java index 24ba80e..32974f1 100755 --- a/src/main/java/eu/svjatoslav/commons/gui/dialog/ExceptionDialog.java +++ b/src/main/java/eu/svjatoslav/commons/gui/dialog/ExceptionDialog.java @@ -1,109 +1,90 @@ /* - * Svjatoslav Commons - shared library of common functionality. - * Copyright ©2012-2014, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of version 3 of the GNU Lesser General Public License - * or later as published by the Free Software Foundation. + * Svjatoslav Commons - shared library of common functionality. Author: Svjatoslav Agejenko. + * This project is released under Creative Commons Zero (CC0) license. */ - package eu.svjatoslav.commons.gui.dialog; -import java.awt.BorderLayout; -import java.awt.TextArea; +import javax.swing.*; +import java.awt.*; -import javax.swing.BoxLayout; -import javax.swing.JFrame; -import javax.swing.JLabel; -import javax.swing.JPanel; +import static javax.swing.BoxLayout.Y_AXIS; public class ExceptionDialog { - /** - * This method is for testing - * - * @param args - * commandline arguments - */ - public static void main(final String[] args) { - - final Throwable cause = new Throwable("details....."); - - final Exception exception = new Exception("test", cause); - - new ExceptionDialog(exception); - } - - public ExceptionDialog(final Exception exception) { - showException(exception); - } - - /** - * @param exception - * exception to show - */ - public void showException(final Exception exception) { - - final JFrame frame = new JFrame("Exception occured!"); - final JPanel contentPanel = new JPanel(new BorderLayout()); - final Throwable cause = exception.getCause(); - - // build top panel - { - final JPanel topPanel = new JPanel(); - topPanel.setLayout(new BoxLayout(topPanel, BoxLayout.Y_AXIS)); - - // add exception type - final JLabel exceptionType = new JLabel("Exception type: " - + exception.getClass().getCanonicalName()); - topPanel.add(exceptionType); - - // add error message - final JLabel message = new JLabel("Error message: " - + exception.getMessage()); - topPanel.add(message); - - // add cause message - if (cause != null) - if (cause.getMessage() != null) { - final JLabel message2 = new JLabel("Cause: " - + cause.getMessage()); - topPanel.add(message2); - } - - contentPanel.add(topPanel, BorderLayout.NORTH); - } - - // build stack trace view - { - final StringBuffer buffer = new StringBuffer(); - - // if cause is available, show original stack trace - if (cause != null) { - buffer.append("Stack trace:\n"); - final StackTraceElement[] stackTrace = cause.getStackTrace(); - for (final StackTraceElement stackTraceElement : stackTrace) - buffer.append(stackTraceElement.toString() + "\n"); - } else { - // otherwise show at least current stack trace - buffer.append("Stack trace from original cause is not available.\nShowing current stack trace instead:\n"); - - for (final StackTraceElement stackTraceElement : new Exception( - "Stack trace").getStackTrace()) - buffer.append(stackTraceElement.toString() + "\n"); - } - - final TextArea textArea = new TextArea(buffer.toString()); - contentPanel.add(textArea, BorderLayout.CENTER); - } - - // finalize frame - frame.getContentPane().add(contentPanel); - frame.setSize(800, 600); - frame.setVisible(true); - - // Thread.dumpStack(); - - } + public ExceptionDialog(final Exception exception) { + showException(exception); + } + + /** + * This method is for testing + * + * @param args commandline arguments + */ + public static void main(final String[] args) { + + final Throwable cause = new Throwable("details....."); + + final Exception exception = new Exception("test", cause); + + new ExceptionDialog(exception); + } + + /** + * @param exception exception to show + */ + public void showException(final Exception exception) { + final JPanel contentPanel = new JPanel(new BorderLayout()); + contentPanel.add(getTopPanel(exception), BorderLayout.NORTH); + contentPanel.add(getStackTraceView(exception.getCause()), BorderLayout.CENTER); + + final JFrame frame = new JFrame("Exception occurred!"); + frame.getContentPane().add(contentPanel); + frame.setSize(800, 600); + frame.setVisible(true); + frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); + frame.setLocationRelativeTo(null); // center frame on screen + } + + private TextArea getStackTraceView(Throwable cause) { + final StringBuilder buffer = new StringBuilder(); + + if (cause != null) { + // if cause is available, show original stack trace + buffer.append("Stack trace:\n"); + final StackTraceElement[] stackTrace = cause.getStackTrace(); + enlistStackTraceElements(buffer, stackTrace); + } else { + // otherwise show at least current stack trace + buffer.append("Stack trace from original cause is not available.\n" + + "Showing current stack trace instead:\n"); + enlistStackTraceElements(buffer, new Exception("Stack trace").getStackTrace()); + } + + return new TextArea(buffer.toString()); + } + + private void enlistStackTraceElements(StringBuilder buffer, StackTraceElement[] stackTrace) { + for (final StackTraceElement stackTraceElement : stackTrace) + buffer.append(stackTraceElement.toString()).append("\n"); + } + + private JPanel getTopPanel(Exception exception) { + + final JPanel topPanel = new JPanel(); + topPanel.setLayout(new BoxLayout(topPanel, Y_AXIS)); + + // add exception type + topPanel.add( + new JLabel("Exception type: " + exception.getClass().getCanonicalName())); + + // add error message + topPanel.add(new JLabel("Error message: " + exception.getMessage())); + + // add cause message + Throwable cause = exception.getCause(); + if (cause != null && cause.getMessage() != null) + topPanel.add(new JLabel("Cause: " + cause.getMessage())); + return topPanel; + } }