1 5 package net.sf.panoptes.swing.dialogs; 6 7 import java.awt.BorderLayout ; 8 import java.awt.Frame ; 9 import java.awt.HeadlessException ; 10 import java.awt.event.ActionEvent ; 11 import java.awt.event.ActionListener ; 12 import java.io.ByteArrayOutputStream ; 13 import java.io.PrintStream ; 14 15 import javax.swing.JButton ; 16 import javax.swing.JFrame ; 17 import javax.swing.JLabel ; 18 import javax.swing.JPanel ; 19 import javax.swing.JScrollPane ; 20 import javax.swing.JTextArea ; 21 import javax.swing.UIManager ; 22 23 import net.sf.panoptes.swing.DefaultFormBuilder; 24 25 import com.jgoodies.forms.layout.FormLayout; 26 27 33 public class ErrorDialog extends BannerDialog { 34 35 JTextArea stackTrace; 36 37 46 public ErrorDialog( 47 Frame owner, 48 String title, 49 String shortDescription, 50 String fullDescription, 51 Throwable throwable) 52 throws HeadlessException { 53 super( 54 owner, 55 title, 56 shortDescription, 57 fullDescription, 58 "icons/banners/error.gif", 59 true, 60 false); 61 FormLayout layout = new FormLayout("right:max(20dlu;pref), 3dlu, 170dlu:grow, 7dlu", ""); 62 DefaultFormBuilder builder = new DefaultFormBuilder(layout); 63 builder.setDefaultDialogBorder(); 64 65 if (throwable != null) { 66 String message = throwable.getMessage(); 67 if (message == null || message.equals("")) 68 message = "No message"; 69 builder.append("Message:", new JLabel (message)); 70 71 builder.appendSeparator("Details:"); 72 builder.appendRow("fill:max(200dlu;min)"); 73 stackTrace = new JTextArea (buildStackTrace(throwable)); 74 stackTrace.setEditable(false); 75 builder.append(new JScrollPane (stackTrace), 4); 76 } 77 setMainPanel(builder.getPanel()); 78 79 JPanel buttonPanel = new JPanel (new BorderLayout ()); 80 JButton btnOK = new JButton ("OK"); 81 btnOK.addActionListener(new ActionListener () { 82 public void actionPerformed(ActionEvent e) { 83 setVisible(false); 84 } 85 }); 86 buttonPanel.add(btnOK, BorderLayout.EAST); 87 setButtonPanel(buttonPanel); 88 } 89 90 94 private String buildStackTrace(Throwable throwable) { 95 ByteArrayOutputStream bout = new ByteArrayOutputStream (); 96 PrintStream s = new PrintStream (bout); 97 throwable.printStackTrace(s); 98 return bout.toString(); 99 100 } 101 102 public static void main(String [] args) { 103 try { 104 UIManager.setLookAndFeel("com.jgoodies.plaf.plastic.PlasticXPLookAndFeel"); 105 } catch (Exception e) { 106 e.printStackTrace(); 107 } 108 109 ErrorDialog d = 110 new ErrorDialog( 111 new JFrame (), 112 "An error occured", 113 "Error", 114 "An error occured during...", 115 new Exception ()); 116 d.pack(); 117 d.show(); 118 System.exit(0); 119 } 120 121 } 122 | Popular Tags |