Search code examples
javaeclipse-plugineclipse-rcprcpjobs

Add a progress bar to a Job in RCP


I have a time consuming job in my RCp application in which I read out a big database and save it in a file. I am running this job in a separate thread so that the UI of my app is not blocked but I dont know how to add a progress bar to this task

My code:

public class MirrorFeatureModel extends Job {

protected class MutexRule implements ISchedulingRule {
    public boolean isConflicting(ISchedulingRule rule) {
         return rule == this;
      }
    public boolean contains(ISchedulingRule rule) {
         return rule == this;
      }
}

private String source;
private String template;
private String target;

public MirrorFeatureModel(String name) {
    super(name);
}

public MirrorFeatureModel(String source, String template, String target){
    super("Mirroring SWA Model");
    this.source = source;
    this.template = template;
    this.target = target;
}

public void run() {
    this.schedule();
}


@Override
protected IStatus run(IProgressMonitor monitor)  {

    ILog logView = Activator.getDefault().getLog();
    String connectionString = this.source;
    String emptyEAP = this.template;
    String target = this.target;

    try{

        monitor.beginTask("Copying swa model to local", 1);
        new Mirror(connectionString, emptyEAP,target).run();
        monitor.worked(1);

    }catch(final Exception e) {

        logView.log(new Status(Status.ERROR, null , "Failed to create local SAM instance", e));         


        Display.getDefault().asyncExec(new Runnable() {

            @Override
            public void run() {
                MessageDialog.openError(PlatformUI.getWorkbench().getDisplay().getActiveShell(), "Failed to create local SAM instance", e.getMessage());
            }

        });
        return Status.CANCEL_STATUS;
    }       

    return Status.OK_STATUS;
  }


}

Mirror.run() Method

 public void run(IProgressMonitor monitor) {

    try{

        if (monitor != null)
            monitor.subTask("Creating directory for eap file");

        File tempTarget=File.createTempFile("eap-mirror", "eap");           
        if (monitor != null)
            monitor.worked(1);  

        try {

            if (monitor != null)
                monitor.subTask("Establising connection");

            this.source=DriverManager.getConnection(com.intel.imc.swa.easql.EaDbStringParser.eaDbStringToJdbc(sourceString));
            this.source.setReadOnly(true);

            if(monitor != null) 
                monitor.worked(1);

            FileUtils.copyFile(new File(templateFileString), tempTarget);

            if (monitor != null)
                monitor.subTask("Opening database"); 

            this.target=Database.open(tempTarget,false,false);

            if(monitor != null)     
                monitor.worked(1);

            if (monitor != null)
                monitor.subTask("Mirroring tables"); 

            Collection<String> tables=selectTables(source);
            long time=System.currentTimeMillis();
            for (String tableName : tables) {
                long tTime=System.currentTimeMillis();
                Table table=target.getTable(tableName);
                System.out.print("Mirroring table "+tableName+"...");
                table.setOverrideAutonumber(true);
                copyTable(table, source, target);
                    System.out.println(" took "+(System.currentTimeMillis()-tTime));
            }

            System.out.println("Done. Overall time: "+(System.currentTimeMillis()-time));
            if(monitor != null)     
                monitor.worked(1); 

        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
    }
}

How can I add a progress bar in this class that shows me the progress of this job

Thanks


Solution

  • Normally jobs show a progress indicator in the progress view and in the status line at the bottom of the main window.

    If you call setUser(true) before schedule() then the job will show a pop-up progress dialog if the job runs for more than a few seconds.

    To show job progress you must specify the total amount of work on the beginTask call

    monitor.beginTask("...", total work);
    

    and then you must call monitor.worked(xxx) each time you have done part of the work.