1 13 package org.eclipse.jface.dialogs; 14 15 import org.eclipse.core.runtime.CoreException; 16 import org.eclipse.core.runtime.IStatus; 17 import org.eclipse.jface.resource.JFaceResources; 18 import org.eclipse.jface.util.Policy; 19 import org.eclipse.swt.SWT; 20 import org.eclipse.swt.dnd.Clipboard; 21 import org.eclipse.swt.dnd.TextTransfer; 22 import org.eclipse.swt.dnd.Transfer; 23 import org.eclipse.swt.events.SelectionEvent; 24 import org.eclipse.swt.events.SelectionListener; 25 import org.eclipse.swt.graphics.Image; 26 import org.eclipse.swt.graphics.Point; 27 import org.eclipse.swt.layout.GridData; 28 import org.eclipse.swt.layout.GridLayout; 29 import org.eclipse.swt.widgets.Button; 30 import org.eclipse.swt.widgets.Composite; 31 import org.eclipse.swt.widgets.Control; 32 import org.eclipse.swt.widgets.Label; 33 import org.eclipse.swt.widgets.List; 34 import org.eclipse.swt.widgets.Menu; 35 import org.eclipse.swt.widgets.MenuItem; 36 import org.eclipse.swt.widgets.Shell; 37 38 46 public class ErrorDialog extends IconAndMessageDialog { 47 50 public static boolean AUTOMATED_MODE = false; 51 52 55 private static final int LIST_ITEM_COUNT = 7; 56 57 60 private static final String NESTING_INDENT = " "; 62 65 private Button detailsButton; 66 67 70 private String title; 71 72 75 private List list; 76 77 80 private boolean listCreated = false; 81 82 85 private int displayMask = 0xFFFF; 86 87 90 private IStatus status; 91 92 95 private Clipboard clipboard; 96 97 private boolean shouldIncludeTopLevelErrorInDetails = false; 98 99 100 128 public ErrorDialog(Shell parentShell, String dialogTitle, String message, 129 IStatus status, int displayMask) { 130 super(parentShell); 131 this.title = dialogTitle == null ? JFaceResources 132 .getString("Problem_Occurred") : dialogTitle; 134 this.message = message == null ? status.getMessage() 135 : JFaceResources 136 .format( 137 "Reason", new Object [] { message, status.getMessage() }); this.status = status; 139 this.displayMask = displayMask; 140 setShellStyle(getShellStyle() | SWT.RESIZE); 141 } 142 143 150 protected void buttonPressed(int id) { 151 if (id == IDialogConstants.DETAILS_ID) { 152 toggleDetailsArea(); 154 } else { 155 super.buttonPressed(id); 156 } 157 } 158 159 162 protected void configureShell(Shell shell) { 163 super.configureShell(shell); 164 shell.setText(title); 165 } 166 167 172 protected void createButtonsForButtonBar(Composite parent) { 173 createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, 175 true); 176 createDetailsButton(parent); 177 } 178 179 184 private void createSupportArea(Composite parent) { 185 186 ErrorSupportProvider provider = Policy.getErrorSupportProvider(); 187 188 if (provider == null) 189 return; 190 191 Composite supportArea = new Composite(parent, SWT.NONE); 192 provider.createSupportArea(supportArea, status); 193 194 GridData supportData = new GridData(SWT.FILL, SWT.FILL, true, true); 195 supportData.verticalSpan = 3; 196 supportArea.setLayoutData(supportData); 197 if (supportArea.getLayout() == null){ 198 GridLayout layout = new GridLayout(); 199 layout.marginWidth = 0; 200 layout.marginHeight = 0; 201 supportArea.setLayout(layout); } 203 204 205 } 206 207 214 protected void createDetailsButton(Composite parent) { 215 if (shouldShowDetailsButton()) { 216 detailsButton = createButton(parent, IDialogConstants.DETAILS_ID, 217 IDialogConstants.SHOW_DETAILS_LABEL, false); 218 } 219 } 220 221 227 protected Control createDialogArea(Composite parent) { 228 createMessageArea(parent); 229 createSupportArea(parent); 230 Composite composite = new Composite(parent, SWT.NONE); 232 GridLayout layout = new GridLayout(); 233 layout.marginHeight = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN); 234 layout.marginWidth = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN); 235 layout.verticalSpacing = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING); 236 layout.horizontalSpacing = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING); 237 layout.numColumns = 2; 238 composite.setLayout(layout); 239 GridData childData = new GridData(GridData.FILL_BOTH); 240 childData.horizontalSpan = 2; 241 composite.setLayoutData(childData); 242 composite.setFont(parent.getFont()); 243 244 return composite; 245 } 246 247 250 protected void createDialogAndButtonArea(Composite parent) { 251 super.createDialogAndButtonArea(parent); 252 if (this.dialogArea instanceof Composite) { 253 Composite dialogComposite = (Composite) dialogArea; 255 if (dialogComposite.getChildren().length == 0) { 256 new Label(dialogComposite, SWT.NULL); 257 } 258 } 259 } 260 261 266 protected Image getImage() { 267 if (status != null) { 268 if (status.getSeverity() == IStatus.WARNING) { 269 return getWarningImage(); 270 } 271 if (status.getSeverity() == IStatus.INFO) { 272 return getInfoImage(); 273 } 274 } 275 return getErrorImage(); 277 } 278 279 286 protected List createDropDownList(Composite parent) { 287 list = new List(parent, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL 289 | SWT.MULTI); 290 populateList(list); 292 GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL 293 | GridData.GRAB_HORIZONTAL | GridData.VERTICAL_ALIGN_FILL 294 | GridData.GRAB_VERTICAL); 295 data.heightHint = list.getItemHeight() * LIST_ITEM_COUNT; 296 data.horizontalSpan = 2; 297 list.setLayoutData(data); 298 list.setFont(parent.getFont()); 299 Menu copyMenu = new Menu(list); 300 MenuItem copyItem = new MenuItem(copyMenu, SWT.NONE); 301 copyItem.addSelectionListener(new SelectionListener() { 302 305 public void widgetSelected(SelectionEvent e) { 306 copyToClipboard(); 307 } 308 309 312 public void widgetDefaultSelected(SelectionEvent e) { 313 copyToClipboard(); 314 } 315 }); 316 copyItem.setText(JFaceResources.getString("copy")); list.setMenu(copyMenu); 318 listCreated = true; 319 return list; 320 } 321 322 325 331 public int open() { 332 if (!AUTOMATED_MODE && shouldDisplay(status, displayMask)) { 333 return super.open(); 334 } 335 setReturnCode(OK); 336 return OK; 337 } 338 339 360 public static int openError(Shell parent, String dialogTitle, 361 String message, IStatus status) { 362 return openError(parent, dialogTitle, message, status, IStatus.OK 363 | IStatus.INFO | IStatus.WARNING | IStatus.ERROR); 364 } 365 366 393 public static int openError(Shell parentShell, String title, 394 String message, IStatus status, int displayMask) { 395 ErrorDialog dialog = new ErrorDialog(parentShell, title, message, 396 status, displayMask); 397 return dialog.open(); 398 } 399 400 409 private void populateList(List listToPopulate) { 410 populateList(listToPopulate, status, 0, 411 shouldIncludeTopLevelErrorInDetails); 412 } 413 414 430 private void populateList(List listToPopulate, IStatus buildingStatus, 431 int nesting, boolean includeStatus) { 432 433 if (!buildingStatus.matches(displayMask)) { 434 return; 435 } 436 437 Throwable t = buildingStatus.getException(); 438 boolean isCoreException = t instanceof CoreException; 439 boolean incrementNesting = false; 440 441 if (includeStatus) { 442 StringBuffer sb = new StringBuffer (); 443 for (int i = 0; i < nesting; i++) { 444 sb.append(NESTING_INDENT); 445 } 446 String message = buildingStatus.getMessage(); 447 sb.append(message); 448 listToPopulate.add(sb.toString()); 449 incrementNesting = true; 450 } 451 452 if (!isCoreException && t != null) { 453 StringBuffer sb = new StringBuffer (); 455 for (int i = 0; i < nesting; i++) { 456 sb.append(NESTING_INDENT); 457 } 458 String message = t.getLocalizedMessage(); 459 if (message == null) { 460 message = t.toString(); 461 } 462 463 sb.append(message); 464 listToPopulate.add(sb.toString()); 465 incrementNesting = true; 466 } 467 468 if (incrementNesting) { 469 nesting++; 470 } 471 472 if (isCoreException) { 474 CoreException ce = (CoreException) t; 475 IStatus eStatus = ce.getStatus(); 476 if (message == null || message.indexOf(eStatus.getMessage()) == -1) { 479 populateList(listToPopulate, eStatus, nesting, true); 480 } 481 } 482 483 IStatus[] children = buildingStatus.getChildren(); 485 for (int i = 0; i < children.length; i++) { 486 populateList(listToPopulate, children[i], nesting, true); 487 } 488 } 489 490 501 protected static boolean shouldDisplay(IStatus status, int mask) { 502 IStatus[] children = status.getChildren(); 503 if (children == null || children.length == 0) { 504 return status.matches(mask); 505 } 506 for (int i = 0; i < children.length; i++) { 507 if (children[i].matches(mask)) { 508 return true; 509 } 510 } 511 return false; 512 } 513 514 518 private void toggleDetailsArea() { 519 Point windowSize = getShell().getSize(); 520 Point oldSize = getShell().computeSize(SWT.DEFAULT, SWT.DEFAULT); 521 if (listCreated) { 522 list.dispose(); 523 listCreated = false; 524 detailsButton.setText(IDialogConstants.SHOW_DETAILS_LABEL); 525 } else { 526 list = createDropDownList((Composite) getContents()); 527 detailsButton.setText(IDialogConstants.HIDE_DETAILS_LABEL); 528 } 529 Point newSize = getShell().computeSize(SWT.DEFAULT, SWT.DEFAULT); 530 getShell() 531 .setSize( 532 new Point(windowSize.x, windowSize.y 533 + (newSize.y - oldSize.y))); 534 } 535 536 543 private void populateCopyBuffer(IStatus buildingStatus, 544 StringBuffer buffer, int nesting) { 545 if (!buildingStatus.matches(displayMask)) { 546 return; 547 } 548 for (int i = 0; i < nesting; i++) { 549 buffer.append(NESTING_INDENT); 550 } 551 buffer.append(buildingStatus.getMessage()); 552 buffer.append("\n"); 554 Throwable t = buildingStatus.getException(); 556 if (t instanceof CoreException) { 557 CoreException ce = (CoreException) t; 558 populateCopyBuffer(ce.getStatus(), buffer, nesting + 1); 559 } else if (t != null) { 560 for (int i = 0; i < nesting; i++) { 562 buffer.append(NESTING_INDENT); 563 } 564 String message = t.getLocalizedMessage(); 565 if (message == null) { 566 message = t.toString(); 567 } 568 buffer.append(message); 569 buffer.append("\n"); } 571 572 IStatus[] children = buildingStatus.getChildren(); 573 for (int i = 0; i < children.length; i++) { 574 populateCopyBuffer(children[i], buffer, nesting + 1); 575 } 576 } 577 578 581 private void copyToClipboard() { 582 if (clipboard != null) { 583 clipboard.dispose(); 584 } 585 StringBuffer statusBuffer = new StringBuffer (); 586 populateCopyBuffer(status, statusBuffer, 0); 587 clipboard = new Clipboard(list.getDisplay()); 588 clipboard.setContents(new Object [] { statusBuffer.toString() }, 589 new Transfer[] { TextTransfer.getInstance() }); 590 } 591 592 597 public boolean close() { 598 if (clipboard != null) { 599 clipboard.dispose(); 600 } 601 return super.close(); 602 } 603 604 614 protected final void showDetailsArea() { 615 if (!listCreated) { 616 Control control = getContents(); 617 if (control != null && !control.isDisposed()) { 618 toggleDetailsArea(); 619 } 620 } 621 } 622 623 633 protected boolean shouldShowDetailsButton() { 634 return status.isMultiStatus() || status.getException() != null; 635 } 636 637 646 protected final void setStatus(IStatus status) { 647 if (this.status != status) { 648 this.status = status; 649 } 650 shouldIncludeTopLevelErrorInDetails = true; 651 if (listCreated) { 652 repopulateList(); 653 } 654 } 655 656 659 private void repopulateList() { 660 if (list != null && !list.isDisposed()) { 661 list.removeAll(); 662 populateList(list); 663 } 664 } 665 666 669 int getColumnCount() { 670 if (Policy.getErrorSupportProvider() == null) 671 return 2; 672 return 3; 673 } 674 } 675 | Popular Tags |