From: Svjatoslav Agejenko Date: Sat, 25 Jun 2022 22:54:59 +0000 (+0300) Subject: Refactored exception dialog. X-Git-Tag: svjatoslavcommons-1.9~3 X-Git-Url: http://www2.svjatoslav.eu/gitweb/?a=commitdiff_plain;h=0bdce2e2b2c16cc9576e8d96c67ce2830c8b0afc;p=svjatoslav_commons.git Refactored exception dialog. --- 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 2b86a6b..d56db29 100755 --- a/src/main/java/eu/svjatoslav/commons/gui/dialog/ExceptionDialog.java +++ b/src/main/java/eu/svjatoslav/commons/gui/dialog/ExceptionDialog.java @@ -7,6 +7,8 @@ package eu.svjatoslav.commons.gui.dialog; import javax.swing.*; import java.awt.*; +import static javax.swing.BoxLayout.Y_AXIS; + public class ExceptionDialog { public ExceptionDialog(final Exception exception) { @@ -31,67 +33,66 @@ public class ExceptionDialog { * @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!"); - 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); - } + frame.getContentPane().add(contentPanel); + frame.setSize(800, 600); + frame.setVisible(true); + frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); + + centerFrameOnScreen(frame); + } + + private void centerFrameOnScreen(JFrame frame) { + final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); + frame.setLocation( + (screenSize.width - frame.getWidth()) / 2, + (screenSize.height - frame.getHeight()) / 2); + } - // build stack trace view - { - final StringBuilder buffer = new StringBuilder(); + private TextArea getStackTraceView(Throwable cause) { + final StringBuilder buffer = new StringBuilder(); + if (cause != null) { // 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); + 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()); } - // finalize frame - frame.getContentPane().add(contentPanel); - frame.setSize(800, 600); - frame.setVisible(true); + 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())); - // Thread.dumpStack(); + // 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; } }