9.4. Progress MonitorFor long-running operations, the progress monitor indicates what the operation is doing and an estimate of how much more there is left to be done. In the preceding code, a progress monitor was used to communicate with the user, indicating that resources were being deleted and how many resources needed to be deleted before the operation completed (see methods in previous sections and the redo() method in Section 8.5.3, Undo/Redo, on page 362). In addition, since DeleteResourcesOperation interacts with the user interface, isCanceled() is called periodically to see if the user has canceled the operation. There is nothing more frustrating than looking at a long running operation with a cancel button only to find out that the cancel button has no effect. 9.4.1. IProgressMonitorThe org.eclipse.core.runtime.IProgressMonitor interface provides methods for indicating when an operation has started, how much has been done, and when it is complete.
The IProgressMonitorWithBlocking interface extends IProgressMonitor for monitors that want to support feedback when an activity is blocked due to concurrent activity in another thread. If a running operation ever calls the setBlocked method listed below, it must eventually call clearBlocked before the operation completes.
9.4.2. Classes for displaying progressEclipse provides several classes that either implement the IProgressMonitor interface or provide a progress monitor via the IRunnableWithProgress interface. These classes are used under different circumstances to notify the user of the progress of long-running operations.
9.4.3. Workbench window status barThe workbench window provides a progress display area along the bottom edge of the window. Use the IWorkbenchWindow.run() method to execute the operation and the progress monitor passed to IRunnableWithProgress will be the progress monitor in the status bar. For example, the following snippet from an action delegate (see Section 6.2.6, Creating an action delegate, on page 216 for more on creating action delegates) shows simulated progress in the status bar: private IWorkbenchWindow window; public void init(IWorkbenchWindow window) { this.window = window; } public void run(IAction action) { try { window.run(true, true, new IRunnableWithProgress() { public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException { monitor.beginTask("simulate status bar progress:", 20); for (int i = 20; i > 0; --i) { monitor.subTask("seconds left = " + i); Thread.sleep(1000); monitor.worked(1); } monitor.done(); } }); } catch (InvocationTargetException e) { FavoritesLog.logError(e); } catch (InterruptedException e) { // User canceled the operation... just ignore. } } If you have a view or editor, you can obtain the containing IWorkbenchWindow via IWorkbenchPart, which both IViewPart and IEditorPart extend. IWorkbenchWindow window = viewOrEditor.getWorkbenchSite().getWorkbenchWindow(); You can also obtain the progress monitor in the status bar directly via the IStatusLineManager interface. viewPart.getViewSite().getActionBars() .getStatusLineManager().getProgressMonitor() or: editorPart.getEditorSite().getActionBars() .getStatusLineManager().getProgressMonitor() 9.4.4. IProgressServiceYet another mechanism for displaying progress in the workbench is using the IProgressService interface. While the run() method in IWorkbenchWindow displays progress in the status bar, the IProgressService interface displays progress using a subclass of ProgressMonitorDialog named TimeTriggeredProgressMonitorDialog. Although you could use a ProgressMonitorDialog, IProgressService only opens a progress dialog if the operation takes longer to execute than a specified amount of time (currently 800 milliseconds). window.getWorkbench().getProgressService().run(true, true, new IRunnableWithProgress() { public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException { monitor.beginTask("Simulated long running task #1", 60); for (int i = 60; i > 0; --i) { monitor.subTask("seconds left = " + i); if (monitor.isCanceled()) break; Thread.sleep(1000); monitor.worked(1); } monitor.done(); } }); Typically, jobs are executed in the background (see Section 20.8, Background TasksJobs API, on page 739), but the IProgressService provides the showInDialog() method for executing them in the foreground. |