|
42 | 42 | import org.eclipse.core.runtime.IStatus; |
43 | 43 | import org.eclipse.core.runtime.MultiStatus; |
44 | 44 | import org.eclipse.core.runtime.Platform; |
45 | | -import org.eclipse.core.runtime.ProgressMonitorWrapper; |
46 | 45 | import org.eclipse.core.runtime.Status; |
47 | 46 | import org.eclipse.core.runtime.jobs.ISchedulingRule; |
48 | 47 | import org.eclipse.core.runtime.jobs.Job; |
|
58 | 57 | import org.eclipse.jface.util.Policy; |
59 | 58 | import org.eclipse.jface.window.Window; |
60 | 59 | import org.eclipse.swt.SWT; |
61 | | -import org.eclipse.swt.events.SelectionAdapter; |
62 | | -import org.eclipse.swt.events.SelectionEvent; |
| 60 | +import org.eclipse.swt.custom.BusyIndicator; |
63 | 61 | import org.eclipse.swt.graphics.Resource; |
64 | | -import org.eclipse.swt.widgets.Composite; |
65 | 62 | import org.eclipse.swt.widgets.Display; |
66 | 63 | import org.eclipse.swt.widgets.Listener; |
67 | | -import org.eclipse.swt.widgets.Shell; |
68 | 64 | import org.eclipse.ui.PlatformUI; |
69 | 65 | import org.eclipse.ui.application.IWorkbenchConfigurer; |
70 | 66 | import org.eclipse.ui.application.IWorkbenchWindowConfigurer; |
|
83 | 79 | import org.eclipse.ui.internal.ide.IDEWorkbenchMessages; |
84 | 80 | import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin; |
85 | 81 | import org.eclipse.ui.internal.ide.undo.WorkspaceUndoMonitor; |
| 82 | +import org.eclipse.ui.internal.progress.ProgressManagerUtil; |
86 | 83 | import org.eclipse.ui.internal.progress.ProgressMonitorJobsDialog; |
87 | 84 | import org.eclipse.ui.progress.IProgressService; |
88 | 85 | import org.eclipse.ui.statushandlers.AbstractStatusHandler; |
@@ -464,119 +461,61 @@ public IStatus runInWorkspace(IProgressMonitor monitor) throws CoreException { |
464 | 461 | } |
465 | 462 | } |
466 | 463 |
|
467 | | - protected static class CancelableProgressMonitorWrapper extends |
468 | | - ProgressMonitorWrapper { |
469 | | - private double total = 0; |
470 | | - private final ProgressMonitorJobsDialog dialog; |
471 | | - |
472 | | - CancelableProgressMonitorWrapper(IProgressMonitor monitor, |
473 | | - ProgressMonitorJobsDialog dialog) { |
474 | | - super(monitor); |
475 | | - this.dialog = dialog; |
476 | | - } |
477 | | - |
478 | | - @Override |
479 | | - public void internalWorked(double work) { |
480 | | - super.internalWorked(work); |
481 | | - total += work; |
482 | | - updateProgressDetails(); |
483 | | - } |
484 | | - |
485 | | - @Override |
486 | | - public void worked(int work) { |
487 | | - super.worked(work); |
488 | | - total += work; |
489 | | - updateProgressDetails(); |
490 | | - } |
491 | | - |
492 | | - @Override |
493 | | - public void beginTask(String name, int totalWork) { |
494 | | - super.beginTask(name, totalWork); |
495 | | - subTask(IDEWorkbenchMessages.IDEWorkbenchAdvisor_preHistoryCompaction); |
496 | | - } |
497 | | - |
498 | | - private void updateProgressDetails() { |
499 | | - if (!isCanceled() && Math.abs(total - 4.0) < 0.0001 /* right before history compacting */) { |
500 | | - subTask(IDEWorkbenchMessages.IDEWorkbenchAdvisor_cancelHistoryPruning); |
501 | | - dialog.setCancelable(true); |
502 | | - } |
503 | | - if (Math.abs(total - 5.0) < 0.0001 /* history compacting finished */) { |
504 | | - subTask(IDEWorkbenchMessages.IDEWorkbenchAdvisor_postHistoryCompaction); |
505 | | - dialog.setCancelable(false); |
506 | | - } |
507 | | - } |
508 | | - } |
509 | | - |
510 | | - protected static class CancelableProgressMonitorJobsDialog extends |
511 | | - ProgressMonitorJobsDialog { |
512 | | - |
513 | | - public CancelableProgressMonitorJobsDialog(Shell parent) { |
514 | | - super(parent); |
515 | | - } |
516 | | - |
517 | | - @Override |
518 | | - protected void createButtonsForButtonBar(Composite parent) { |
519 | | - super.createButtonsForButtonBar(parent); |
520 | | - registerCancelButtonListener(); |
521 | | - } |
522 | | - |
523 | | - public void registerCancelButtonListener() { |
524 | | - cancel.addSelectionListener(new SelectionAdapter() { |
525 | | - @Override |
526 | | - public void widgetSelected(SelectionEvent e) { |
527 | | - subTaskLabel.setText(""); //$NON-NLS-1$ |
528 | | - } |
529 | | - }); |
530 | | - } |
531 | | - } |
532 | | - |
533 | 464 | /** |
534 | 465 | * Disconnect from the core workspace. |
535 | 466 | * |
| 467 | + * Shows the progress dialog only if the save operation takes longer than |
| 468 | + * the {@link IProgressService#getLongOperationTime() long operation time}; |
| 469 | + * otherwise a busy cursor is shown while the save runs on a worker thread. |
| 470 | + * This avoids a flashing dialog for fast shutdowns. |
| 471 | + * |
536 | 472 | * Locks workspace in a background thread, should not be called while |
537 | 473 | * holding any workspace locks. |
538 | 474 | */ |
539 | 475 | protected void disconnectFromWorkspace() { |
540 | | - // save the workspace |
541 | 476 | final MultiStatus status = new MultiStatus(IDEWorkbenchPlugin.IDE_WORKBENCH, 1, |
542 | 477 | IDEWorkbenchMessages.ProblemSavingWorkbench); |
543 | | - try { |
544 | | - final ProgressMonitorJobsDialog p = new CancelableProgressMonitorJobsDialog( |
545 | | - null); |
546 | 478 |
|
547 | | - final boolean applyPolicy = ResourcesPlugin.getWorkspace() |
548 | | - .getDescription().isApplyFileStatePolicy(); |
| 479 | + final ProgressMonitorJobsDialog dialog = new ProgressMonitorJobsDialog(null); |
| 480 | + dialog.setOpenOnRun(false); |
549 | 481 |
|
550 | | - IRunnableWithProgress runnable = monitor -> { |
551 | | - try { |
552 | | - if (applyPolicy) { |
553 | | - monitor = new CancelableProgressMonitorWrapper(monitor, p); |
554 | | - } |
| 482 | + IRunnableWithProgress runnable = monitor -> { |
| 483 | + try { |
| 484 | + status.merge(((Workspace) ResourcesPlugin.getWorkspace()).save(true, true, monitor)); |
| 485 | + } catch (CoreException e) { |
| 486 | + status.merge(e.getStatus()); |
| 487 | + } |
| 488 | + }; |
555 | 489 |
|
556 | | - status.merge(((Workspace) ResourcesPlugin.getWorkspace()).save(true, true, monitor)); |
557 | | - } catch (CoreException e) { |
558 | | - status.merge(e.getStatus()); |
559 | | - } |
560 | | - }; |
| 490 | + final Display display = PlatformUI.getWorkbench().getDisplay(); |
| 491 | + final int longOperationTime = PlatformUI.getWorkbench().getProgressService().getLongOperationTime(); |
| 492 | + final Runnable openDialog = () -> { |
| 493 | + if (ProgressManagerUtil.safeToOpen(dialog, null)) { |
| 494 | + dialog.open(); |
| 495 | + } |
| 496 | + }; |
| 497 | + display.timerExec(longOperationTime, openDialog); |
561 | 498 |
|
562 | | - p.run(true, false, runnable); |
563 | | - } catch (InvocationTargetException e) { |
564 | | - status |
565 | | - .merge(new Status(IStatus.ERROR, |
566 | | - IDEWorkbenchPlugin.IDE_WORKBENCH, 1, |
567 | | - IDEWorkbenchMessages.InternalError, e |
568 | | - .getTargetException())); |
569 | | - } catch (InterruptedException e) { |
570 | | - status.merge(new Status(IStatus.ERROR, |
571 | | - IDEWorkbenchPlugin.IDE_WORKBENCH, 1, |
572 | | - IDEWorkbenchMessages.InternalError, e)); |
| 499 | + try { |
| 500 | + BusyIndicator.showWhile(display, () -> { |
| 501 | + try { |
| 502 | + dialog.run(true, false, runnable); |
| 503 | + } catch (InvocationTargetException e) { |
| 504 | + status.merge(new Status(IStatus.ERROR, IDEWorkbenchPlugin.IDE_WORKBENCH, 1, |
| 505 | + IDEWorkbenchMessages.InternalError, e.getTargetException())); |
| 506 | + } catch (InterruptedException e) { |
| 507 | + status.merge(new Status(IStatus.ERROR, IDEWorkbenchPlugin.IDE_WORKBENCH, 1, |
| 508 | + IDEWorkbenchMessages.InternalError, e)); |
| 509 | + } |
| 510 | + }); |
| 511 | + } finally { |
| 512 | + display.timerExec(-1, openDialog); |
573 | 513 | } |
| 514 | + |
574 | 515 | if (!status.isOK()) { |
575 | | - ErrorDialog.openError(null, |
576 | | - IDEWorkbenchMessages.ProblemsSavingWorkspace, null, status, |
| 516 | + ErrorDialog.openError(null, IDEWorkbenchMessages.ProblemsSavingWorkspace, null, status, |
577 | 517 | IStatus.ERROR | IStatus.WARNING); |
578 | | - IDEWorkbenchPlugin.log( |
579 | | - IDEWorkbenchMessages.ProblemsSavingWorkspace, status); |
| 518 | + IDEWorkbenchPlugin.log(IDEWorkbenchMessages.ProblemsSavingWorkspace, status); |
580 | 519 | } |
581 | 520 | } |
582 | 521 |
|
|
0 commit comments