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" 
  name="A diagram editor">

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.
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) {
  protected void doExecute() {

 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).
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;


Administrator said...


I found famous Null pointer exception at


That means "resource" are not created!!

Any suggestions are heartly welcomes.

satreth said...

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

Anudeep said...

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