Code and Stuff

Aug 10, 2011

Graphiti: Using Graphiti in an Eclipse RCP application

UPDATE 9 Aug. 2013

Information in this post is outdated and probably not best practice. I'm busy writing a new version of the post that should be simpler and work with recent version of Graphiti. I'll post a link here as soon as it is ready.



As many others I implemented the nice eclipse Graphiti tutorial and ran it as a plugin in eclipse. This works just fine and shows your diagram as expected.
Using it inside an RCP application was not described in the tutorial. Many posts on various forum suggested to use the code of the example org.eclipse.graphiti.examples.common.ui.CreateDiagramWizard. It works and it can be made also a little easier.
I assume that the diagram type is already declared in the plugin.

A custom Graphiti DiagramEditor

To have some more expansion possibilities I created a custom Graphiti editor by subclassing DiagramEditor and declaring it in the plugin.xml using the editor extension point.
<extension point="org.eclipse.ui.editors">
 <editor class="ch.idsia.diagram.MyGraphitiEditor" 
  id="ch.idsia.diagram.myDiagramEditor" 
  name="A diagram editor">
 </editor>
</extension>

Creating the diagram

Creating a new diagram will require the creation of the DiagramEditingDomain and the DiagramEditorInput. The actual creation of the editor is done the usual was via the openEditor method of the Workbech Page.
@Override
public Object execute(ExecutionEvent event) throws ExecutionException { 
 Diagram diagram = Graphiti.getPeCreateService().createDiagram(DIAG_TYPE_ID, "Name", true);
 String editorID = "ch.idsia.diagram.myDiagramEditor";
   
 TransactionalEditingDomain domain = createEmfFileForDiagram(diagram);
 String providerId = GraphitiUi.getExtensionManager().getDiagramTypeProviderId(diagram.getDiagramTypeId());
 DiagramEditorInput editorInput = new DiagramEditorInput(EcoreUtil.getURI(diagram), domain, providerId, true);
  
 try {
  HandlerUtil.getActiveWorkbenchWindow(event).getActivePage().openEditor(editorInput, editorID);
 } catch (PartInitException e) {
  // TODO let the user know
  return false;
 }
 return true;
}

Differently than the example I'm not using a FileService class an I simply added a method to the handler that creates the TransactionalEditingDomain.
/**
 * This code is mostly obtained from the org.eclipse.graphiti.examples.common.FileService class.
 */
public static TransactionalEditingDomain createEmfFileForDiagram(final Diagram diagram) {
 // Create a resource set and EditingDomain
 TransactionalEditingDomain editingDomain = DiagramEditorFactory.createResourceSetAndEditingDomain();
 ResourceSet resourceSet = editingDomain.getResourceSet();

 // Create a resource for this file.
 URI uri = URI.createFileURI("<untitled>.ext");  
 final Resource resource = resourceSet.createResource(uri);
  
 editingDomain.getCommandStack().execute(new RecordingCommand(editingDomain) {
  @Override
  protected void doExecute() {
   resource.setTrackingModification(true);
   resource.getContents().add(diagram);
  }
 });

 return editingDomain;
}

Opening a Diagram

To open a diagram a handler can use the openEditor method with an editor input and the ID of our declared editor as parameters. The editor input, however, must be a DiagramEditorInput, which is obtained by providing a URIEditorInput to the DiagramEditorInputFactory (other EditorInputs might be provided to the factory, have a look at the factory code for more info).
@Override
public void execute(ExecutionEvent event) throws ExecutionException {
 ... 
 // prompt the user for the filepath
 ...
 
 URI uri = URI.createFileURI(filePath);
 DiagramEditorFactory factory = new DiagramEditorFactory();
 DiagramEditorInput input = null;

 try {
  input = factory.createEditorInput(new URIEditorInput(uri));
 } catch(Exception exception) {
  // TODO let the user know that we did not manage to open the uri
  return false;
 }

 try {
  page.openEditor(input, "ch.idsia.diagram.MyDiagramEditor");
 } catch (PartInitException exception) {
  // TODO show a message
  return false;
 }
}

3 comments:

Administrator said...

Hi,

I found famous Null pointer exception at

resource.setTrackingModification(true);

That means "resource" are not created!!

Any suggestions are heartly welcomes.

satreth said...

Hi,
the resource is be null when no factory is registered. Please make sure you have org.eclipse.emf.ecore.xmi plugin in your dependencies.
If that does not work, you can try changing the extension of your file to xmi.
Hope this helps
David

Anudeep said...

can anyone let me know how to create a custom diagram file with coordinate information of all objects in diagram file....