1 11 package org.eclipse.ui.internal.progress; 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.ViewerSorter; 37 import org.eclipse.swt.SWT; 38 import org.eclipse.swt.events.DisposeListener; 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.WorkbenchPlugin; 49 import org.eclipse.ui.progress.IProgressConstants; 50 51 55 public class JobErrorDialog extends ErrorDialog { 56 57 61 private static final String PREF_SKIP_GOTO_ACTION_PROMPT = "pref_skip_goto_action_prompt"; 63 66 private static final int GOTO_ACTION_ID = IDialogConstants.CLIENT_ID + 1; 67 68 private TableViewer jobListViewer; 69 70 private ErrorInfo selectedError; 71 72 81 public JobErrorDialog(Shell parentShell, String title, String msg, 82 ErrorInfo errorInfo, int displayMask) { 83 super(parentShell, 84 title == null ? errorInfo.getJob().getName() : title, msg, 85 errorInfo.getErrorStatus(), displayMask); 86 setShellStyle(SWT.DIALOG_TRIM | SWT.MODELESS | SWT.RESIZE | SWT.MIN 87 | getDefaultOrientation()); this.selectedError = errorInfo; 89 setBlockOnOpen(false); 90 } 91 92 96 void refresh() { 97 98 if (AUTOMATED_MODE) { 99 return; 100 } 101 102 if (dialogArea == null || dialogArea.isDisposed()) { 105 return; 106 } 107 108 if (isMultipleJobErrors()) { 109 if (jobListViewer == null) { 110 setMessage(ProgressMessages.JobErrorDialog_MultipleErrorsMessage); 112 getShell().setText( 113 ProgressMessages.JobErrorDialog_MultipleErrorsTitle); 114 createJobListArea((Composite) dialogArea); 115 showDetailsArea(); 116 } 117 refreshJobList(); 118 } 119 updateEnablements(); 120 } 121 122 127 protected void createButtonsForButtonBar(Composite parent) { 128 IAction gotoAction = getGotoAction(); 129 String text = null; 130 if (gotoAction != null) { 131 text = gotoAction.getText(); 132 } 133 if (text == null) { 134 text = ProgressMessages.JobErrorDialog_CustomJobText; 136 } 137 createButton(parent, GOTO_ACTION_ID, text, false); 138 super.createButtonsForButtonBar(parent); 139 } 140 141 144 private void updateEnablements() { 145 Button details = getButton(IDialogConstants.DETAILS_ID); 146 if (details != null) { 147 details.setEnabled(selectedError.getErrorStatus().isMultiStatus() 148 || isMultipleJobErrors()); 149 } 150 Button gotoButton = getButton(GOTO_ACTION_ID); 151 if (gotoButton != null) { 152 IAction gotoAction = getGotoAction(); 153 boolean hasValidGotoAction = gotoAction != null; 154 String text = gotoButton.getText(); 155 String newText = null; 156 if (hasValidGotoAction) { 157 newText = gotoAction.getText(); 158 } 159 if (newText == null) { 160 hasValidGotoAction = false; 161 newText = ProgressMessages.JobErrorDialog_CustomJobText; 162 } 163 if (!newText.equals(text)) { 164 gotoButton.setText(newText); 165 } 166 gotoButton.setEnabled(hasValidGotoAction); 167 gotoButton.setVisible(hasValidGotoAction); 168 } 169 } 170 171 176 protected void buttonPressed(int id) { 177 if (id == GOTO_ACTION_ID) { 178 IAction gotoAction = getGotoAction(); 179 if (gotoAction != null) { 180 if (!isMultipleJobErrors() || isPromptToClose()) { 181 okPressed(); gotoAction.run(); } 184 } 185 } 186 super.buttonPressed(id); 187 } 188 189 193 private boolean isPromptToClose() { 194 IPreferenceStore store = WorkbenchPlugin.getDefault() 195 .getPreferenceStore(); 196 if (!store.contains(PREF_SKIP_GOTO_ACTION_PROMPT) 197 || !store.getString(PREF_SKIP_GOTO_ACTION_PROMPT).equals( 198 MessageDialogWithToggle.ALWAYS)) { 199 MessageDialogWithToggle dialog = MessageDialogWithToggle 200 .openOkCancelConfirm( 201 getShell(), 202 ProgressMessages.JobErrorDialog_CloseDialogTitle, 203 ProgressMessages.JobErrorDialog_CloseDialogMessage, 204 ProgressMessages.JobErrorDialog_DoNotShowAgainMessage, 205 false, store, PREF_SKIP_GOTO_ACTION_PROMPT); 206 return dialog.getReturnCode() == OK; 207 } 208 return true; 209 } 210 211 private IAction getGotoAction() { 212 Object property = selectedError.getJob().getProperty( 213 IProgressConstants.ACTION_PROPERTY); 214 if (property instanceof IAction) { 215 return (IAction) property; 216 } 217 return null; 218 } 219 220 226 private void setMessage(String messageString) { 227 message = messageString == null ? "" : messageString; if (messageLabel == null || messageLabel.isDisposed()) { 230 return; 231 } 232 messageLabel.setText(message); 233 } 234 235 242 private void createJobListArea(Composite parent) { 243 jobListViewer = new TableViewer(parent, SWT.SINGLE | SWT.H_SCROLL 245 | SWT.V_SCROLL | SWT.BORDER); 246 jobListViewer.setSorter(getViewerSorter()); 247 Control control = jobListViewer.getControl(); 248 GridData data = new GridData(GridData.FILL_BOTH 249 | GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL); 250 data.heightHint = convertHeightInCharsToPixels(10); 251 control.setLayoutData(data); 252 initContentProvider(); 253 initLabelProvider(); 254 jobListViewer 255 .addSelectionChangedListener(new ISelectionChangedListener() { 256 public void selectionChanged(SelectionChangedEvent event) { 257 handleSelectionChange(); 258 } 259 }); 260 applyDialogFont(parent); 261 } 262 263 266 private boolean isMultipleJobErrors() { 267 return getManager().getErrors().size() > 1; 268 } 269 270 273 private ErrorNotificationManager getManager() { 274 return ProgressManager.getInstance().errorManager; 275 } 276 277 282 public ErrorInfo getSelectedError() { 283 return selectedError; 284 } 285 286 291 private ViewerSorter getViewerSorter() { 292 return new ViewerSorter() { 293 299 public int compare(Viewer testViewer, Object e1, Object e2) { 300 return ((Comparable ) e1).compareTo(e2); 301 } 302 }; 303 } 304 305 308 protected void initContentProvider() { 309 IContentProvider provider = new IStructuredContentProvider() { 310 315 public void dispose() { 316 } 318 319 324 public Object [] getElements(Object inputElement) { 325 return getManager().getErrors().toArray(); 326 } 327 328 334 public void inputChanged(Viewer viewer, Object oldInput, 335 Object newInput) { 336 if (newInput != null) { 337 refreshJobList(); 338 } 339 } 340 }; 341 jobListViewer.setContentProvider(provider); 342 jobListViewer.setInput(getManager()); 343 jobListViewer.setSelection(new StructuredSelection(selectedError)); 344 } 345 346 349 void refreshJobList() { 350 if (jobListViewer != null && !jobListViewer.getControl().isDisposed()) { 351 jobListViewer.refresh(); 352 Point newSize = getShell().computeSize(SWT.DEFAULT, SWT.DEFAULT); 353 getShell().setSize(newSize); 354 } 355 setStatus(selectedError.getErrorStatus()); 356 } 357 358 private void initLabelProvider() { 359 ITableLabelProvider provider = new ITableLabelProvider() { 360 Map imageTable = new HashMap (); 361 362 367 public void addListener(ILabelProviderListener listener) { 368 } 370 371 376 public void dispose() { 377 if (!imageTable.isEmpty()) { 378 for (Iterator iter = imageTable.values().iterator(); iter 379 .hasNext();) { 380 Image image = (Image) iter.next(); 381 image.dispose(); 382 } 383 } 384 } 385 386 392 public Image getColumnImage(Object element, int columnIndex) { 393 return getIcon(((ErrorInfo) element).getJob()); 394 } 395 396 399 private Image getIcon(Job job) { 400 if (job != null) { 401 402 Object property = job 403 .getProperty(IProgressConstants.ICON_PROPERTY); 404 405 Image im = (Image) imageTable.get(property); 407 if (im != null) { 408 return im; 409 } 410 411 Display display = getShell().getDisplay(); 413 if (property instanceof ImageDescriptor) { 414 im = ((ImageDescriptor) property).createImage(display); 415 imageTable.put(property, im); } else if (property instanceof URL ) { 417 im = ImageDescriptor.createFromURL((URL ) property) 418 .createImage(display); 419 imageTable.put(property, im); } else { 421 im = ProgressManager.getInstance().getIconFor(job); 422 } 424 return im; 425 } 426 return null; 427 } 428 429 435 public String getColumnText(Object element, int columnIndex) { 436 return ((ErrorInfo) element).getDisplayString(); 437 } 438 439 445 public boolean isLabelProperty(Object element, String property) { 446 return false; 447 } 448 449 454 public void removeListener(ILabelProviderListener listener) { 455 } 457 }; 458 jobListViewer.setLabelProvider(provider); 459 } 460 461 467 private ErrorInfo getSingleSelection() { 468 ISelection rawSelection = jobListViewer.getSelection(); 469 if (rawSelection != null 470 && rawSelection instanceof IStructuredSelection) { 471 IStructuredSelection selection = (IStructuredSelection) rawSelection; 472 if (selection.size() == 1) { 473 return (ErrorInfo) selection.getFirstElement(); 474 } 475 } 476 return null; 477 } 478 479 public boolean close() { 480 Rectangle shellPosition = getShell().getBounds(); 481 boolean result = super.close(); 482 ProgressManagerUtil.animateDown(shellPosition); 483 return result; 484 } 485 486 491 protected void initializeBounds() { 492 refresh(); 496 super.initializeBounds(); 497 Rectangle shellPosition = getShell().getBounds(); 498 ProgressManagerUtil.animateUp(shellPosition); 499 } 500 501 505 void handleSelectionChange() { 506 ErrorInfo newSelection = getSingleSelection(); 507 if (newSelection != null && newSelection != selectedError) { 508 selectedError = newSelection; 509 setStatus(selectedError.getErrorStatus()); 510 updateEnablements(); 511 showDetailsArea(); 512 } 513 } 514 515 520 protected boolean shouldShowDetailsButton() { 521 return true; 522 } 523 524 529 protected void configureShell(Shell shell) { 530 super.configureShell(shell); 531 shell.addDisposeListener(new DisposeListener() { 532 535 public void widgetDisposed(org.eclipse.swt.events.DisposeEvent e) { 536 ProgressManager.getInstance().errorManager.dialogClosed(); 537 } 538 539 }); 540 } 541 } 542 | Popular Tags |