The GMF database Editor
In this step of the tutorial the generated editor class will be changed to accomodate for a database resource. This editor is made available as a menu option in the main Eclipse window.
Change MindmapDiagramEditor
The generated MindmapDiagramEditor can be found in the org.eclipse.gmf.examples.mindmap.diagram.part package. The changes are required because the generated editor assumes that the information (resources) is stored in files. This assumption results in the following issues when directly using database resources:
- GMF checks if the file containing the data exists in the workspace, for a database resource this will not work and always result in a save-as dialog.
- GMF will consider the database uri as a read-only uri as it is not a standard platform uri.
To overcome these issue the following code has to be added to the MindmapDiagramEditor:
@Override // register the resource as not being readonly public void setInput(IEditorInput input) { // this part is copied from the super.setInput because // the catch block was hiding the underlying exception // for now a printStackTrace is added for better visibility // this should ofcourse be replaced by logging try { doSetInput(input, true); } catch (CoreException x) { x.printStackTrace(System.err); String title = x.getMessage(); String msg = x.getMessage(); Shell shell = getSite().getShell(); ErrorDialog.openError(shell, title, msg, x.getStatus()); } // set the resource in the resourcetoreadonly map final ResourceSet rs = getEditingDomain().getResourceSet(); for (Resource res : rs.getResources()) { ((AdapterFactoryEditingDomain) getEditingDomain()) .getResourceToReadOnlyMap().put(res, new Boolean(false)); } } @Override // implement a more simple save as there are no save-as dialogs involved public void doSave(IProgressMonitor progressMonitor) { updateState(getEditorInput()); validateState(getEditorInput()); performSave(false, progressMonitor); }
Open editor action
Then the next step is to actually open the editor directly without going through a open file dialog.
For this you can use the OpenMindmapDBEditor.java which can be downloaded here. This class is fairly simple, the relevant code is:
initializeData(); final IWorkbenchPage page = window.getActivePage(); page.openEditor(new URIEditorInput(StoreController.DATABASE_URI), MindmapDiagramEditor.ID);
The initializeData call is to a method (see the source file) which makes sure that there is at least one Map and Diagram instance in the db. This is required by the editor.
The most relevant part is opening the generated MindmapDiagramEditor using the Database Uri.
To make this ActionDelegate available as a global Eclipse menu option the following has to be added to the plugin.xml of the gmf diagram project (just before the closing plugin tag):
<!-- Adds a global menu, note is a rather simplistic method of adding a global menu option (is always visible). --> <extension id="mindmap.gmf.editor.actions" point="org.eclipse.ui.actionSets"> <actionSet id="mindmap.gmf.editor.actions" label="Mindmap DB" visible="true"> <menu id="mmMenu" label="Mindmap DB"> <separator name="mmgroup"> </separator> </menu> <action id="mindmap.teneo.gmf.action0" label="Open DB Editor" menubarPath="mmMenu/mmgroup" style="push"> <class class="org.eclipse.gmf.examples.mindmap.diagram.db.OpenMindmapDBEditor"> </class> </action> </actionSet> </extension>
The next step wraps it up and discusses running the diagram editor.