From plugin to RCP: Save/Load functionality [message #246886] |
Thu, 15 January 2009 02:14  |
Eclipse User |
|
|
|
Originally posted by: el01120.mail.ntua.gr
Hello,
I have writing an eclipse plugin using GEF. Now I am migrating it to a
standalone RCP application. It went fairly smoothly, but I am a bit
confused with how files are loaded and saved in an RCP application.
In the plugin (class MyGraphicalEditor), there were the methods doSave(), doSaveAs() and setInput(), like this
(and like in the Shapes example):
public class MyGraphicalEditor extends EditorPart{
...
public void doSave(IProgressMonitor monitor) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
createOutputStream(out);
IFile file = ((IFileEditorInput) getEditorInput()).getFile();
file.setContents (new ByteArrayInputStream(out.toByteArray()), true, false, monitor);
getEditDomain().getCommandStack().markSaveLocation();
} catch (CoreException ce) {
ce.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
protected void setInput(IEditorInput input) {
super.setInput(input);
try {
IFile file = ((IFileEditorInput) input).getFile();
InputStream is = file.getContents();
if (is.available() == 0) root = new RootNode();
else {
ObjectInputStream in = new ObjectInputStream(is);
root = (RootNode) in.readObject();
in.close();
setPartName(file.getName());
}
}
catch (IOException e) {
handleLoadException(e);
} catch (CoreException e) {
handleLoadException(e);
} catch (ClassNotFoundException e) {
handleLoadException(e);
}
}
}
When moving to RCP, at first I get null pointer if I leave setInput() as it is.
I saw in an example a new class like this:
public class ApplicationWorkbenchAdvisor extends WorkbenchAdvisor {
...
@Override
public void postStartup() {
try {
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActi vePage();
page.openEditor(new MyEditorInput("editor"), MyGraphicalEditor.ID, false);
} catch (Exception e) {
e.printStackTrace();
}
}
From now on I am not at all sure what I have to do. I did create MyEditorInput and I modified setInput() in the editor like this:
protected void setInput(IEditorInput input) {
super.setInput(input);
MyEditorInput myeditorInput = ((MyEditorInput) input);
model = myeditorInput.getModel();
setPartName(sseditorInput.getName());
}
When running the application the model is loaded and I can edit it, but then doSave() and doSaveAs() have problems.
I don't know what interface MyEditorInput should implement. I guess something like IEditorInput/IFileEditorInput/IPathEditorInput.
I have not found clear documentation about this, and unfortunately the RCP Shapes example does not implement doSave(), doSaveAs().
I am not even sure that MyEditorInput has to exist? Maybe there is another way or small tweak that can be done in doSave() without it.
I would like to simply have the minimal functionality: When opening the editor, the user is editing an Untitled file and then he can
use save as to set the file name and location. Or maybe there is no file open at startup: the user creates a new file and then he can
use save. Either way is good.
What direction should I take? Is there a working example with source code?
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.04085 seconds