Article Index
Progress Bars in Eclipse UI
Sub Tasks
Properties, Action & Command
All Pages

In the "Top 10 mistakes done by Eclipse plug-in developers" article, I said the top most mistake is running long running operations in the UI thread. Assuming that you are running it in a non-UI thread, how to show the progress of the execution? Obviously thru progress monitors. But how many different ways are there to show a progress monitor? Lets see them in this tip.

 

The first option is ProgressMonitorDialog. You have to create a IRunnableWithProgress and use the run method:

ProgressMonitorDialog dialog = new ProgressMonitorDialog(shell);    
dialog.run(true, true, new IRunnableWithProgress(){
     public void run(IProgressMonitor monitor) {
         monitor.beginTask("Some nice progress message here ...", 100);
         // execute the task ...
         monitor.done();
     }
 });

Result:

ProgressMonitorDialog

 

As you can see, the dialog is kind-of blocking the user. This dialog is advisable only in the rare cases where the user have to wait till the operation is completed. In most other cases, you should prefer running a Job. A Job is not blocking to the user and can still show the progress. The progress is available in the Progress View.

  Job job = new Job("My new job") {
     @Override
     protected IStatus run(IProgressMonitor monitor) {
         monitor.beginTask("Some nice progress message here ...", 100);
         // execute the task ...
         monitor.done();
         return Status.OK_STATUS;
     }
 };
 job.schedule();

Result:

Job in Progress View

If you make the job as a user job by calling setUser(true), the progress dialog will be opened, where the user can chose to run the job as a background job. If the user doesn't want to see this dialog again, he can select to 'Always run in background': When the job is initiated by the user interacting with a workbench part (like clicking a button in a view), a better way to schedule the job is thru the site's IWorkbencSiteProgressService.schedule. When the job begins to execute, the workbench part can show it visually. The default behaviour is to italize the part name:

 

WorkbenchPart.showBusy

 

But you can customize it by overriding the showBusy() method in your WorkbenchPart:

 public void showBusy(boolean busy) {
     super.showBusy(busy);
     if(busy)
         setPartName("I'm doing a job right now...");
     else
         setPartName("Sample View");
 }

Result:

WorkbenchPart.showBusy

 

In case your action doesn't have any WorkbenchPart associated with it, you can use the IWorkbenchWindow.run(...) to show the progress: IWorkbenchWindow.run

 

If you are running a long running operation in a wizard/wizardPage you should consider running it thru getContainer().run(). This will show the progressBar right there in the wizard dialog itself:

 

progressbar in wizard

If you are updating the UI elements in the wizardPage during the execution, you have to note that if you do a setEnabled() on any widget, it won't reflect. Its because the container registers the enabled state of the widgets before execution; disables the widgets; executes the task and restores the widget's enabled state to the saved value. So if you have changed the state during the operation, it will be overwritten by the container. In case, you can't display a progressbar, you should at least use BusyIndicator.showWhile() and display the busy cursor to show that some operation is being executed. Else the user might be wondering why the UI is frozen.

 



More articles :

» Extending FilteredItemsSelectionDialog

In a previous tip, you have seen various . One thing which was not explained in it was FilteredItemsSelectionDialog, as it deserves a tip on its own. In this tip, I'll explain how to extend that class.

» How to create a new File Wizard?

Problem is simple. Implement a INewWizard for creating some file type in your application. For this problem, I have seen people start creating the WizardPage on their own. But luckily there is a reusable WizardPage which is specifically meant for...

» Adding a new editorAction for Orion

In case you have not heard it yet,  is the new Web IDE from Eclipse. In a blog entry , I said that this one is going to stand out from the crowd and going to rule the world. Why can't the other Web IDEs do that? Because, Orion also would be walking...

» API Tooling Tutorial

IntroductionRecently, while fixing a bug, there was a discussion in the emails going on whether adding a new field to an interface in the API would break the binary compatibility to the existing clients. I strongly believed that only adding a method...

» Selection Dialogs in Eclipse

If you are an Eclipse Plug-in developer, you must have used the MessageDialog. There are many other Dialogs provided by Eclipse Platform are reusable and part of the API. I'll try to explain the various selection dialogs that I know of. In case I've...

Subscribe To

Unless stated, all the text contents of this site is available under Eclipse Public License