How to implement save on an IEditorPart [message #1237701] |
Thu, 30 January 2014 11:17 |
Jan Krakora Messages: 477 Registered: December 2009 Location: Prague |
Senior Member |
|
|
Hi all,
I would like to implement doSave() method on my editor. But I'm a little confused.
1. The method is called by workbench from the UI thread. So the UI freeze when saving to a database take a while.
2. What am I supposed to do with the provided IProgressMonitor?
The javadoc saysIf the save is successful, the part should fire a property changed event reflecting the new dirty state (PROP_DIRTY property).
If the save is cancelled through user action, or for any other reason, the part should invoke setCancelled on the IProgressMonitor to inform the caller.
This method is long-running; progress and cancellation are provided by the given progress monitor.
Here is my testing code:
@Override
public void doSave(IProgressMonitor monitor) {
monitor.beginTask("Saving", IProgressMonitor.UNKNOWN);
try {
// simulate saving to a database
Thread.sleep(6000);
firePropertyChange(IEditorPart.PROP_DIRTY);
} catch (InterruptedException e) {
monitor.setCanceled(true);
} finally {
monitor.done();
}
}
I can imagine put the saving code in a Job, but the provided monitor really confuses me.
[Updated on: Thu, 30 January 2014 11:17] Report message to a moderator
|
|
|
Re: How to implement save on an IEditorPart [message #1240059 is a reply to message #1237701] |
Wed, 05 February 2014 16:09 |
|
Hi Jan,
Quote: 1. The method is called by workbench from the UI thread. So the UI freeze when saving to a database take a while.
In order to prevent the freeze of the UI you really should use a org.eclipse.core.runtime.jobs.Job.
Inside the org.eclipse.core.runtime.jobs.Job.run(IProgressMonitor) method you get an org.eclipse.core.runtime.IProgressMonitor to work with and to pass to the org.eclipse.ui.part.EditorPart.doSave(IProgressMonitor) method.
public class YourEditorSaveJob extends org.eclipse.core.runtime.jobs.Job{
private EditorPart editorPart;
public YourJob(org.eclipse.ui.part.EditorPart editorPart){
Assert.isNotNull(editorPart);
this.editorPart = editorPart;
}
protected IStatus run(IProgressMonitor monitor){
// try catch in case of runtimeexception or something similar
try {
this.editorPart.doSave(monitor);
} catch (Exception e) {
return new org.eclipse.core.runtime.Status(org.eclipse.core.runtime.IStatus.ERROR, "your.plugin.id", e.getMessage(), e);
} finally {
monitor.done();
}
if (monitor.isCanceled()) {
return org.eclipse.core.runtime.Status.CANCEL_STATUS;
}
return org.eclipse.core.runtime.Status.OK_STATUS;
}
}
Quote: 2. What am I supposed to do with the provided IProgressMonitor?
Inside your doSave method of your Editor you can add subtasks to the monitor in order to give feedback to the user or to react somehow, if the Job is canceled by the user.
Like you see in the code snippet, you can also return an IStatus according to the monitors state.
As an aside:
In case the save operation is not that heavy and does not cause a freeze of the UI you may also pass a org.eclipse.core.runtime.NullProgressMonitor, which internally does nothing but supports cancelation, to the save operation.
I hope this helps and do not hesitate to ask further questions
Best regards,
Simon
[Updated on: Wed, 05 February 2014 16:23] Report message to a moderator
|
|
|
Powered by
FUDForum. Page generated in 0.01594 seconds