Categories
eclipse eclipse-rcp java jface swt

Using ProgressMonitorDialog in Eclipse 4 properly

I have a set of APIs that do file operations e.g. saveToFile(CustomObject objectToSave);

Since a file operation could be lengthy I decided that some indication should be shown to the user e.g. progress bar.

I read about a ProgressMonitorDialog and so I tried it, but it doesn’t exactly work as I need (or better I don’t know how to use it properly).

Currently I do:

ProgressMonitorDialog progressDialog = new ProgressMonitorDialog(theShell);  
try {
progressDialog.run(false, true, new IRunnableWithProgress() {
@Override
public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
monitor.beginTask("Saving your data", 100);
try {
Utils.saveToFile(objectToSave);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
monitor.done();
}
});

This code shows a progress dialog very fast and ends, but the problem is that on a slower PC this would stack until the Utils.saveToFile returned, while I have no idea how to indicate intermediate process until the save is completed.

I found a thread mentioning about IProgressMonitor.UNKNOWN but it does not say about what happens in monitor during the performRead(_fileName, monitor);

How would I solve this?