1 11 package org.eclipse.ui.internal.statushandlers; 12 13 import java.net.URL ; 14 import java.util.HashMap ; 15 import java.util.Iterator ; 16 import java.util.Map ; 17 18 import org.eclipse.core.runtime.jobs.Job; 19 import org.eclipse.jface.action.IAction; 20 import org.eclipse.jface.dialogs.ErrorDialog; 21 import org.eclipse.jface.dialogs.IDialogConstants; 22 import org.eclipse.jface.dialogs.MessageDialogWithToggle; 23 import org.eclipse.jface.preference.IPreferenceStore; 24 import org.eclipse.jface.resource.ImageDescriptor; 25 import org.eclipse.jface.viewers.IContentProvider; 26 import org.eclipse.jface.viewers.ILabelProviderListener; 27 import org.eclipse.jface.viewers.ISelection; 28 import org.eclipse.jface.viewers.ISelectionChangedListener; 29 import org.eclipse.jface.viewers.IStructuredContentProvider; 30 import org.eclipse.jface.viewers.IStructuredSelection; 31 import org.eclipse.jface.viewers.ITableLabelProvider; 32 import org.eclipse.jface.viewers.SelectionChangedEvent; 33 import org.eclipse.jface.viewers.StructuredSelection; 34 import org.eclipse.jface.viewers.TableViewer; 35 import org.eclipse.jface.viewers.Viewer; 36 import org.eclipse.jface.viewers.ViewerComparator; 37 import org.eclipse.osgi.util.NLS; 38 import org.eclipse.swt.SWT; 39 import org.eclipse.swt.graphics.Image; 40 import org.eclipse.swt.graphics.Point; 41 import org.eclipse.swt.graphics.Rectangle; 42 import org.eclipse.swt.layout.GridData; 43 import org.eclipse.swt.widgets.Button; 44 import org.eclipse.swt.widgets.Composite; 45 import org.eclipse.swt.widgets.Control; 46 import org.eclipse.swt.widgets.Display; 47 import org.eclipse.swt.widgets.Shell; 48 import org.eclipse.ui.internal.WorkbenchMessages; 49 import org.eclipse.ui.internal.WorkbenchPlugin; 50 import org.eclipse.ui.internal.progress.ProgressManager; 51 import org.eclipse.ui.internal.progress.ProgressManagerUtil; 52 import org.eclipse.ui.internal.progress.ProgressMessages; 53 import org.eclipse.ui.internal.statushandlers.StatusNotificationManager.StatusInfo; 54 import org.eclipse.ui.progress.IProgressConstants; 55 import org.eclipse.ui.statushandlers.StatusAdapter; 56 57 61 public class StatusDialog extends ErrorDialog { 62 63 67 private static final String PREF_SKIP_GOTO_ACTION_PROMPT = "pref_skip_goto_action_prompt"; 69 72 private static final int GOTO_ACTION_ID = IDialogConstants.CLIENT_ID + 1; 73 74 private TableViewer statusListViewer; 75 76 private StatusInfo selectedStatus; 77 78 86 public StatusDialog(Shell parentShell, StatusInfo statusInfo, 87 int displayMask, boolean modal) { 88 super(parentShell, (String )statusInfo.getStatus().getProperty( 89 StatusAdapter.TITLE_PROPERTY), statusInfo.getStatus() 90 .getStatus().getMessage(), statusInfo.getStatus().getStatus(), 91 displayMask); 92 setShellStyle(SWT.RESIZE | SWT.MIN | getShellStyle()); 93 this.selectedStatus = statusInfo; 94 setBlockOnOpen(false); 95 96 if (!modal) { 97 setShellStyle(~SWT.APPLICATION_MODAL & getShellStyle()); 98 } 99 100 String reason = WorkbenchMessages.StatusDialog_checkDetailsMessage; 101 if (statusInfo.getStatus().getStatus().getException() != null) { 102 reason = statusInfo.getStatus().getStatus().getException() 103 .getMessage() == null ? statusInfo.getStatus().getStatus() 104 .getException().toString() : statusInfo.getStatus() 105 .getStatus().getException().getMessage(); 106 } 107 this.message = NLS.bind(WorkbenchMessages.StatusDialog_reason, 108 new Object [] { statusInfo.getDisplayString(), reason }); 109 } 110 111 115 void refresh() { 116 117 if (AUTOMATED_MODE) { 118 return; 119 } 120 121 if (dialogArea == null || dialogArea.isDisposed()) { 124 return; 125 } 126 127 if (isMultipleStatusDialog()) { 128 if (statusListViewer == null) { 129 setMessage(ProgressMessages.JobErrorDialog_MultipleErrorsMessage); 131 getShell().setText( 132 ProgressMessages.JobErrorDialog_MultipleErrorsTitle); 133 createStatusListArea((Composite) dialogArea); 134 showDetailsArea(); 135 } 136 refreshStatusList(); 137 } 138 updateEnablements(); 139 } 140 141 146 protected void createButtonsForButtonBar(Composite parent) { 147 IAction gotoAction = getGotoAction(); 148 String text = null; 149 if (gotoAction != null) { 150 text = gotoAction.getText(); 151 } 152 if (text == null) { 153 text = ProgressMessages.JobErrorDialog_CustomJobText; 155 } 156 createButton(parent, GOTO_ACTION_ID, text, false); 157 super.createButtonsForButtonBar(parent); 158 } 159 160 163 private void updateEnablements() { 164 Button details = getButton(IDialogConstants.DETAILS_ID); 165 if (details != null) { 166 details.setEnabled(true); 167 } 168 Button gotoButton = getButton(GOTO_ACTION_ID); 169 if (gotoButton != null) { 170 IAction gotoAction = getGotoAction(); 171 boolean hasValidGotoAction = gotoAction != null; 172 String text = gotoButton.getText(); 173 String newText = null; 174 if (hasValidGotoAction) { 175 newText = gotoAction.getText(); 176 } 177 if (newText == null) { 178 hasValidGotoAction = false; 179 newText = ProgressMessages.JobErrorDialog_CustomJobText; 180 } 181 if (!newText.equals(text)) { 182 gotoButton.setText(newText); 183 } 184 gotoButton.setEnabled(hasValidGotoAction); 185 gotoButton.setVisible(hasValidGotoAction); 186 } 187 } 188 189 194 protected void buttonPressed(int id) { 195 if (id == GOTO_ACTION_ID) { 196 IAction gotoAction = getGotoAction(); 197 if (gotoAction != null) { 198 if (!isMultipleStatusDialog() || isPromptToClose()) { 199 okPressed(); gotoAction.run(); } 202 } 203 } 204 super.buttonPressed(id); 205 } 206 207 public boolean isModal() 208 { 209 return ((getShellStyle() & SWT.APPLICATION_MODAL) == SWT.APPLICATION_MODAL); 210 } 211 212 216 private boolean isPromptToClose() { 217 IPreferenceStore store = WorkbenchPlugin.getDefault() 218 .getPreferenceStore(); 219 if (!store.contains(PREF_SKIP_GOTO_ACTION_PROMPT) 220 || !store.getString(PREF_SKIP_GOTO_ACTION_PROMPT).equals( 221 MessageDialogWithToggle.ALWAYS)) { 222 MessageDialogWithToggle dialog = MessageDialogWithToggle 223 .openOkCancelConfirm( 224 getShell(), 225 ProgressMessages.JobErrorDialog_CloseDialogTitle, 226 ProgressMessages.JobErrorDialog_CloseDialogMessage, 227 ProgressMessages.JobErrorDialog_DoNotShowAgainMessage, 228 false, store, PREF_SKIP_GOTO_ACTION_PROMPT); 229 return dialog.getReturnCode() == OK; 230 } 231 return true; 232 } 233 234 private IAction getGotoAction() { 235 Object property = null; 236 237 StatusAdapter statusAdapter = selectedStatus.getStatus(); 238 Job job = (Job) (statusAdapter.getAdapter(Job.class)); 239 if (job != null) { 240 property = job.getProperty(IProgressConstants.ACTION_PROPERTY); 241 } 242 243 if (property instanceof IAction) { 244 return (IAction) property; 245 } 246 return null; 247 } 248 249 255 private void setMessage(String messageString) { 256 message = messageString == null ? "" : messageString; if (messageLabel == null || messageLabel.isDisposed()) { 259 return; 260 } 261 messageLabel.setText(message); 262 } 263 264 271 private void createStatusListArea(Composite parent) { 272 statusListViewer = new TableViewer(parent, SWT.SINGLE | SWT.H_SCROLL 274 | SWT.V_SCROLL | SWT.BORDER); 275 statusListViewer.setComparator(getViewerComparator()); 276 Control control = statusListViewer.getControl(); 277 GridData data = new GridData(GridData.FILL_BOTH 278 | GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL); 279 data.heightHint = convertHeightInCharsToPixels(10); 280 control.setLayoutData(data); 281 initContentProvider(); 282 initLabelProvider(); 283 statusListViewer 284 .addSelectionChangedListener(new ISelectionChangedListener() { 285 public void selectionChanged(SelectionChangedEvent event) { 286 handleSelectionChange(); 287 } 288 }); 289 applyDialogFont(parent); 290 } 291 292 295 private boolean isMultipleStatusDialog() { 296 return getManager().getErrors().size() > 1; 297 } 298 299 302 private StatusNotificationManager getManager() { 303 return StatusNotificationManager.getInstance(); 304 } 305 306 311 public StatusInfo getSelectedError() { 312 return selectedStatus; 313 } 314 315 320 private ViewerComparator getViewerComparator() { 321 return new ViewerComparator() { 322 328 public int compare(Viewer testViewer, Object e1, Object e2) { 329 return ((Comparable ) e1).compareTo(e2); 330 } 331 }; 332 } 333 334 337 protected void initContentProvider() { 338 IContentProvider provider = new IStructuredContentProvider() { 339 344 public void dispose() { 345 } 347 348 353 public Object [] getElements(Object inputElement) { 354 return getManager().getErrors().toArray(); 355 } 356 357 363 public void inputChanged(Viewer viewer, Object oldInput, 364 Object newInput) { 365 if (newInput != null) { 366 refreshStatusList(); 367 } 368 } 369 }; 370 statusListViewer.setContentProvider(provider); 371 statusListViewer.setInput(getManager()); 372 statusListViewer.setSelection(new StructuredSelection(selectedStatus)); 373 } 374 375 378 void refreshStatusList() { 379 if (statusListViewer != null 380 && !statusListViewer.getControl().isDisposed()) { 381 statusListViewer.refresh(); 382 Point newSize = getShell().computeSize(SWT.DEFAULT, SWT.DEFAULT); 383 getShell().setSize(newSize); 384 } 385 setStatus(selectedStatus.getStatus().getStatus()); 386 } 387 388 private void initLabelProvider() { 389 ITableLabelProvider provider = new ITableLabelProvider() { 390 Map imageTable = new HashMap (); 391 392 397 public void addListener(ILabelProviderListener listener) { 398 } 400 401 406 public void dispose() { 407 if (!imageTable.isEmpty()) { 408 for (Iterator iter = imageTable.values().iterator(); iter 409 .hasNext();) { 410 Image image = (Image) iter.next(); 411 image.dispose(); 412 } 413 } 414 } 415 416 422 public Image getColumnImage(Object element, int columnIndex) { 423 if (element != null) { 424 StatusAdapter statusAdapter = ((StatusInfo) element) 425 .getStatus(); 426 Job job = (Job) (statusAdapter.getAdapter(Job.class)); 427 if (job != null) { 428 return getIcon(job); 429 } 430 } 431 return null; 432 } 433 434 437 private Image getIcon(Job job) { 438 if (job != null) { 439 440 Object property = job 441 .getProperty(IProgressConstants.ICON_PROPERTY); 442 443 Image im = (Image) imageTable.get(property); 445 if (im != null) { 446 return im; 447 } 448 449 Display display = getShell().getDisplay(); 451 if (property instanceof ImageDescriptor) { 452 im = ((ImageDescriptor) property).createImage(display); 453 imageTable.put(property, im); } else if (property instanceof URL ) { 455 im = ImageDescriptor.createFromURL((URL ) property) 456 .createImage(display); 457 imageTable.put(property, im); } else { 459 im = ProgressManager.getInstance().getIconFor(job); 460 } 462 return im; 463 } 464 return null; 465 } 466 467 473 public String getColumnText(Object element, int columnIndex) { 474 return ((StatusInfo) element).getDisplayString(); 475 } 476 477 483 public boolean isLabelProperty(Object element, String property) { 484 return false; 485 } 486 487 492 public void removeListener(ILabelProviderListener listener) { 493 } 495 }; 496 statusListViewer.setLabelProvider(provider); 497 } 498 499 505 private StatusInfo getSingleSelection() { 506 ISelection rawSelection = statusListViewer.getSelection(); 507 if (rawSelection != null 508 && rawSelection instanceof IStructuredSelection) { 509 IStructuredSelection selection = (IStructuredSelection) rawSelection; 510 if (selection.size() == 1) { 511 return (StatusInfo) selection.getFirstElement(); 512 } 513 } 514 return null; 515 } 516 517 public boolean close() { 518 Rectangle shellPosition = getShell().getBounds(); 519 boolean result = super.close(); 520 ProgressManagerUtil.animateDown(shellPosition); 521 return result; 522 } 523 524 public int open() { 525 int result = super.open(); 526 setStatus(selectedStatus.getStatus().getStatus()); 527 return result; 528 } 529 530 535 protected void initializeBounds() { 536 refresh(); 540 super.initializeBounds(); 541 Rectangle shellPosition = getShell().getBounds(); 542 ProgressManagerUtil.animateUp(shellPosition); 543 } 544 545 549 void handleSelectionChange() { 550 StatusInfo newSelection = getSingleSelection(); 551 if (newSelection != null && newSelection != selectedStatus) { 552 selectedStatus = newSelection; 553 setStatus(selectedStatus.getStatus().getStatus()); 554 updateEnablements(); 555 showDetailsArea(); 556 } 557 } 558 559 564 protected boolean shouldShowDetailsButton() { 565 return true; 566 } 567 } 568 | Popular Tags |