1 11 package org.eclipse.ui.internal.progress; 12 13 import org.eclipse.core.runtime.IProgressMonitor; 14 import org.eclipse.core.runtime.IProgressMonitorWithBlocking; 15 import org.eclipse.core.runtime.IStatus; 16 import org.eclipse.core.runtime.Status; 17 import org.eclipse.core.runtime.jobs.IJobChangeEvent; 18 import org.eclipse.core.runtime.jobs.IJobChangeListener; 19 import org.eclipse.core.runtime.jobs.Job; 20 import org.eclipse.core.runtime.jobs.JobChangeAdapter; 21 import org.eclipse.jface.dialogs.IDialogConstants; 22 import org.eclipse.swt.SWT; 23 import org.eclipse.swt.custom.BusyIndicator; 24 import org.eclipse.swt.events.SelectionAdapter; 25 import org.eclipse.swt.events.SelectionEvent; 26 import org.eclipse.swt.graphics.Rectangle; 27 import org.eclipse.swt.widgets.Button; 28 import org.eclipse.swt.widgets.Composite; 29 import org.eclipse.swt.widgets.Control; 30 import org.eclipse.swt.widgets.Display; 31 import org.eclipse.swt.widgets.Shell; 32 import org.eclipse.ui.PlatformUI; 33 import org.eclipse.ui.progress.IProgressConstants; 34 import org.eclipse.ui.progress.WorkbenchJob; 35 36 41 class ProgressMonitorFocusJobDialog extends ProgressMonitorJobsDialog { 42 Job job; 43 44 50 public ProgressMonitorFocusJobDialog(Shell parentShell) { 51 super(parentShell == null ? ProgressManagerUtil.getNonModalShell() 52 : parentShell); 53 setShellStyle(getDefaultOrientation() | SWT.BORDER | SWT.TITLE 54 | SWT.RESIZE | SWT.MODELESS); 55 setCancelable(true); 56 enableDetailsButton = true; 57 } 58 59 64 protected void cancelPressed() { 65 job.cancel(); 66 super.cancelPressed(); 67 } 68 69 74 protected void configureShell(Shell shell) { 75 super.configureShell(shell); 76 shell.setText(job.getName()); 77 78 } 79 80 85 protected void createButtonsForButtonBar(Composite parent) { 86 Button runInWorkspace = createButton( 87 parent, 88 IDialogConstants.CLOSE_ID, 89 ProgressMessages.ProgressMonitorFocusJobDialog_RunInBackgroundButton, 90 true); 91 runInWorkspace.addSelectionListener(new SelectionAdapter() { 92 97 public void widgetSelected(SelectionEvent e) { 98 Rectangle shellPosition = getShell().getBounds(); 99 job.setProperty(IProgressConstants.PROPERTY_IN_DIALOG, 100 Boolean.FALSE); 101 finishedRun(); 102 ProgressManagerUtil.animateDown(shellPosition); 103 } 104 }); 105 runInWorkspace.setCursor(arrowCursor); 106 107 cancel = createButton(parent, IDialogConstants.CANCEL_ID, 108 IDialogConstants.CANCEL_LABEL, false); 109 cancel.setCursor(arrowCursor); 110 111 createDetailsButton(parent); 112 } 113 114 119 private IJobChangeListener createCloseListener() { 120 return new JobChangeAdapter() { 121 126 public void done(IJobChangeEvent event) { 127 event.getJob().removeJobChangeListener(this); 129 if (!PlatformUI.isWorkbenchRunning()) { 130 return; 131 } 132 if (getShell() == null) { 134 return; 135 } 136 WorkbenchJob closeJob = new WorkbenchJob( 137 ProgressMessages.ProgressMonitorFocusJobDialog_CLoseDialogJob) { 138 143 public IStatus runInUIThread(IProgressMonitor monitor) { 144 Shell currentShell = getShell(); 145 if (currentShell == null || currentShell.isDisposed()) { 146 return Status.CANCEL_STATUS; 147 } 148 finishedRun(); 149 return Status.OK_STATUS; 150 } 151 }; 152 closeJob.setSystem(true); 153 closeJob.schedule(); 154 } 155 }; 156 } 157 158 163 private IProgressMonitorWithBlocking getBlockingProgressMonitor() { 164 return new IProgressMonitorWithBlocking() { 165 171 public void beginTask(String name, int totalWork) { 172 final String finalName = name; 173 final int finalWork = totalWork; 174 runAsync(new Runnable () { 175 180 public void run() { 181 getProgressMonitor().beginTask(finalName, finalWork); 182 } 183 }); 184 } 185 186 191 public void clearBlocked() { 192 runAsync(new Runnable () { 193 198 public void run() { 199 ((IProgressMonitorWithBlocking) getProgressMonitor()) 200 .clearBlocked(); 201 } 202 }); 203 } 204 205 210 public void done() { 211 runAsync(new Runnable () { 212 217 public void run() { 218 getProgressMonitor().done(); 219 } 220 }); 221 } 222 223 228 public void internalWorked(double work) { 229 final double finalWork = work; 230 runAsync(new Runnable () { 231 236 public void run() { 237 getProgressMonitor().internalWorked(finalWork); 238 } 239 }); 240 } 241 242 247 public boolean isCanceled() { 248 return getProgressMonitor().isCanceled(); 249 } 250 251 256 private void runAsync(final Runnable runnable) { 257 258 if (alreadyClosed) { 259 return; 260 } 261 Shell currentShell = getShell(); 262 263 Display display; 264 if (currentShell == null) { 265 display = Display.getDefault(); 266 } else { 267 if (currentShell.isDisposed()) return; 270 display = currentShell.getDisplay(); 271 } 272 273 display.asyncExec(new Runnable () { 274 279 public void run() { 280 if (alreadyClosed) { 281 return; } 284 Shell shell = getShell(); 285 if (shell != null && shell.isDisposed()) 286 return; 287 288 runnable.run(); 289 } 290 }); 291 } 292 293 298 public void setBlocked(IStatus reason) { 299 final IStatus finalReason = reason; 300 runAsync(new Runnable () { 301 306 public void run() { 307 ((IProgressMonitorWithBlocking) getProgressMonitor()) 308 .setBlocked(finalReason); 309 } 310 }); 311 } 312 313 318 public void setCanceled(boolean value) { 319 } 321 322 327 public void setTaskName(String name) { 328 final String finalName = name; 329 runAsync(new Runnable () { 330 335 public void run() { 336 getProgressMonitor().setTaskName(finalName); 337 } 338 }); 339 } 340 341 346 public void subTask(String name) { 347 final String finalName = name; 348 runAsync(new Runnable () { 349 354 public void run() { 355 getProgressMonitor().subTask(finalName); 356 } 357 }); 358 } 359 360 365 public void worked(int work) { 366 internalWorked(work); 367 } 368 }; 369 } 370 371 376 public int open() { 377 int result = super.open(); 378 379 IJobChangeListener listener = createCloseListener(); 381 job.addJobChangeListener(listener); 382 if (job.getState() == Job.NONE) { 383 job.removeJobChangeListener(listener); 386 finishedRun(); 387 cleanUpFinishedJob(); 388 } 389 390 return result; 391 } 392 393 401 public void show(Job jobToWatch, final Shell originatingShell) { 402 job = jobToWatch; 403 job.setProperty(IProgressConstants.PROPERTY_IN_DIALOG, Boolean.TRUE); 405 406 ProgressManager.getInstance().progressFor(job).addProgressListener( 407 getBlockingProgressMonitor()); 408 409 setOpenOnRun(false); 410 aboutToRun(); 411 BusyIndicator.showWhile(PlatformUI.getWorkbench().getDisplay(), 414 new Runnable () { 415 public void run() { 416 try { 417 Thread 418 .sleep(ProgressManagerUtil.SHORT_OPERATION_TIME); 419 } catch (InterruptedException e) { 420 } 423 } 424 }); 425 426 WorkbenchJob openJob = new WorkbenchJob( 427 ProgressMessages.ProgressMonitorFocusJobDialog_UserDialogJob) { 428 433 public IStatus runInUIThread(IProgressMonitor monitor) { 434 435 if (job.getState() == Job.NONE) { 437 finishedRun(); 438 cleanUpFinishedJob(); 439 return Status.CANCEL_STATUS; 440 } 441 442 if (!ProgressManagerUtil.safeToOpen( 444 ProgressMonitorFocusJobDialog.this, originatingShell)) { 445 return Status.CANCEL_STATUS; 446 } 447 448 if (getParentShell() != null && getParentShell().isDisposed()) { 450 return Status.CANCEL_STATUS; 451 } 452 453 open(); 454 455 return Status.OK_STATUS; 456 } 457 }; 458 openJob.setSystem(true); 459 openJob.schedule(); 460 461 } 462 463 467 private void cleanUpFinishedJob() { 468 ProgressManager.getInstance().checkForStaleness(job); 469 } 470 471 476 protected Control createDialogArea(Composite parent) { 477 Control area = super.createDialogArea(parent); 478 getProgressMonitor().setTaskName( 480 ProgressManager.getInstance().getJobInfo(this.job) 481 .getDisplayString()); 482 return area; 483 } 484 } 485 | Popular Tags |