1 package ch.odi.justblog.gui.swing;
2
3 import java.awt.Container;
4 import java.awt.Frame;
5 import java.awt.event.ActionEvent;
6 import java.awt.event.ActionListener;
7 import java.io.PrintWriter;
8 import java.io.StringWriter;
9
10 import javax.swing.BoxLayout;
11 import javax.swing.JButton;
12 import javax.swing.JDialog;
13 import javax.swing.JLabel;
14 import javax.swing.JScrollPane;
15 import javax.swing.JTextArea;
16
17 import org.apache.commons.logging.Log;
18 import org.apache.commons.logging.LogFactory;
19
20 /***
21 * Presents a error message to the user.
22 * The dialog can show a stack trace if the log level for this class
23 * is set to DEBUG.
24 *
25 * @author oglueck
26 */
27 public class ErrorMessage extends JDialog {
28
29 /***
30 * Creates a modal dialog, showing the exception.
31 *
32 */
33 public ErrorMessage(Frame owner, Exception ex) {
34 super(owner, true);
35 this.setTitle("Error");
36 Container cp = getContentPane();
37 BoxLayout lo = new BoxLayout(cp, BoxLayout.Y_AXIS);
38 cp.setLayout(lo);
39 cp.add(new JLabel(ex.getMessage()));
40
41 Log log = LogFactory.getLog(getClass());
42 if (log.isDebugEnabled()) {
43 StringWriter sw = new StringWriter();
44 PrintWriter w = new PrintWriter(sw);
45 ex.printStackTrace(w);
46 w.close();
47 JTextArea text = new JTextArea(sw.toString(), 10, 50);
48 text.setEditable(false);
49 JScrollPane scroll = new JScrollPane(text);
50 cp.add(scroll);
51 }
52
53 JButton ok = new JButton("ok");
54 ok.setDefaultCapable(true);
55 ok.addActionListener(
56 new ActionListener() {
57 public void actionPerformed(ActionEvent e) {
58 ErrorMessage.this.dispose();
59 }
60 }
61 );
62 cp.add(ok);
63 getRootPane().setDefaultButton(ok);
64 pack();
65 show();
66 }
67
68 }