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 :

» 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...

» e4: First e4 RCP Application

The next big thing in Eclipse is Eclipse 4.0 dubbed as e4. It will be released in 2010. That doesn't mean that the 3.x stream will be deprecated or discontinued. The 3.x releases will go on for "few" years till everyone boards the 4.0. But clearly...

» Opening an editor without IFile

When we use Eclipse, we are used to the notion of editors operating on a workspace file. But workspace and resources are optional for an RCP application. You might have to invoke an editor on a non-file object like a database record or an in-memory...

» Creating the defacto RCP Mail application

  In many of the RCP related tips in this website and other sites, the starting point would be this one liner "Create the RCP Mail application". Instead of repeatedly this step in those tips, I've put the steps in this item. Step 1: Open the "New"...

» How to add TrayItem in Eclipse RCP application?

In an application like Yahoo Messenger or Skype, you don't want to quit the application when the main window is closed. Most of these applications will move themselves to System Tray when the main window is closed and clicking on the Tray icon would...

Subscribe To

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