Hi everybody,
I am trying to save the state of all currently opened editors into a "session" which can then later be restored. For this, I would also like to save the GUI state of the editors, e.g. whether the editor area has been split etc.
I tried to use the same mechanism that eclipse uses on shutdown to save the workbench state, but only for the editor area for a WorkbenchPage.
Saving already works, like this:
MUIElement area = ((WorkbenchPage) page).findSharedArea();
if (area instanceof MPlaceholder) {
area = ((MPlaceholder) area).getRef();
}
ResourceSetImpl resourceSetImpl = new ResourceSetImpl();
resourceSetImpl.getResourceFactoryRegistry().getExtensionToFactoryMap().put(Resource.Factory.Registry.DEFAULT_EXTENSION, new E4XMIResourceFactory());
resourceSetImpl.getPackageRegistry().put(ApplicationPackageImpl.eNS_URI, ApplicationPackageImpl.eINSTANCE);
resourceSetImpl.getPackageRegistry().put(CommandsPackageImpl.eNS_URI, CommandsPackageImpl.eINSTANCE);
resourceSetImpl.getPackageRegistry().put(UiPackageImpl.eNS_URI, UiPackageImpl.eINSTANCE);
resourceSetImpl.getPackageRegistry().put(MenuPackageImpl.eNS_URI, MenuPackageImpl.eINSTANCE);
resourceSetImpl.getPackageRegistry().put(BasicPackageImpl.eNS_URI, BasicPackageImpl.eINSTANCE);
resourceSetImpl.getPackageRegistry().put(AdvancedPackageImpl.eNS_URI, AdvancedPackageImpl.eINSTANCE);
resourceSetImpl.getPackageRegistry().put(org.eclipse.e4.ui.model.application.descriptor.basic.impl.BasicPackageImpl.eNS_URI, org.eclipse.e4.ui.model.application.descriptor.basic.impl.BasicPackageImpl.eINSTANCE);
URI uri = URI.createFileURI(PresentationPlugin.getDefault().getStateLocation().append("editors.xmi").toFile().getAbsolutePath());
Resource res = resourceSetImpl.createResource(uri);
EObject obj=EcoreUtil.copy((EObject)area);
res.getContents().add(obj);
res.save(null);
This creates a file ("editors.xmi") that contains the contents of the editor area.
I use the following code to recreate the MUIElement of the editor area:
URI uri = URI.createFileURI(PresentationPlugin.getDefault().getStateLocation().append("editors.xmi").toFile().getAbsolutePath());
Resource res = resourceSetImpl.getResource(uri, true);
MUIElement restoredArea = (MUIElement) res.getContents().get(0);
However, I did not yet find a way to exchange the current editor area with my restored one. I tried the following code:
MUIElement areaPlaceHolder = ((WorkbenchPage) page).findSharedArea();
if (areaPlaceHolder instanceof MPlaceholder) {
MUIElement oldArea = ((MPlaceholder) areaPlaceHolder).getRef();
restoredArea.setParent(oldArea.getParent());
restoredArea.setRenderer(oldArea.getRenderer());
((MPlaceholder) areaPlaceHolder).setRef(restoredArea);
}
Unfortunately, this does not exchange any editor and puts the workbench in a completely unusable state where I get all kinds of exceptions.
Is there a way to replace parts of the ui model with a model I loaded from file?
Best regards,
Christoph