1 19 package org.columba.core.gui.dialog; 20 21 import java.awt.BorderLayout ; 22 import java.awt.Container ; 23 import java.awt.Dialog ; 24 import java.awt.Font ; 25 import java.awt.Frame ; 26 import java.awt.GridLayout ; 27 import java.awt.Window ; 28 import java.awt.event.ActionEvent ; 29 import java.awt.event.ActionListener ; 30 import java.io.PrintWriter ; 31 import java.io.StringWriter ; 32 import java.net.MalformedURLException ; 33 import java.net.URL ; 34 35 import javax.swing.BorderFactory ; 36 import javax.swing.JDialog ; 37 import javax.swing.JLabel ; 38 import javax.swing.JPanel ; 39 import javax.swing.JScrollPane ; 40 import javax.swing.JTextArea ; 41 import javax.swing.JToggleButton ; 42 import javax.swing.SwingConstants ; 43 import javax.swing.UIManager ; 44 45 import org.columba.core.desktop.ColumbaDesktop; 46 import org.columba.core.gui.base.ButtonWithMnemonic; 47 import org.columba.core.gui.base.MultiLineLabel; 48 import org.columba.core.gui.base.SingleSideEtchedBorder; 49 import org.columba.core.resourceloader.GlobalResourceLoader; 50 import org.frapuccino.swing.ActiveWindowTracker; 51 52 import com.jgoodies.forms.layout.CellConstraints; 53 import com.jgoodies.forms.layout.FormLayout; 54 55 64 65 public class ErrorDialog extends JDialog implements ActionListener { 66 private static final String URL_FORUM = "http://columba.sourceforge.net/phpBB2/viewforum.php?f=15"; 67 68 public static final String CMD_CLOSE = "CLOSE"; 69 70 public static final String CMD_REPORT_BUG = "REPORT_BUG"; 71 72 private static final String RESOURCE_BUNDLE_PATH = "org.columba.core.i18n.dialog"; 73 74 private boolean bool = false; 75 76 private JLabel imageLabel; 77 78 private JTextArea stacktraceTextArea; 79 80 private ButtonWithMnemonic closeButton; 81 82 private ButtonWithMnemonic reportBugButton; 83 84 private MultiLineLabel messageMultiLineLabel; 85 86 private JLabel label; 87 88 private String message; 89 90 private JToggleButton detailsButton; 91 92 private Throwable ex; 93 94 public static ErrorDialog createDialog(String message, Throwable ex) { 95 Window w = ActiveWindowTracker.findActiveWindow(); 96 if (w instanceof Frame ) 97 return new ErrorDialog((Frame ) w, message, ex); 98 else if (w instanceof Dialog ) 99 return new ErrorDialog((Dialog ) w, message, ex); 100 else 101 return new ErrorDialog(message, ex); 102 } 103 104 private ErrorDialog(String message, Throwable ex) { 105 super(); 106 107 init(message, ex); 108 } 109 110 private ErrorDialog(Dialog frame, String message, Throwable ex) { 111 super(frame, true); 112 113 init(message, ex); 114 } 115 116 private ErrorDialog(Frame frame, String message, Throwable ex) { 117 super(frame, true); 118 119 init(message, ex); 120 } 121 122 private void init(String message, Throwable ex) { 123 this.message = message; 124 this.ex = ex; 125 126 setTitle(GlobalResourceLoader.getString("org.columba.core.i18n.dialog", 127 "error", "error_title")); 128 129 initComponents(); 130 layoutComponents(); 131 pack(); 132 133 setLocationRelativeTo(null); 134 setVisible(true); 135 } 136 137 private JPanel createCenterPanel(boolean showDetails) { 138 FormLayout layout = new FormLayout( 139 "default, 3dlu, fill:default:grow, 3dlu", 140 "default, 8dlu, default 2dlu, fill:default:grow"); 141 142 JPanel centerPanel = new JPanel (layout); 143 centerPanel.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12)); 144 145 CellConstraints cc = new CellConstraints(); 146 centerPanel.add(imageLabel, cc.xy(1, 1)); 147 148 centerPanel.add(messageMultiLineLabel, cc.xywh(3, 1, 1, 1)); 149 151 centerPanel.add(detailsButton, cc.xywh(1, 3, 1, 1)); 152 if (showDetails) { 153 centerPanel.add(new JScrollPane (stacktraceTextArea), cc.xywh(1, 5, 154 3, 1)); 155 156 } 157 158 return centerPanel; 159 } 160 161 private void initComponents() { 162 imageLabel = new JLabel (UIManager.getIcon("OptionPane.errorIcon"), 163 SwingConstants.LEFT); 164 165 messageMultiLineLabel = new MultiLineLabel(message); 166 167 messageMultiLineLabel.setFont(messageMultiLineLabel.getFont() 168 .deriveFont(Font.BOLD)); 169 label = new JLabel (message); 170 label.setFont(label.getFont().deriveFont(Font.BOLD)); 171 172 stacktraceTextArea = new JTextArea (); 173 174 if ( ex != null ) { 175 StringWriter stringWriter = new StringWriter (); 176 ex.printStackTrace(new PrintWriter (stringWriter)); 177 178 stacktraceTextArea.append(stringWriter.toString()); 179 stacktraceTextArea.setEditable(false); 180 } 181 182 detailsButton = new JToggleButton ("Details >>"); 184 detailsButton.setSelected(false); 185 detailsButton.setActionCommand("DETAILS"); 186 detailsButton.addActionListener(this); 187 188 if ( ex == null ) { 189 detailsButton.setEnabled(false); 190 } 191 192 closeButton = new ButtonWithMnemonic(GlobalResourceLoader.getString( 193 "global", "global", "close")); 194 closeButton.setActionCommand(CMD_CLOSE); 195 closeButton.addActionListener(this); 196 197 reportBugButton = new ButtonWithMnemonic(GlobalResourceLoader 198 .getString(RESOURCE_BUNDLE_PATH, "exception", "report_bug")); 199 reportBugButton.setActionCommand(CMD_REPORT_BUG); 200 reportBugButton.addActionListener(this); 201 } 202 203 private void layoutComponents() { 204 if (getContentPane().getComponentCount() > 0) { 205 getContentPane().removeAll(); 206 } 207 208 getRootPane().setDefaultButton(closeButton); 209 210 JPanel bottomPanel = new JPanel (new BorderLayout (0, 0)); 211 bottomPanel.setBorder(new SingleSideEtchedBorder(SwingConstants.TOP)); 212 213 JPanel buttonPanel = new JPanel (new GridLayout (1, 3, 6, 0)); 214 buttonPanel.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12)); 215 216 buttonPanel.add(closeButton); 218 219 bottomPanel.add(buttonPanel, BorderLayout.EAST); 220 221 Container c = getContentPane(); 222 c.setLayout(new BorderLayout ()); 223 224 c.add(createCenterPanel(detailsButton.isSelected()), 225 BorderLayout.CENTER); 226 c.add(bottomPanel, BorderLayout.SOUTH); 227 228 236 237 } 238 239 public boolean success() { 240 return bool; 241 } 242 243 public void actionPerformed(ActionEvent e) { 244 String command = e.getActionCommand(); 245 if (CMD_CLOSE.equals(command)) { 246 bool = true; 247 dispose(); 248 } else if (CMD_REPORT_BUG.equals(command)) { 249 bool = false; 250 251 try { 252 ColumbaDesktop.getInstance().browse(new URL (URL_FORUM)); 253 } catch (MalformedURLException e1) { 254 } 255 256 } else if (command.equals("DETAILS")) { 257 layoutComponents(); 258 pack(); 259 setLocationRelativeTo(null); 260 } 261 } 262 } | Popular Tags |