1 56 57 package org.objectstyle.cayenne.modeler.dialog; 58 59 import java.awt.BorderLayout ; 60 import java.awt.Container ; 61 import java.awt.HeadlessException ; 62 import java.awt.event.ActionEvent ; 63 import java.awt.event.ActionListener ; 64 import java.io.IOException ; 65 import java.io.PrintWriter ; 66 import java.io.StringWriter ; 67 68 import javax.swing.BorderFactory ; 69 import javax.swing.JButton ; 70 import javax.swing.JEditorPane ; 71 import javax.swing.JPanel ; 72 import javax.swing.JScrollPane ; 73 import javax.swing.JTextArea ; 74 75 import org.objectstyle.cayenne.modeler.Application; 76 import org.objectstyle.cayenne.modeler.CayenneModelerFrame; 77 import org.objectstyle.cayenne.modeler.util.CayenneDialog; 78 import org.objectstyle.cayenne.modeler.util.PanelFactory; 79 import org.objectstyle.cayenne.util.Util; 80 import org.scopemvc.util.UIStrings; 81 82 87 public class ErrorDebugDialog extends CayenneDialog implements ActionListener { 88 protected JButton close; 89 protected JButton showHide; 90 protected JTextArea exText = new JTextArea (); 91 protected JPanel exPanel; 92 protected Throwable throwable; 93 protected boolean detailed; 94 95 public static void guiException(Throwable th) { 96 if (th != null) { 97 th.printStackTrace(); 98 } 99 100 ErrorDebugDialog dialog = 101 new ErrorDebugDialog(Application.getFrame(), "CayenneModeler Error", th, true); 102 dialog.setVisible(true); 103 } 104 105 public static void guiWarning(Throwable th, String message) { 106 if (th != null) { 107 th.printStackTrace(); 108 } 109 110 WarningDialog dialog = new WarningDialog(Application.getFrame(), message, th, false); 111 dialog.setVisible(true); 112 } 113 114 117 protected ErrorDebugDialog( 118 CayenneModelerFrame owner, 119 String title, 120 Throwable throwable, 121 boolean detailed) 122 throws HeadlessException { 123 124 super(owner, title, true); 125 126 setThrowable(Util.unwindException(throwable)); 127 setDetailed(detailed); 128 init(); 129 } 130 131 protected void init() { 132 setResizable(false); 133 134 Container pane = this.getContentPane(); 135 pane.setLayout(new BorderLayout ()); 136 137 JEditorPane infoText = new JEditorPane ("text/html", infoHTML()); 139 infoText.setBackground(pane.getBackground()); 140 infoText.setEditable(false); 141 infoText.addHyperlinkListener(this); 143 144 JPanel infoPanel = new JPanel (); 145 infoPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); 146 infoPanel.add(infoText); 147 pane.add(infoPanel, BorderLayout.NORTH); 148 149 if (throwable != null) { 151 exText.setEditable(false); 152 exText.setLineWrap(true); 153 exText.setWrapStyleWord(true); 154 exText.setRows(16); 155 exText.setColumns(40); 156 JScrollPane exScroll = 157 new JScrollPane ( 158 exText, 159 JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, 160 JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); 161 exPanel = new JPanel (); 162 exPanel.setLayout(new BorderLayout ()); 163 exPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); 164 exPanel.add(exScroll, BorderLayout.CENTER); 165 166 showHide = new JButton (""); 168 showHide.addActionListener(this); 169 if (isDetailed()) { 170 showDetails(); 171 } else { 172 hideDetails(); 173 } 174 } 175 176 close = new JButton ("Close"); 177 close.addActionListener(this); 178 179 JButton [] buttons = (showHide != null) ? new JButton [] { showHide, close } 180 : new JButton [] { close }; 181 pane.add(PanelFactory.createButtonPanel(buttons), BorderLayout.SOUTH); 182 183 this.pack(); 185 this.centerWindow(); 186 } 187 188 protected String infoHTML() { 189 String bugreportURL = UIStrings.get("cayenne.bugreport.url"); 190 return "<b><font face='Arial,Helvetica' size='+1' color='red'>" 191 + getTitle() 192 + "</font></b><br>" 193 + "<font face='Arial,Helvetica' size='-1'>Please copy the message below and " 194 + "report this error by going to <br>" 195 + "<a HREF='" 196 + bugreportURL 197 + "'>" 198 + bugreportURL 199 + "</a></font>"; 200 } 201 202 protected void setThrowable(Throwable throwable) { 203 this.throwable = throwable; 204 205 String text = null; 206 if (throwable != null) { 207 StringWriter str = new StringWriter (); 208 PrintWriter out = new PrintWriter (str); 209 210 String version = UIStrings.get("cayenne.version"); 212 version = (version != null) ? version : "(unknown)"; 213 214 String buildDate = UIStrings.get("cayenne.build.date"); 215 buildDate = (buildDate != null) ? buildDate : "(unknown)"; 216 217 out.println("CayenneModeler Info"); 218 out.println("Version: " + version); 219 out.println("Build Date: " + buildDate); 220 out.println("Exception: "); 221 out.println("================================="); 222 buildStackTrace(out, throwable); 223 224 try { 225 out.close(); 226 str.close(); 227 } catch (IOException ioex) { 228 } 230 text = str.getBuffer().toString(); 231 } 232 233 exText.setText(text); 234 } 235 236 protected void buildStackTrace(PrintWriter out, Throwable th) { 237 if (th == null) { 238 return; 239 } 240 241 th.printStackTrace(out); 242 243 Throwable cause = th.getCause(); 244 if (cause != null) { 245 out.println("Caused by:"); 246 buildStackTrace(out, cause); 247 } 248 } 249 250 public void actionPerformed(ActionEvent e) { 251 if (e.getSource() == close) { 252 this.dispose(); 253 } else if (e.getSource() == showHide) { 254 if (isDetailed()) { 255 hideDetails(); 256 } else { 257 showDetails(); 258 } 259 this.pack(); 260 this.centerWindow(); 261 } 262 } 263 264 protected void hideDetails() { 265 getContentPane().remove(exPanel); 266 showHide.setText("Show Details"); 267 setDetailed(false); 268 } 269 270 protected void showDetails() { 271 getContentPane().add(exPanel, BorderLayout.CENTER); 272 showHide.setText("Hide Details"); 273 setDetailed(true); 274 } 275 276 280 public boolean isDetailed() { 281 return detailed; 282 } 283 284 288 public void setDetailed(boolean detailed) { 289 this.detailed = detailed; 290 } 291 } 292 | Popular Tags |