Posted in
How To Guides
| Article Index |
|---|
| Progress Bars in Eclipse UI |
| Sub Tasks |
| Properties, Action & Command |
| All Pages |
When inspecting a bug, I found a interesting thing on Progress Monitors. We know monitor.worked() increments the progress bar, but how do we change the text to update the current subtask? The initial text is set by the beginTask() method and it should be called only once. I digged into the IProgressMonitor and found the subtask() method:
IRunnableWithProgress operation = new IRunnableWithProgress() {
public void run(IProgressMonitor monitor) {
monitor.beginTask("Main task running ...", 5);
for (int i = 0; i < 5; i++) {
monitor.subTask("Subtask # " + i + " running.");
runSubTask(new SubProgressMonitor(monitor, 1), i);
}
}
};

Now the question is what happens when the runSubTask() method sets another subTask on the SubProgressMonitor?
private void runSubTask(IProgressMonitor monitor, int subTaskId) {
monitor.beginTask("Sub task running", 10);
for (int i = 0; i < 10; i++) {
monitor.subTask("Inside subtask, " + i + " out of 10");
// do something here ...
monitor.worked(1);
if (monitor.isCanceled())
throw new OperationCanceledException();
}
monitor.done();
}
}

Basically the SubProgressMonitor's subTask() overwrites the parent's subTask(). Thats the default behaviour. You can customize it with the style bits provided in the SubProgressMonitor: If you want to append the SubProgressMonitor's subTask info, use the style PREPEND_MAIN_LABEL_TO_SUBTASK:
new SubProgressMonitor(monitor, 1, SubProgressMonitor.PREPEND_MAIN_LABEL_TO_SUBTASK), i);

Else if you want to ignore it altogher then use the SUPPRESS_SUBTASK_LABEL style:
new SubProgressMonitor(monitor, 1, SubProgressMonitor.SUPPRESS_SUBTASK_LABEL), i);
How do you show the partial results of a background job that is not yet completed? Quick answer is by adding an Action to that Job. The long answer is this tip. The Progress view shows all the jobs that are scheduled. It also shows the progress bar which is updated with the amount of work done. Consider this snippet:
Job job = new Job("My Job") {
@Override
protected IStatus run(IProgressMonitor monitor) {
monitor.beginTask("My job is working...", 100);
for (int i = 0; i < 100; i++) {
try {
Thread.sleep(200);
} catch (InterruptedException e) {} // ignore
monitor.worked(1);
}
monitor.done();
return new Status(IStatus.OK, Activator.PLUGIN_ID, "Job finished");
}
};
job.schedule();
The Progress view shows this job like this:

| < Prev | |
|---|---|


