1 11 package org.eclipse.ui.internal.progress; 12 import java.util.ArrayList ; 13 import java.util.Collection ; 14 15 import org.eclipse.core.runtime.IStatus; 16 import org.eclipse.jface.dialogs.Dialog; 17 import org.eclipse.jface.dialogs.ErrorDialog; 18 import org.eclipse.jface.dialogs.IDialogConstants; 19 import org.eclipse.jface.resource.JFaceResources; 20 import org.eclipse.jface.viewers.IContentProvider; 21 import org.eclipse.jface.viewers.ILabelProviderListener; 22 import org.eclipse.jface.viewers.ISelection; 23 import org.eclipse.jface.viewers.ISelectionChangedListener; 24 import org.eclipse.jface.viewers.IStructuredContentProvider; 25 import org.eclipse.jface.viewers.IStructuredSelection; 26 import org.eclipse.jface.viewers.ITableLabelProvider; 27 import org.eclipse.jface.viewers.SelectionChangedEvent; 28 import org.eclipse.jface.viewers.TableViewer; 29 import org.eclipse.jface.viewers.Viewer; 30 import org.eclipse.jface.viewers.ViewerSorter; 31 import org.eclipse.swt.SWT; 32 import org.eclipse.swt.dnd.Clipboard; 33 import org.eclipse.swt.dnd.TextTransfer; 34 import org.eclipse.swt.dnd.Transfer; 35 import org.eclipse.swt.events.DisposeEvent; 36 import org.eclipse.swt.events.DisposeListener; 37 import org.eclipse.swt.events.MouseAdapter; 38 import org.eclipse.swt.events.MouseEvent; 39 import org.eclipse.swt.events.SelectionAdapter; 40 import org.eclipse.swt.events.SelectionEvent; 41 import org.eclipse.swt.events.SelectionListener; 42 import org.eclipse.swt.graphics.Image; 43 import org.eclipse.swt.graphics.Rectangle; 44 import org.eclipse.swt.layout.GridData; 45 import org.eclipse.swt.widgets.Button; 46 import org.eclipse.swt.widgets.Composite; 47 import org.eclipse.swt.widgets.Control; 48 import org.eclipse.swt.widgets.List; 49 import org.eclipse.swt.widgets.Menu; 50 import org.eclipse.swt.widgets.MenuItem; 51 import org.eclipse.swt.widgets.Shell; 52 56 public class ErrorNotificationDialog extends Dialog { 57 TableViewer errorViewer; 58 Button clearButton; 59 List detailsList; 60 private Clipboard clipboard; 61 private ErrorInfo selectedError = null; 62 63 66 private static final int DETAILS_LIST_ITEM_COUNT = 7; 67 68 71 private static final String NESTING_INDENT = " "; 73 78 public ErrorNotificationDialog(Shell parentShell) { 79 super(parentShell == null 80 ? ProgressManagerUtil.getDefaultParent() 81 : parentShell); 82 setBlockOnOpen(false); 83 setShellStyle(SWT.CLOSE | SWT.MODELESS | SWT.BORDER | SWT.TITLE 84 | SWT.RESIZE); 85 } 86 91 protected void configureShell(Shell newShell) { 92 super.configureShell(newShell); 93 newShell.setText(ProgressMessages 94 .getString("ErrorNotificationDialog.ErrorNotificationTitle")); newShell.addDisposeListener(new DisposeListener() { 96 101 public void widgetDisposed(DisposeEvent e) { 102 getManager().clearDialog(); 103 } 104 }); 105 } 106 107 112 protected int getShellStyle() { 113 return super.getShellStyle() | SWT.MIN; 114 } 115 120 protected Control createDialogArea(Composite parent) { 121 initializeDialogUnits(parent); 122 Composite topArea = (Composite) super.createDialogArea(parent); 123 errorViewer = new TableViewer(topArea, SWT.MULTI | SWT.H_SCROLL 124 | SWT.V_SCROLL | SWT.BORDER); 125 errorViewer.setSorter(getViewerSorter()); 126 errorViewer.getControl().addMouseListener(new MouseAdapter() { 127 132 public void mouseDoubleClick(MouseEvent e) { 133 openErrorDialog(); 134 } 135 }); 136 errorViewer 137 .addSelectionChangedListener(new ISelectionChangedListener() { 138 public void selectionChanged(SelectionChangedEvent event) { 139 140 clearButton.setEnabled(!errorViewer.getSelection() 141 .isEmpty()); 142 setDetailsContents(); 143 } 144 }); 145 Control control = errorViewer.getControl(); 146 GridData data = new GridData(GridData.FILL_BOTH 147 | GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL); 148 data.widthHint = convertWidthInCharsToPixels(60); 149 data.heightHint = convertHeightInCharsToPixels(10); 150 control.setLayoutData(data); 151 initContentProvider(); 152 initLabelProvider(); 153 applyDialogFont(parent); 154 155 createDetailsList(topArea); 156 return topArea; 157 } 158 163 protected void createButtonsForButtonBar(Composite parent) { 164 165 clearButton = createButton( 166 parent, 167 IDialogConstants.CLIENT_ID + 2, 168 ProgressMessages 169 .getString("ErrorNotificationDialog.ClearButtonTitle"), false); clearButton.setEnabled(false); 171 clearButton.addSelectionListener(new SelectionAdapter() { 172 public void widgetSelected(SelectionEvent e) { 173 ISelection rawSelection = errorViewer.getSelection(); 174 if (rawSelection != null 175 && rawSelection instanceof IStructuredSelection) { 176 IStructuredSelection selection = (IStructuredSelection) rawSelection; 177 getManager().removeErrors(selection.toList()); 178 } 179 refresh(); 180 } 181 }); 182 Button button = createButton(parent, IDialogConstants.CLOSE_ID, 183 IDialogConstants.CLOSE_LABEL, true); 184 button.addSelectionListener(new SelectionListener() { 185 190 public void widgetSelected(SelectionEvent e) { 191 close(); 192 } 193 198 public void widgetDefaultSelected(SelectionEvent e) { 199 close(); 200 } 201 }); 202 } 203 208 private ViewerSorter getViewerSorter() { 209 return new ViewerSorter() { 210 216 public int compare(Viewer testViewer, Object e1, Object e2) { 217 return ((Comparable ) e1).compareTo(e2); 218 } 219 }; 220 } 221 224 protected void initContentProvider() { 225 IContentProvider provider = new IStructuredContentProvider() { 226 231 public void dispose() { 232 } 234 239 public Object [] getElements(Object inputElement) { 240 return getManager().getErrors().toArray(); 241 } 242 248 public void inputChanged(Viewer viewer, Object oldInput, 249 Object newInput) { 250 if (newInput != null) 251 refresh(); 252 } 253 }; 254 errorViewer.setContentProvider(provider); 255 errorViewer.setInput(getManager()); 256 } 257 262 private ErrorNotificationManager getManager() { 263 return ProgressManager.getInstance().errorManager; 264 } 265 268 void refresh() { 269 errorViewer.refresh(); 270 } 271 private void initLabelProvider() { 272 ITableLabelProvider provider = new ITableLabelProvider() { 273 278 public void addListener(ILabelProviderListener listener) { 279 } 281 286 public void dispose() { 287 } 289 295 public Image getColumnImage(Object element, int columnIndex) { 296 return JFaceResources.getImageRegistry().get( 297 ErrorNotificationManager.ERROR_JOB_KEY); 298 } 299 305 public String getColumnText(Object element, int columnIndex) { 306 return ((ErrorInfo) element).getDisplayString(); 307 } 308 314 public boolean isLabelProperty(Object element, String property) { 315 return false; 316 } 317 322 public void removeListener(ILabelProviderListener listener) { 323 } 325 }; 326 errorViewer.setLabelProvider(provider); 327 } 328 331 private void openErrorDialog() { 332 ErrorInfo element = getSingleSelection(); 333 if (element == null) 334 return; 335 ErrorDialog.openError(getShell(), element.getDisplayString(), null, 336 element.getErrorStatus()); 337 } 338 344 private ErrorInfo getSingleSelection() { 345 ISelection rawSelection = errorViewer.getSelection(); 346 if (rawSelection != null 347 && rawSelection instanceof IStructuredSelection) { 348 IStructuredSelection selection = (IStructuredSelection) rawSelection; 349 if (selection.size() == 1) 350 return (ErrorInfo) selection.getFirstElement(); 351 } 352 return null; 353 } 354 359 public boolean close() { 360 getManager().clearAllErrors(); 361 Rectangle shellPosition = getShell().getBounds(); 362 boolean result = super.close(); 363 ProgressManagerUtil.animateDown(shellPosition); 364 return result; 365 } 366 367 372 protected void initializeBounds() { 373 super.initializeBounds(); 374 Rectangle shellPosition = getShell().getBounds(); 375 ProgressManagerUtil.animateUp(shellPosition); 376 } 377 378 384 private void createDetailsList(Composite detailsParent) { 385 detailsList = new List (detailsParent, SWT.BORDER | SWT.H_SCROLL 387 | SWT.V_SCROLL | SWT.MULTI); 388 389 GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL 390 | GridData.GRAB_HORIZONTAL | GridData.VERTICAL_ALIGN_FILL 391 | GridData.GRAB_VERTICAL); 392 data.heightHint = detailsList.getItemHeight() * DETAILS_LIST_ITEM_COUNT; 393 data.horizontalSpan = 2; 394 detailsList.setLayoutData(data); 395 Menu copyMenu = new Menu(detailsList); 396 MenuItem copyItem = new MenuItem(copyMenu, SWT.NONE); 397 copyItem.addSelectionListener(new SelectionListener() { 398 401 public void widgetSelected(SelectionEvent e) { 402 copyToClipboard(); 403 } 404 407 public void widgetDefaultSelected(SelectionEvent e) { 408 copyToClipboard(); 409 } 410 }); 411 copyItem.setText(JFaceResources.getString("copy")); detailsList.setMenu(copyMenu); 413 } 414 415 419 private void setDetailsContents() { 420 421 Collection statusList = new ArrayList (); 422 423 ErrorInfo info = getSingleSelection(); 424 425 if (info != null) { 426 selectedError = info; 427 statusList.add(selectedError.getErrorStatus().getMessage()); 428 if (selectedError.getErrorStatus().getException() != null) { 429 Throwable exception = selectedError.getErrorStatus() 430 .getException(); 431 statusList.add(exception.toString()); 432 StackTraceElement [] elements = exception.getStackTrace(); 433 for (int i = 0; i < elements.length; i++) { 434 statusList.add(elements[i].toString()); 435 } 436 } 437 IStatus[] statuses = (selectedError.getErrorStatus().getChildren()); 438 for (int i = 0; i < statuses.length; i++) { 439 statusList.add(NESTING_INDENT + statuses[i].getMessage()); 440 } 441 } 442 443 String [] items = new String [statusList.size()]; 444 statusList.toArray(items); 445 446 detailsList.setItems(items); 447 448 } 449 450 453 private void copyToClipboard() { 454 455 if (selectedError == null) 456 return; 457 458 if (clipboard != null) 459 clipboard.dispose(); 460 461 StringBuffer statusBuffer = new StringBuffer (); 462 populateCopyBuffer(selectedError.getErrorStatus(), statusBuffer, 0); 463 clipboard = new Clipboard(detailsList.getDisplay()); 464 clipboard.setContents(new Object []{statusBuffer.toString()}, 465 new Transfer[]{TextTransfer.getInstance()}); 466 } 467 468 475 private void populateCopyBuffer(IStatus buildingStatus, 476 StringBuffer buffer, int nesting) { 477 478 for (int i = 0; i < nesting; i++) { 479 buffer.append(NESTING_INDENT); } 481 buffer.append(buildingStatus.getMessage()); 482 483 if (buildingStatus.getException() != null) { 484 Throwable exception = buildingStatus.getException(); 485 buffer.append("\n"); buffer.append(exception.toString()); 487 StackTraceElement [] elements = exception.getStackTrace(); 488 for (int i = 0; i < elements.length; i++) { 489 buffer.append("\n"); buffer.append(elements[i].toString()); 491 492 } 493 } 494 495 buffer.append("\n"); IStatus[] children = buildingStatus.getChildren(); 497 for (int i = 0; i < children.length; i++) { 498 populateCopyBuffer(children[i], buffer, nesting + 1); 499 } 500 } 501 } | Popular Tags |