| 1 |
package org.freehep.graphicsbase.swing; |
| 2 |
|
| 3 |
import java.awt.BorderLayout; |
| 4 |
import java.awt.Component; |
| 5 |
import java.awt.Dimension; |
| 6 |
import java.awt.event.ActionEvent; |
| 7 |
import java.awt.event.ActionListener; |
| 8 |
|
| 9 |
import javax.swing.JButton; |
| 10 |
import javax.swing.JComponent; |
| 11 |
import javax.swing.JDialog; |
| 12 |
import javax.swing.JOptionPane; |
| 13 |
import javax.swing.JPanel; |
| 14 |
import javax.swing.JScrollPane; |
| 15 |
import javax.swing.JTabbedPane; |
| 16 |
import javax.swing.JTextArea; |
| 17 |
import javax.swing.SwingUtilities; |
| 18 |
import javax.swing.WindowConstants; |
| 19 |
|
| 20 |
/** |
| 21 |
* A utility for displaying errors in dialogs. |
| 22 |
* @author Tony Johnson |
| 23 |
*/ |
| 24 |
public class ErrorDialog |
| 25 |
{ |
| 26 |
private ErrorDialog() |
| 27 |
{ |
| 28 |
} |
| 29 |
/** |
| 30 |
* Creates a dialog which will display a message to the user. |
| 31 |
* If a Throwable is provided the user can click the "Details" button to |
| 32 |
* get a full stack trace, including any nested errors ("caused by"). |
| 33 |
* @param source The parent component to be used for the dialog (may be null) |
| 34 |
* @param message The message to be displayed in the dialog |
| 35 |
* @param detail The exception that caused the error (may be null) |
| 36 |
*/ |
| 37 |
public static void showErrorDialog(Component source, Object message, final Throwable detail) |
| 38 |
{ |
| 39 |
final JButton details = new JButton("Details..."); |
| 40 |
details.setEnabled(detail != null); |
| 41 |
details.addActionListener(new ActionListener() |
| 42 |
{ |
| 43 |
public void actionPerformed(ActionEvent e) |
| 44 |
{ |
| 45 |
JDialog owner = (JDialog) SwingUtilities.getAncestorOfClass(JDialog.class,details); |
| 46 |
ErrorDetailsDialog dlg = new ErrorDetailsDialog(owner,detail); |
| 47 |
dlg.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); |
| 48 |
dlg.pack(); |
| 49 |
dlg.setLocationRelativeTo(owner); |
| 50 |
dlg.setVisible(true); |
| 51 |
} |
| 52 |
}); |
| 53 |
if (source != null) source.getToolkit().beep(); |
| 54 |
Object[] options = { "OK" , details }; |
| 55 |
JOptionPane.showOptionDialog(source,message,"Error...",JOptionPane.DEFAULT_OPTION,JOptionPane.ERROR_MESSAGE,null,options,options[0]); |
| 56 |
} |
| 57 |
public static class ErrorDetailsDialog extends JDialog |
| 58 |
{ |
| 59 |
public ErrorDetailsDialog(JDialog owner, Throwable detail) |
| 60 |
{ |
| 61 |
super(owner); |
| 62 |
JComponent messageComponent = null; |
| 63 |
JTabbedPane tabs = null; |
| 64 |
Throwable ex = detail; |
| 65 |
for (; ex != null;) |
| 66 |
{ |
| 67 |
JTextArea ta = new JTextArea(); |
| 68 |
ta.append(ex+"\n"); |
| 69 |
StackTraceElement[] trace = ex.getStackTrace(); |
| 70 |
for (int i=0; i<trace.length; i++) |
| 71 |
{ |
| 72 |
ta.append(" at "+trace[i]+"\n"); |
| 73 |
} |
| 74 |
JScrollPane scroll = new JScrollPane(ta); |
| 75 |
ta.setCaretPosition(0); |
| 76 |
ta.setEditable(false); |
| 77 |
scroll.setPreferredSize(new Dimension(400,300)); |
| 78 |
ex = ex.getCause(); |
| 79 |
if (ex != null) |
| 80 |
{ |
| 81 |
if (messageComponent == null) |
| 82 |
{ |
| 83 |
messageComponent = tabs = new JTabbedPane(); |
| 84 |
tabs.addTab("Exception",scroll); |
| 85 |
} |
| 86 |
else |
| 87 |
{ |
| 88 |
tabs.addTab("Caused by",scroll); |
| 89 |
} |
| 90 |
} |
| 91 |
else |
| 92 |
{ |
| 93 |
if (messageComponent == null) messageComponent = scroll; |
| 94 |
else tabs.addTab("Caused by",scroll); |
| 95 |
break; |
| 96 |
} |
| 97 |
} |
| 98 |
getContentPane().add(messageComponent); |
| 99 |
JButton close = new JButton("Close") |
| 100 |
{ |
| 101 |
public void fireActionPerformed(ActionEvent event) |
| 102 |
{ |
| 103 |
dispose(); |
| 104 |
} |
| 105 |
}; |
| 106 |
JPanel panel = new JPanel(); |
| 107 |
panel.add(close); |
| 108 |
getContentPane().add(panel,BorderLayout.SOUTH); |
| 109 |
} |
| 110 |
} |
| 111 |
} |