| 1 | /* |
| 2 | * $Id: UnhandledExceptionDialog.java,v 1.2 2005/12/03 07:52:34 hastings Exp $ |
| 3 | * |
| 4 | * (c) Copyright, Moebius Solutions, Inc., 2004 |
| 5 | * |
| 6 | * All Rights Reserved |
| 7 | * |
| 8 | * This material may be reproduced by or for the U. S. Government |
| 9 | * pursuant to the copyright license under the clause at |
| 10 | * DFARS 252.227-7014 (OCT 2001). |
| 11 | */ |
| 12 | |
| 13 | package com.moesol.util; |
| 14 | |
| 15 | import java.awt.EventQueue; |
| 16 | import java.io.PrintWriter; |
| 17 | import java.io.StringWriter; |
| 18 | |
| 19 | import javax.swing.JDialog; |
| 20 | import javax.swing.JEditorPane; |
| 21 | import javax.swing.JOptionPane; |
| 22 | import javax.swing.JScrollPane; |
| 23 | |
| 24 | /** |
| 25 | * @author robert |
| 26 | */ |
| 27 | public class UnhandledExceptionDialog { |
| 28 | /* |
| 29 | * Posts the exception dialog from the awt thread. |
| 30 | */ |
| 31 | public static void showExceptionLater(final Throwable t) { |
| 32 | EventQueue.invokeLater(new Runnable() { |
| 33 | public void run() { |
| 34 | showException(t); |
| 35 | } |
| 36 | }); |
| 37 | } |
| 38 | /* |
| 39 | * Shows the exception in an unhandled exception dialog. |
| 40 | * asserts that it is called from the awt thread. |
| 41 | */ |
| 42 | public static void showException(Throwable t) { |
| 43 | assert(EventQueue.isDispatchThread()); |
| 44 | try { |
| 45 | safeShowException(t); |
| 46 | } catch (Throwable e) { |
| 47 | // Since we are in the process or reporting |
| 48 | // an exception generating another exception |
| 49 | // now could cause an infinite loop of exceptions! |
| 50 | e.printStackTrace(); |
| 51 | } |
| 52 | } |
| 53 | private static void safeShowException(Throwable t) { |
| 54 | Object[] options = { "OK", "Details..." }; |
| 55 | int selectedOption = |
| 56 | JOptionPane.showOptionDialog( |
| 57 | null, |
| 58 | t, |
| 59 | "Unhandled Exception", |
| 60 | JOptionPane.DEFAULT_OPTION, |
| 61 | JOptionPane.ERROR_MESSAGE, |
| 62 | null, |
| 63 | options, |
| 64 | options[0]); |
| 65 | if (selectedOption == 1) { |
| 66 | showExceptionDetails(t); |
| 67 | } |
| 68 | } |
| 69 | private static void showExceptionDetails(Throwable t) { |
| 70 | // create |
| 71 | JDialog details_dlg = new JDialog(); |
| 72 | JEditorPane msg_pane = new JEditorPane(); |
| 73 | JScrollPane scroll_pane = new JScrollPane(); |
| 74 | |
| 75 | // connect |
| 76 | scroll_pane.getViewport().add(msg_pane); |
| 77 | details_dlg.getContentPane().add(scroll_pane); |
| 78 | |
| 79 | // configure |
| 80 | details_dlg.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); |
| 81 | StringWriter sw = new StringWriter(); |
| 82 | PrintWriter pw = new PrintWriter(sw); |
| 83 | t.printStackTrace(pw); |
| 84 | pw.flush(); |
| 85 | //msg_pane.setContentType("text/html"); |
| 86 | msg_pane.setText(sw.toString()); |
| 87 | |
| 88 | // show |
| 89 | details_dlg.pack(); |
| 90 | details_dlg.setSize(640, 480); |
| 91 | details_dlg.setVisible(true); |
| 92 | details_dlg.toFront(); |
| 93 | } |
| 94 | } |