1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
|
/*****************************************************************************
* Java Plug-in Framework (JPF)
* Copyright (C) 2004-2007 Dmitry Olshansky
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*****************************************************************************/
package org.java.plugin.boot;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.Collection;
import java.util.Date;
import java.util.Map;
import java.util.TreeMap;
import javax.swing.AbstractAction;
import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRootPane;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.KeyStroke;
import org.java.plugin.util.ResourceManager;
/**
* Helper class to display detailed message about application error.
*
* @version $Id$
*/
public class ErrorDialog extends JDialog {
private static final long serialVersionUID = 7142861251076530780L;
/**
* Displays error dialogue to the user.
* @param parentComponent parent component, may be <code>null</code>
* @param title window title
* @param message error message
*/
public static void showError(final Component parentComponent,
final String title, final String message) {
showError(parentComponent, title, message, null, null);
}
/**
* Displays error dialogue to the user.
* @param parentComponent parent component, may be <code>null</code>
* @param title window title
* @param message error message
* @param data error data, {@link Collection collections} and arrays are
* handled specially, all other objects are shown using
* <code>toString()</code> method
*/
public static void showError(final Component parentComponent,
final String title, final String message, final Object data) {
showError(parentComponent, title, message, data, null);
}
/**
* Displays error dialogue to the user.
* @param parentComponent parent component, may be <code>null</code>
* @param title window title
* @param data error data, {@link Collection collections} and arrays are
* handled specially, all other objects are shown using
* <code>toString()</code> method
* @param error an error to be shown in details section
*/
public static void showError(final Component parentComponent,
final String title, final Object data, final Throwable error) {
String message = error.getMessage();
if ((message == null) || (message.trim().length() == 0)) {
message = error.toString();
}
showError(parentComponent, title, message, data, error);
}
/**
* Displays error dialogue to the user.
* @param parentComponent parent component, may be <code>null</code>
* @param title window title
* @param error an error to be shown in details section
*/
public static void showError(final Component parentComponent,
final String title, final Throwable error) {
String message = error.getMessage();
if ((message == null) || (message.trim().length() == 0)) {
message = error.toString();
}
showError(parentComponent, title, message, error);
}
/**
* Displays error dialogue to the user.
* @param parentComponent parent component, may be <code>null</code>
* @param title window title
* @param message error message
* @param error an error to be shown in details section
*/
public static void showError(final Component parentComponent,
final String title, final String message, final Throwable error) {
showError(parentComponent, title, message, null, error);
}
/**
* Displays error dialogue to the user.
* @param parentComponent parent component, may be <code>null</code>
* @param title window title
* @param message error message
* @param data error data, {@link Collection collections} and arrays are
* handled specially, all other objects are shown using
* <code>toString()</code> method
* @param error an error to be shown in details section
*/
public static void showError(final Component parentComponent,
final String title, final String message, final Object data,
final Throwable error) {
Frame frame = (parentComponent != null) ? JOptionPane
.getFrameForComponent(parentComponent) : JOptionPane
.getRootFrame();
new ErrorDialog(frame, title, message, data, error, false).setVisible(true);
}
/**
* Displays error dialogue to the user and lets him to make a decision with
* "Yes" and "No" buttons. The question should be in the given message.
* @param parentComponent parent component, may be <code>null</code>
* @param title window title
* @param message error message
* @return <code>true</code> if user chooses "Yes" answer
*/
public static boolean showWarning(final Component parentComponent,
final String title, final String message) {
return showWarning(parentComponent, title, message, null, null);
}
/**
* Displays error dialogue to the user and lets him to make a decision with
* "Yes" and "No" buttons. The question should be in the given message.
* @param parentComponent parent component, may be <code>null</code>
* @param title window title
* @param message error message
* @param data error data, {@link Collection collections} and arrays are
* handled specially, all other objects are shown using
* <code>toString()</code> method
* @return <code>true</code> if user chooses "Yes" answer
*/
public static boolean showWarning(final Component parentComponent,
final String title, final String message, final Object data) {
return showWarning(parentComponent, title, message, data, null);
}
/**
* Displays error dialogue to the user and lets him to make a decision with
* "Yes" and "No" buttons. The question should be in the given message.
* @param parentComponent parent component, may be <code>null</code>
* @param title window title
* @param message error message
* @param error an error to be shown in details section
* @return <code>true</code> if user chooses "Yes" answer
*/
public static boolean showWarning(final Component parentComponent,
final String title, final String message, final Throwable error) {
return showWarning(parentComponent, title, message, null, error);
}
/**
* Displays error dialogue to the user and lets him to make a decision with
* "Yes" and "No" buttons. The question should be in the given message.
* @param parentComponent parent component, may be <code>null</code>
* @param title window title
* @param message error message
* @param data error data, {@link Collection collections} and arrays are
* handled specially, all other objects are shown using
* <code>toString()</code> method
* @param error an error to be shown in details section
* @return <code>true</code> if user chooses "Yes" answer
*/
public static boolean showWarning(final Component parentComponent,
final String title, final String message, final Object data,
final Throwable error) {
Frame frame = (parentComponent != null) ? JOptionPane
.getFrameForComponent(parentComponent) : JOptionPane
.getRootFrame();
ErrorDialog dialog = new ErrorDialog(frame, title, message, data, error,
true);
dialog.setVisible(true);
return dialog.yesBtnPressed;
}
/**
* Utility method to get detailed error report.
* @param t exception instance, may be <code>null</code>
* @return detailed error message with most important system information
* included
*/
public static String getErrorDetails(final Throwable t) {
StringBuilder sb = new StringBuilder();
String nl = System.getProperty("line.separator"); //$NON-NLS-1$
sb.append(new Date()).append(nl);
if (t != null) {
// Print exception details.
sb.append(nl).append(
"-----------------------------------------------") //$NON-NLS-1$
.append(nl);
sb.append("Exception details.").append(nl).append(nl); //$NON-NLS-1$
sb.append("Class: ").append(t.getClass().getName()).append(nl); //$NON-NLS-1$
sb.append("Message: ").append(t.getMessage()).append(nl); //$NON-NLS-1$
printError(t, "Stack trace:", sb); //$NON-NLS-1$
}
// Print system properties.
sb.append(nl).append("-----------------------------------------------") //$NON-NLS-1$
.append(nl);
sb.append("System properties:").append(nl).append(nl); //$NON-NLS-1$
for (Map.Entry<Object, Object> entry : new TreeMap<Object, Object>(System.getProperties()).entrySet()) {
sb.append(entry.getKey()).append("=") //$NON-NLS-1$
.append(entry.getValue()).append(nl);
}
// Print runtime info.
sb.append(nl).append("-----------------------------------------------") //$NON-NLS-1$
.append(nl);
sb.append("Runtime info:").append(nl).append(nl); //$NON-NLS-1$
Runtime rt = Runtime.getRuntime();
sb.append("Memory TOTAL / FREE / MAX: ") //$NON-NLS-1$
.append(rt.totalMemory()).append(" / ") //$NON-NLS-1$
.append(rt.freeMemory()).append(" / ") //$NON-NLS-1$
.append(rt.maxMemory()).append(nl);
sb.append("Available processors: ") //$NON-NLS-1$
.append(rt.availableProcessors()).append(nl);
sb.append("System class loader: ").append("" //$NON-NLS-1$ //$NON-NLS-2$
+ ClassLoader.getSystemClassLoader()).append(nl);
sb.append("Thread context class loader: ").append("" //$NON-NLS-1$ //$NON-NLS-2$
+ Thread.currentThread().getContextClassLoader()).append(nl);
sb.append("Security manager: ").append("" //$NON-NLS-1$ //$NON-NLS-2$
+ System.getSecurityManager()).append(nl);
return sb.toString();
}
/**
* Prints detailed stack trace to the given buffer.
* @param t exception instance, may be <code>null</code>
* @param header stack trace caption
* @param sb output text buffer
*/
public static void printError(final Throwable t, final String header,
final StringBuilder sb) {
if (t == null) {
return;
}
String nl = System.getProperty("line.separator"); //$NON-NLS-1$
sb.append(nl).append(header).append(nl).append(nl);
StackTraceElement[] stackTrace = t.getStackTrace();
for (int i = 0; i < stackTrace.length; i++) {
sb.append(stackTrace[i].toString()).append(nl);
}
Throwable next = t.getCause();
printError(next, "Caused by " + next, sb); //$NON-NLS-1$
if (t instanceof SQLException) {
// Handle SQLException specifically.
next = ((SQLException) t).getNextException();
printError(next, "Next exception: " + next, sb); //$NON-NLS-1$
} else if (t instanceof InvocationTargetException) {
// Handle InvocationTargetException specifically.
next = ((InvocationTargetException) t).getTargetException();
printError(next, "Target exception: " + next, sb); //$NON-NLS-1$
}
}
private javax.swing.JPanel jContentPane = null;
private JPanel jPanel = null;
private JLabel messageLabel = null;
private JLabel errorLabel = null;
private JPanel jPanel1 = null;
private JButton closeButton = null;
private JScrollPane jScrollPane = null;
JTextArea jTextArea = null;
private JTabbedPane jTabbedPane = null;
private JPanel jPanelInfo = null;
private JScrollPane jScrollPane2 = null;
private JList jList = null;
private JLabel dataLabel = null;
boolean yesBtnPressed = false;
private JButton yesButton = null;
private ErrorDialog(final Frame owner, final String title, final String message,
final Object data, final Throwable t, boolean yesNo) {
super((owner != null) ? owner : JOptionPane.getRootFrame());
initialize();
setLocationRelativeTo(getOwner());
jTabbedPane.setTitleAt(0, ResourceManager.getMessage(Boot.PACKAGE_NAME,
"infoTabLabel")); //$NON-NLS-1$
jTabbedPane.setTitleAt(1, ResourceManager.getMessage(Boot.PACKAGE_NAME,
"detailsTabLabel")); //$NON-NLS-1$
setTitle(title);
messageLabel.setText(message);
if (t != null) {
errorLabel.setText(ResourceManager.getMessage(Boot.PACKAGE_NAME,
"errorLabel", t)); //$NON-NLS-1$
} else {
getJPanel().remove(errorLabel);
}
if (data instanceof Collection) {
DefaultListModel model = new DefaultListModel();
for (Object object : (Collection) data) {
model.addElement(object);
}
jList.setModel(model);
getJPanel().remove(dataLabel);
} else if (data instanceof Object[]) {
DefaultListModel model = new DefaultListModel();
for (Object object : (Object[]) data)
model.addElement(object);
jList.setModel(model);
getJPanel().remove(dataLabel);
} else if (data != null) {
dataLabel.setText(data.toString());
getJPanelInfo().remove(getJScrollPane());
} else {
getJPanel().remove(dataLabel);
getJPanelInfo().remove(getJScrollPane());
}
jTextArea.setText(getErrorDetails(t));
jTextArea.setCaretPosition(0);
if (yesNo) {
closeButton.setText(ResourceManager.getMessage(Boot.PACKAGE_NAME,
"noLabel")); //$NON-NLS-1$
yesButton.setText(ResourceManager.getMessage(Boot.PACKAGE_NAME,
"yesLabel")); //$NON-NLS-1$
} else {
getJPanel1().remove(yesButton);
closeButton.setText(ResourceManager.getMessage(Boot.PACKAGE_NAME,
"closeLabel")); //$NON-NLS-1$
}
}
private void initialize() {
this.setDefaultCloseOperation(
javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
this.setModal(true);
this.setTitle("An error has occurred"); //$NON-NLS-1$
this.setSize(460, 280);
this.setContentPane(getJContentPane());
getRootPane().setWindowDecorationStyle(JRootPane.ERROR_DIALOG);
getRootPane().setDefaultButton(closeButton);
getRootPane().getInputMap(
JComponent.WHEN_IN_FOCUSED_WINDOW).put(
KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
"doCloseDefault"); //$NON-NLS-1$
getRootPane().getActionMap().put("doCloseDefault", //$NON-NLS-1$
new AbstractAction() {
private static final long serialVersionUID = -9167454634726502084L;
public void actionPerformed(final ActionEvent evt) {
dispose();
}
});
getRootPane().setDefaultButton(getCloseButton());
}
private javax.swing.JPanel getJContentPane() {
if (jContentPane == null) {
BorderLayout borderLayout2 = new BorderLayout();
jContentPane = new javax.swing.JPanel();
jContentPane.setLayout(borderLayout2);
borderLayout2.setVgap(2);
jContentPane.add(getJPanel1(), java.awt.BorderLayout.SOUTH);
jContentPane.add(getJTabbedPane(), java.awt.BorderLayout.CENTER);
}
return jContentPane;
}
private JPanel getJPanel() {
if (jPanel == null) {
dataLabel = new JLabel();
dataLabel.setText("JLabel"); //$NON-NLS-1$
errorLabel = new JLabel();
messageLabel = new JLabel();
jPanel = new JPanel();
jPanel.setLayout(new BoxLayout(getJPanel(), BoxLayout.Y_AXIS));
messageLabel.setText("JLabel"); //$NON-NLS-1$
errorLabel.setText("JLabel"); //$NON-NLS-1$
jPanel.add(messageLabel, null);
jPanel.add(errorLabel, null);
jPanel.add(dataLabel, null);
}
return jPanel;
}
private JPanel getJPanel1() {
if (jPanel1 == null) {
FlowLayout flowLayout = new FlowLayout();
flowLayout.setAlignment(java.awt.FlowLayout.RIGHT);
jPanel1 = new JPanel();
jPanel1.setLayout(flowLayout);
jPanel1.add(getYesButton(), null);
jPanel1.add(getCloseButton(), null);
}
return jPanel1;
}
private JButton getCloseButton() {
if (closeButton == null) {
closeButton = new JButton();
closeButton.setText("Close"); //$NON-NLS-1$
closeButton.setSelected(true);
closeButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
dispose();
}
});
}
return closeButton;
}
private JScrollPane getJScrollPane() {
if (jScrollPane == null) {
jScrollPane = new JScrollPane();
jScrollPane.setViewportView(getJList());
}
return jScrollPane;
}
private JTextArea getJTextArea() {
if (jTextArea == null) {
jTextArea = new JTextArea();
jTextArea.setBackground(java.awt.SystemColor.control);
jTextArea.setEditable(false);
jTextArea.setOpaque(false);
jTextArea.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(final MouseEvent evt) {
if (!evt.isPopupTrigger()) {
return;
}
copyText();
}
@Override
public void mouseReleased(final MouseEvent evt) {
if (!evt.isPopupTrigger()) {
return;
}
copyText();
}
private void copyText() {
if (jTextArea.getSelectedText() != null) {
jTextArea.copy();
return;
}
jTextArea.setSelectionStart(0);
jTextArea.setSelectionEnd(jTextArea.getText().length());
jTextArea.copy();
jTextArea.setSelectionEnd(0);
}
});
}
return jTextArea;
}
private JTabbedPane getJTabbedPane() {
if (jTabbedPane == null) {
jTabbedPane = new JTabbedPane();
jTabbedPane.addTab("Info", null, getJPanelInfo(), null); //$NON-NLS-1$
jTabbedPane.addTab("Details", null, getJScrollPane2(), null); //$NON-NLS-1$
}
return jTabbedPane;
}
private JPanel getJPanelInfo() {
if (jPanelInfo == null) {
jPanelInfo = new JPanel();
jPanelInfo.setLayout(new BorderLayout());
jPanelInfo.add(getJPanel(), java.awt.BorderLayout.NORTH);
jPanelInfo.add(getJScrollPane(), java.awt.BorderLayout.CENTER);
}
return jPanelInfo;
}
private JScrollPane getJScrollPane2() {
if (jScrollPane2 == null) {
jScrollPane2 = new JScrollPane();
jScrollPane2.setViewportView(getJTextArea());
}
return jScrollPane2;
}
private JList getJList() {
if (jList == null) {
jList = new JList();
}
return jList;
}
private JButton getYesButton() {
if (yesButton == null) {
yesButton = new JButton();
yesButton.setText("Yes"); //$NON-NLS-1$
yesButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
yesBtnPressed = true;
dispose();
}
});
}
return yesButton;
}
} // @jve:decl-index=0:visual-constraint="10,10"
|