1 19 package org.java.plugin.boot; 20 21 import java.awt.BorderLayout ; 22 import java.awt.Component ; 23 import java.awt.FlowLayout ; 24 import java.awt.Frame ; 25 import java.awt.event.ActionEvent ; 26 import java.awt.event.KeyEvent ; 27 import java.awt.event.MouseAdapter ; 28 import java.awt.event.MouseEvent ; 29 import java.lang.reflect.InvocationTargetException ; 30 import java.sql.SQLException ; 31 import java.util.Collection ; 32 import java.util.Date ; 33 import java.util.Iterator ; 34 import java.util.Map ; 35 import java.util.TreeMap ; 36 37 import javax.swing.AbstractAction ; 38 import javax.swing.BoxLayout ; 39 import javax.swing.DefaultListModel ; 40 import javax.swing.JButton ; 41 import javax.swing.JComponent ; 42 import javax.swing.JDialog ; 43 import javax.swing.JLabel ; 44 import javax.swing.JList ; 45 import javax.swing.JOptionPane ; 46 import javax.swing.JPanel ; 47 import javax.swing.JRootPane ; 48 import javax.swing.JScrollPane ; 49 import javax.swing.JTabbedPane ; 50 import javax.swing.JTextArea ; 51 import javax.swing.KeyStroke ; 52 53 import org.java.plugin.util.ResourceManager; 54 55 60 public class ErrorDialog extends JDialog { 61 private static final long serialVersionUID = 7142861251076530780L; 62 63 69 public static void showError(final Component parentComponent, 70 final String title, final String message) { 71 showError(parentComponent, title, message, null, null); 72 } 73 74 83 public static void showError(final Component parentComponent, 84 final String title, final String message, final Object data) { 85 showError(parentComponent, title, message, data, null); 86 } 87 88 97 public static void showError(final Component parentComponent, 98 final String title, final Object data, final Throwable error) { 99 String message = error.getMessage(); 100 if ((message == null) || (message.trim().length() == 0)) { 101 message = error.toString(); 102 } 103 showError(parentComponent, title, message, data, error); 104 } 105 106 112 public static void showError(final Component parentComponent, 113 final String title, final Throwable error) { 114 String message = error.getMessage(); 115 if ((message == null) || (message.trim().length() == 0)) { 116 message = error.toString(); 117 } 118 showError(parentComponent, title, message, error); 119 } 120 121 128 public static void showError(final Component parentComponent, 129 final String title, final String message, final Throwable error) { 130 showError(parentComponent, title, message, null, error); 131 } 132 133 143 public static void showError(final Component parentComponent, 144 final String title, final String message, final Object data, 145 final Throwable error) { 146 Frame frame = (parentComponent != null) ? JOptionPane 147 .getFrameForComponent(parentComponent) : JOptionPane 148 .getRootFrame(); 149 new ErrorDialog(frame, title, message, data, error, false).show(); 150 } 151 152 160 public static boolean showWarning(final Component parentComponent, 161 final String title, final String message) { 162 return showWarning(parentComponent, title, message, null, null); 163 } 164 165 176 public static boolean showWarning(final Component parentComponent, 177 final String title, final String message, final Object data) { 178 return showWarning(parentComponent, title, message, data, null); 179 } 180 181 190 public static boolean showWarning(final Component parentComponent, 191 final String title, final String message, final Throwable error) { 192 return showWarning(parentComponent, title, message, null, error); 193 } 194 195 207 public static boolean showWarning(final Component parentComponent, 208 final String title, final String message, final Object data, 209 final Throwable error) { 210 Frame frame = (parentComponent != null) ? JOptionPane 211 .getFrameForComponent(parentComponent) : JOptionPane 212 .getRootFrame(); 213 ErrorDialog dialog = new ErrorDialog(frame, title, message, data, error, 214 true); 215 dialog.show(); 216 return dialog.yesBtnPressed; 217 } 218 219 225 public static String getErrorDetails(final Throwable t) { 226 StringBuffer sb = new StringBuffer (); 227 String nl = System.getProperty("line.separator"); sb.append(new Date ()).append(nl); 229 if (t != null) { 230 sb.append(nl).append( 232 "-----------------------------------------------") .append(nl); 234 sb.append("Exception details.").append(nl).append(nl); sb.append("Class: ").append(t.getClass().getName()).append(nl); sb.append("Message: ").append(t.getMessage()).append(nl); printError(t, "Stack trace:", sb); } 239 sb.append(nl).append("-----------------------------------------------") .append(nl); 242 sb.append("System properties:").append(nl).append(nl); for (Iterator it = new TreeMap (System.getProperties()).entrySet() 244 .iterator(); it.hasNext();) { 245 Map.Entry entry = (Map.Entry ) it.next(); 246 sb.append(entry.getKey()).append("=") .append(entry.getValue()).append(nl); 248 } 249 sb.append(nl).append("-----------------------------------------------") .append(nl); 252 sb.append("Runtime info:").append(nl).append(nl); Runtime rt = Runtime.getRuntime(); 254 sb.append("Memory TOTAL / FREE / MAX: ") .append(rt.totalMemory()).append(" / ") .append(rt.freeMemory()).append(" / ") .append(rt.maxMemory()).append(nl); 258 sb.append("Available processors: ") .append(rt.availableProcessors()).append(nl); 260 sb.append("System class loader: ").append("" + ClassLoader.getSystemClassLoader()).append(nl); 262 sb.append("Thread context class loader: ").append("" + Thread.currentThread().getContextClassLoader()).append(nl); 264 sb.append("Security manager: ").append("" + System.getSecurityManager()).append(nl); 266 return sb.toString(); 267 } 268 269 275 public static void printError(final Throwable t, final String header, 276 final StringBuffer sb) { 277 if (t == null) { 278 return; 279 } 280 String nl = System.getProperty("line.separator"); sb.append(nl).append(header).append(nl).append(nl); 282 StackTraceElement [] stackTrace = t.getStackTrace(); 283 for (int i = 0; i < stackTrace.length; i++) { 284 sb.append(stackTrace[i].toString()).append(nl); 285 } 286 Throwable next = t.getCause(); 287 printError(next, "Caused by " + next, sb); if (t instanceof SQLException ) { 289 next = ((SQLException ) t).getNextException(); 291 printError(next, "Next exception: " + next, sb); } else if (t instanceof InvocationTargetException ) { 293 next = ((InvocationTargetException ) t).getTargetException(); 295 printError(next, "Target exception: " + next, sb); } 297 } 298 299 private javax.swing.JPanel jContentPane = null; 300 private JPanel jPanel = null; 301 private JLabel messageLabel = null; 302 private JLabel errorLabel = null; 303 private JPanel jPanel1 = null; 304 private JButton closeButton = null; 305 private JScrollPane jScrollPane = null; 306 JTextArea jTextArea = null; 307 private JTabbedPane jTabbedPane = null; 308 private JPanel jPanelInfo = null; 309 private JScrollPane jScrollPane2 = null; 310 private JList jList = null; 311 private JLabel dataLabel = null; 312 boolean yesBtnPressed = false; 313 private JButton yesButton = null; 314 315 private ErrorDialog(final Frame owner, final String title, final String message, 316 final Object data, final Throwable t, boolean yesNo) { 317 super((owner != null) ? owner : JOptionPane.getRootFrame()); 318 initialize(); 319 setLocationRelativeTo(getOwner()); 320 jTabbedPane.setTitleAt(0, ResourceManager.getMessage(Boot.PACKAGE_NAME, 321 "infoTabLabel")); jTabbedPane.setTitleAt(1, ResourceManager.getMessage(Boot.PACKAGE_NAME, 323 "detailsTabLabel")); setTitle(title); 325 messageLabel.setText(message); 326 if (t != null) { 327 errorLabel.setText(ResourceManager.getMessage(Boot.PACKAGE_NAME, 328 "errorLabel", t)); } else { 330 getJPanel().remove(errorLabel); 331 } 332 if (data instanceof Collection ) { 333 DefaultListModel model = new DefaultListModel (); 334 for (Iterator it = ((Collection ) data).iterator(); it.hasNext();) { 335 model.addElement(it.next()); 336 } 337 jList.setModel(model); 338 getJPanel().remove(dataLabel); 339 } else if (data instanceof Object []) { 340 DefaultListModel model = new DefaultListModel (); 341 for (int i = 0; i < ((Object []) data).length; i++) { 342 model.addElement(((Object []) data)[i]); 343 } 344 jList.setModel(model); 345 getJPanel().remove(dataLabel); 346 } else if (data != null) { 347 dataLabel.setText(data.toString()); 348 getJPanelInfo().remove(getJScrollPane()); 349 } else { 350 getJPanel().remove(dataLabel); 351 getJPanelInfo().remove(getJScrollPane()); 352 } 353 jTextArea.setText(getErrorDetails(t)); 354 jTextArea.setCaretPosition(0); 355 if (yesNo) { 356 closeButton.setText(ResourceManager.getMessage(Boot.PACKAGE_NAME, 357 "noLabel")); yesButton.setText(ResourceManager.getMessage(Boot.PACKAGE_NAME, 359 "yesLabel")); } else { 361 getJPanel1().remove(yesButton); 362 closeButton.setText(ResourceManager.getMessage(Boot.PACKAGE_NAME, 363 "closeLabel")); } 365 } 366 367 private void initialize() { 368 this.setDefaultCloseOperation( 369 javax.swing.WindowConstants.DISPOSE_ON_CLOSE); 370 this.setModal(true); 371 this.setTitle("An error has occurred"); this.setSize(460, 280); 373 this.setContentPane(getJContentPane()); 374 getRootPane().setWindowDecorationStyle(JRootPane.ERROR_DIALOG); 375 getRootPane().setDefaultButton(closeButton); 376 getRootPane().getInputMap( 377 JComponent.WHEN_IN_FOCUSED_WINDOW).put( 378 KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), 379 "doCloseDefault"); getRootPane().getActionMap().put("doCloseDefault", new AbstractAction () { 382 private static final long serialVersionUID = -9167454634726502084L; 383 384 public void actionPerformed(final ActionEvent evt) { 385 dispose(); 386 } 387 }); 388 getRootPane().setDefaultButton(getCloseButton()); 389 } 390 391 private javax.swing.JPanel getJContentPane() { 392 if (jContentPane == null) { 393 BorderLayout borderLayout2 = new BorderLayout (); 394 jContentPane = new javax.swing.JPanel (); 395 jContentPane.setLayout(borderLayout2); 396 borderLayout2.setVgap(2); 397 jContentPane.add(getJPanel1(), java.awt.BorderLayout.SOUTH); 398 jContentPane.add(getJTabbedPane(), java.awt.BorderLayout.CENTER); 399 } 400 return jContentPane; 401 } 402 403 private JPanel getJPanel() { 404 if (jPanel == null) { 405 dataLabel = new JLabel (); 406 dataLabel.setText("JLabel"); errorLabel = new JLabel (); 408 messageLabel = new JLabel (); 409 jPanel = new JPanel (); 410 jPanel.setLayout(new BoxLayout (getJPanel(), BoxLayout.Y_AXIS)); 411 messageLabel.setText("JLabel"); errorLabel.setText("JLabel"); jPanel.add(messageLabel, null); 414 jPanel.add(errorLabel, null); 415 jPanel.add(dataLabel, null); 416 } 417 return jPanel; 418 } 419 420 private JPanel getJPanel1() { 421 if (jPanel1 == null) { 422 FlowLayout flowLayout = new FlowLayout (); 423 flowLayout.setAlignment(java.awt.FlowLayout.RIGHT); 424 jPanel1 = new JPanel (); 425 jPanel1.setLayout(flowLayout); 426 jPanel1.add(getYesButton(), null); 427 jPanel1.add(getCloseButton(), null); 428 } 429 return jPanel1; 430 } 431 432 private JButton getCloseButton() { 433 if (closeButton == null) { 434 closeButton = new JButton (); 435 closeButton.setText("Close"); closeButton.setSelected(true); 437 closeButton.addActionListener(new java.awt.event.ActionListener () { 438 public void actionPerformed(java.awt.event.ActionEvent e) { 439 dispose(); 440 } 441 }); 442 } 443 return closeButton; 444 } 445 446 private JScrollPane getJScrollPane() { 447 if (jScrollPane == null) { 448 jScrollPane = new JScrollPane (); 449 jScrollPane.setViewportView(getJList()); 450 } 451 return jScrollPane; 452 } 453 454 private JTextArea getJTextArea() { 455 if (jTextArea == null) { 456 jTextArea = new JTextArea (); 457 jTextArea.setBackground(java.awt.SystemColor.control); 458 jTextArea.setEditable(false); 459 jTextArea.setOpaque(false); 460 jTextArea.addMouseListener(new MouseAdapter () { 461 public void mousePressed(final MouseEvent evt) { 462 if (!evt.isPopupTrigger()) { 463 return; 464 } 465 copyText(); 466 } 467 468 public void mouseReleased(final MouseEvent evt) { 469 if (!evt.isPopupTrigger()) { 470 return; 471 } 472 copyText(); 473 } 474 475 private void copyText() { 476 if (jTextArea.getSelectedText() != null) { 477 jTextArea.copy(); 478 return; 479 } 480 jTextArea.setSelectionStart(0); 481 jTextArea.setSelectionEnd(jTextArea.getText().length()); 482 jTextArea.copy(); 483 jTextArea.setSelectionEnd(0); 484 } 485 }); 486 } 487 return jTextArea; 488 } 489 490 private JTabbedPane getJTabbedPane() { 491 if (jTabbedPane == null) { 492 jTabbedPane = new JTabbedPane (); 493 jTabbedPane.addTab("Info", null, getJPanelInfo(), null); jTabbedPane.addTab("Details", null, getJScrollPane2(), null); } 496 return jTabbedPane; 497 } 498 499 private JPanel getJPanelInfo() { 500 if (jPanelInfo == null) { 501 jPanelInfo = new JPanel (); 502 jPanelInfo.setLayout(new BorderLayout ()); 503 jPanelInfo.add(getJPanel(), java.awt.BorderLayout.NORTH); 504 jPanelInfo.add(getJScrollPane(), java.awt.BorderLayout.CENTER); 505 } 506 return jPanelInfo; 507 } 508 509 private JScrollPane getJScrollPane2() { 510 if (jScrollPane2 == null) { 511 jScrollPane2 = new JScrollPane (); 512 jScrollPane2.setViewportView(getJTextArea()); 513 } 514 return jScrollPane2; 515 } 516 517 private JList getJList() { 518 if (jList == null) { 519 jList = new JList (); 520 } 521 return jList; 522 } 523 524 private JButton getYesButton() { 525 if (yesButton == null) { 526 yesButton = new JButton (); 527 yesButton.setText("Yes"); yesButton.addActionListener(new java.awt.event.ActionListener () { 529 public void actionPerformed(java.awt.event.ActionEvent e) { 530 yesBtnPressed = true; 531 dispose(); 532 } 533 }); 534 } 535 return yesButton; 536 } 537 } | Popular Tags |