Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Rich Client Platform (RCP) » How to implement save on an IEditorPart
How to implement save on an IEditorPart [message #1237701] Thu, 30 January 2014 11:17 Go to next message
Jan Krakora is currently offline 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 says
If 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 Go to previous message
Simon Scholz is currently offline Simon Scholz
Messages: 73
Registered: April 2012
Location: Germany
Member
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 Wink

Best regards,

Simon

[Updated on: Wed, 05 February 2014 16:23]

Report message to a moderator

Previous Topic:Read file in fragment in junit test
Next Topic:dynamic ContributionItems in toolbar are ignored completely
Goto Forum:
  


Current Time: Sun Oct 26 00:27:08 GMT 2014

Powered by FUDForum. Page generated in 0.01597 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software