1 11 package org.eclipse.ui.internal.progress; 12 13 import org.eclipse.core.runtime.IProgressMonitor; 14 import org.eclipse.core.runtime.IStatus; 15 import org.eclipse.core.runtime.Status; 16 import org.eclipse.jface.dialogs.IconAndMessageDialog; 17 import org.eclipse.jface.resource.JFaceResources; 18 import org.eclipse.jface.viewers.Viewer; 19 import org.eclipse.jface.viewers.ViewerComparator; 20 import org.eclipse.swt.SWT; 21 import org.eclipse.swt.graphics.Cursor; 22 import org.eclipse.swt.graphics.Image; 23 import org.eclipse.swt.layout.GridData; 24 import org.eclipse.swt.widgets.Button; 25 import org.eclipse.swt.widgets.Composite; 26 import org.eclipse.swt.widgets.Control; 27 import org.eclipse.swt.widgets.Shell; 28 import org.eclipse.ui.PlatformUI; 29 import org.eclipse.ui.internal.WorkbenchMessages; 30 import org.eclipse.ui.progress.WorkbenchJob; 31 32 36 public class BlockedJobsDialog extends IconAndMessageDialog { 37 42 protected static BlockedJobsDialog singleton; 43 44 47 private DetailedProgressViewer viewer; 48 49 52 private String blockedTaskName = null; 53 54 57 private Button cancelSelected; 58 59 62 private Cursor arrowCursor; 63 64 67 private Cursor waitCursor; 68 69 private IProgressMonitor blockingMonitor; 70 71 private JobTreeElement blockedElement = new BlockedUIElement(); 72 73 77 private class BlockedUIElement extends JobTreeElement { 78 79 84 Object [] getChildren() { 85 return ProgressManagerUtil.EMPTY_OBJECT_ARRAY; 86 } 87 88 93 String getDisplayString() { 94 if (blockedTaskName == null || blockedTaskName.length() == 0) { 95 return ProgressMessages.BlockedJobsDialog_UserInterfaceTreeElement; 96 } 97 return blockedTaskName; 98 } 99 100 105 public Image getDisplayImage() { 106 return JFaceResources.getImage(ProgressManager.WAITING_JOB_KEY); 107 } 108 109 114 Object getParent() { 115 return null; 116 } 117 118 123 boolean hasChildren() { 124 return false; 125 } 126 127 132 boolean isActive() { 133 return true; 134 } 135 136 141 boolean isJobInfo() { 142 return false; 143 } 144 145 150 public void cancel() { 151 blockingMonitor.setCanceled(true); 152 } 153 154 159 public boolean isCancellable() { 160 return true; 161 } 162 } 163 164 185 public static BlockedJobsDialog createBlockedDialog(Shell parentShell, 186 IProgressMonitor blockedMonitor, IStatus reason, String taskName) { 187 if (singleton != null) { 189 return singleton; 190 } 191 singleton = new BlockedJobsDialog(parentShell, blockedMonitor, reason); 192 193 if (taskName == null || taskName.length() == 0) 194 singleton 195 .setBlockedTaskName(ProgressMessages.BlockedJobsDialog_UserInterfaceTreeElement); 196 else 197 singleton.setBlockedTaskName(taskName); 198 199 203 if (parentShell == null) { 204 WorkbenchJob dialogJob = new WorkbenchJob( 206 WorkbenchMessages.EventLoopProgressMonitor_OpenDialogJobName) { 207 212 public IStatus runInUIThread(IProgressMonitor monitor) { 213 if (singleton == null) { 214 return Status.CANCEL_STATUS; 215 } 216 if (ProgressManagerUtil.rescheduleIfModalShellOpen(this)) { 217 return Status.CANCEL_STATUS; 218 } 219 singleton.open(); 220 return Status.OK_STATUS; 221 } 222 }; 223 dialogJob.setSystem(true); 226 dialogJob.schedule(PlatformUI.getWorkbench().getProgressService() 227 .getLongOperationTime()); 228 } else { 229 singleton.open(); 230 } 231 232 return singleton; 233 } 234 235 241 public static void clear(IProgressMonitor monitor) { 242 if (singleton == null) { 243 return; 244 } 245 singleton.close(monitor); 246 247 } 248 249 261 private BlockedJobsDialog(Shell parentShell, IProgressMonitor blocking, 262 IStatus blockingStatus) { 263 super(parentShell == null ? ProgressManagerUtil.getDefaultParent() 264 : parentShell); 265 blockingMonitor = blocking; 266 setShellStyle(SWT.BORDER | SWT.TITLE | SWT.APPLICATION_MODAL 267 | SWT.RESIZE | getDefaultOrientation()); 268 setBlockOnOpen(false); 270 setMessage(blockingStatus.getMessage()); 271 } 272 273 281 protected Control createDialogArea(Composite parent) { 282 setMessage(message); 283 createMessageArea(parent); 284 showJobDetails(parent); 285 return parent; 286 } 287 288 295 void showJobDetails(Composite parent) { 296 viewer = new DetailedProgressViewer(parent, SWT.MULTI | SWT.H_SCROLL 297 | SWT.V_SCROLL | SWT.BORDER); 298 viewer.setComparator(new ViewerComparator() { 299 305 public int compare(Viewer testViewer, Object e1, Object e2) { 306 return ((Comparable ) e1).compareTo(e2); 307 } 308 }); 309 ProgressViewerContentProvider provider = getContentProvider(); 310 viewer.setContentProvider(provider); 311 viewer.setInput(provider); 312 viewer.setLabelProvider(new ProgressLabelProvider()); 313 GridData data = new GridData(GridData.GRAB_HORIZONTAL 314 | GridData.GRAB_VERTICAL | GridData.FILL_BOTH); 315 data.horizontalSpan = 2; 316 int heightHint = convertHeightInCharsToPixels(10); 317 data.heightHint = heightHint; 318 viewer.getControl().setLayoutData(data); 319 } 320 321 326 private ProgressViewerContentProvider getContentProvider() { 327 return new ProgressViewerContentProvider(viewer, true, false) { 328 329 334 public Object [] getElements(Object inputElement) { 335 Object [] elements = super.getElements(inputElement); 336 Object [] result = new Object [elements.length + 1]; 337 System.arraycopy(elements, 0, result, 1, elements.length); 338 result[0] = blockedElement; 339 return result; 340 } 341 }; 342 } 343 344 347 private void clearCursors() { 348 clearCursor(cancelSelected); 349 clearCursor(getShell()); 350 if (arrowCursor != null) { 351 arrowCursor.dispose(); 352 } 353 if (waitCursor != null) { 354 waitCursor.dispose(); 355 } 356 arrowCursor = null; 357 waitCursor = null; 358 } 359 360 365 private void clearCursor(Control control) { 366 if (control != null && !control.isDisposed()) { 367 control.setCursor(null); 368 } 369 } 370 371 376 protected void configureShell(Shell shell) { 377 super.configureShell(shell); 378 shell.setText(ProgressMessages.BlockedJobsDialog_BlockedTitle); 379 if (waitCursor == null) { 380 waitCursor = new Cursor(shell.getDisplay(), SWT.CURSOR_WAIT); 381 } 382 shell.setCursor(waitCursor); 383 } 384 385 391 private void setMessage(String messageString) { 392 message = messageString == null ? "" : messageString; if (messageLabel == null || messageLabel.isDisposed()) { 395 return; 396 } 397 messageLabel.setText(message); 398 } 399 400 405 protected Image getImage() { 406 return getInfoImage(); 407 } 408 409 415 public IProgressMonitor getProgressMonitor() { 416 return blockingMonitor; 417 } 418 419 426 public boolean close(IProgressMonitor monitor) { 427 if (blockingMonitor != monitor) { 429 return false; 430 } 431 return close(); 432 } 433 434 439 public boolean close() { 440 singleton = null; 442 clearCursors(); 443 return super.close(); 444 } 445 446 451 protected Control createButtonBar(Composite parent) { 452 return parent; 454 } 455 456 460 void setBlockedTaskName(String taskName) { 461 this.blockedTaskName = taskName; 462 } 463 464 } 465 | Popular Tags |