Search code examples
javaeclipse-plugineclipse-rcpjobse4

RCP: No progress dialog when starting a Job


When I start a Job I get the NullProgressMonitor by default instead of a progress dialog. How do I change this behavior?

Problem

According to this article under "Providing Feedback about Jobs" the only thing you need to set is job.setUser(true); to get a progress dialog. But this does not work for me.

To reproduce the problem I created a new Eclipse4 project with sample content and modified the created AboutHandler:

import org.eclipse.core.runtime.*;
import org.eclipse.core.runtime.jobs.*;
import org.eclipse.e4.core.di.annotations.*;
import org.eclipse.swt.widgets.*;

public class AboutHandler
{
  @Execute
  public void execute(Shell shell)
  {
    Job j = new YourThread(10);
    j.setUser(true);
    j.schedule();
  }

  private static class YourThread extends Job
  {
    private int workload;

    public YourThread(int workload)
    {
      super("Test");
      this.workload = workload;
    }

    @Override
    public IStatus run(IProgressMonitor monitor)
   {
      // Tell the user what you are doing
      monitor.beginTask("Copying files", workload);

      // Do your work
      for (int i = 0; i < workload; i++)
      {
        // Optionally add subtasks
        monitor.subTask("Copying file " + (i + 1) + " of " + workload + "...");
        System.out.println("Copying file " + (i + 1) + " of " + workload + "...");

       try
        {
          Thread.sleep(2000);
        }
        catch (InterruptedException e)
        {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }

        // Tell the monitor that you successfully finished one item of
        // "workload"-many
        monitor.worked(1);

        // Check if the user pressed "cancel"
        if (monitor.isCanceled())
        {
         monitor.done();
          return Status.CANCEL_STATUS;
        }
      }

      // You are done
      monitor.done();
      return Status.OK_STATUS;
    }
  }
}

When I press the About-menuitem the only thing I see is the println() and in the debugger I see that monitor is a NullProgressMonitor.

Is this the default behavior? And how do I change it?

Solution

I wrote my own IProgressMonitor as greg-449 suggested. I found out that the jFace ProgressMonitorDialog contains a IProgressMonitor and was a good reference for me.


Solution

  • For an e4 application it is your responsibility to provide a progress provider for the Job system using:

    Job.getJobManager().setProgressProvider(provider);
    

    where 'provider' is a class extending ProgressProvider. The provider returns the progress monitor to be used by the job.

    This article has some information about the progress provider.