Bug 540469 - [Gendoc] [Sirius] Sirius 6.0.0 throws a clast cast
exception to org.eclipse.sirius.diagram.ui.tools.internal.part.SiriusDiagramGraphicalViewer

Change-Id: Id666205a96cec90f49a76808ab8660b6d5119aa6
diff --git a/plugins/org.eclipse.gendoc.bundle.acceleo.gmf/plugin.xml b/plugins/org.eclipse.gendoc.bundle.acceleo.gmf/plugin.xml
index 814a25d..3b19cea 100644
--- a/plugins/org.eclipse.gendoc.bundle.acceleo.gmf/plugin.xml
+++ b/plugins/org.eclipse.gendoc.bundle.acceleo.gmf/plugin.xml
@@ -9,5 +9,21 @@
             name="gmf">
       </service>
    </extension>
+   <extension
+         point="org.eclipse.gendoc.serviceTypes">
+      <serviceType
+            id="DiagramRenderer"
+            interface="org.eclipse.gendoc.bundle.acceleo.gmf.service.IDiagramRenderer">
+      </serviceType>
+   </extension>
+   <extension
+         point="org.eclipse.gendoc.services">
+      <service
+            class="org.eclipse.gendoc.bundle.acceleo.gmf.service.GMFDiagramRenderer"
+            default="true"
+            id="GMFDiagramRenderer"
+            serviceType="DiagramRenderer">
+      </service>
+   </extension>
 
 </plugin>
diff --git a/plugins/org.eclipse.gendoc.bundle.acceleo.gmf/src/org/eclipse/gendoc/bundle/acceleo/gmf/impl/GMFDiagramRunnable.java b/plugins/org.eclipse.gendoc.bundle.acceleo.gmf/src/org/eclipse/gendoc/bundle/acceleo/gmf/impl/GMFDiagramRunnable.java
index 2cdca6a..8259028 100644
--- a/plugins/org.eclipse.gendoc.bundle.acceleo.gmf/src/org/eclipse/gendoc/bundle/acceleo/gmf/impl/GMFDiagramRunnable.java
+++ b/plugins/org.eclipse.gendoc.bundle.acceleo.gmf/src/org/eclipse/gendoc/bundle/acceleo/gmf/impl/GMFDiagramRunnable.java
@@ -14,78 +14,42 @@
 package org.eclipse.gendoc.bundle.acceleo.gmf.impl;
 
 import java.io.File;
-import java.util.ArrayList;
-import java.util.Collection;
 import java.util.Collections;
-import java.util.HashSet;
-import java.util.LinkedList;
 import java.util.List;
-import java.util.Stack;
 
 import org.eclipse.core.databinding.observable.Realm;
-import org.eclipse.core.resources.IFile;
-import org.eclipse.core.runtime.Assert;
 import org.eclipse.core.runtime.CoreException;
 import org.eclipse.core.runtime.IPath;
 import org.eclipse.core.runtime.IStatus;
 import org.eclipse.core.runtime.NullProgressMonitor;
 import org.eclipse.core.runtime.Path;
-import org.eclipse.core.runtime.Status;
 import org.eclipse.emf.common.util.BasicDiagnostic;
 import org.eclipse.emf.common.util.Diagnostic;
-import org.eclipse.emf.common.util.TreeIterator;
 import org.eclipse.emf.ecore.EObject;
 import org.eclipse.emf.ecore.resource.Resource;
 import org.eclipse.emf.ecore.resource.ResourceSet;
-import org.eclipse.emf.ecore.util.EcoreUtil;
 import org.eclipse.emf.transaction.TransactionalEditingDomain;
 import org.eclipse.emf.transaction.TransactionalEditingDomain.Factory;
 import org.eclipse.emf.transaction.util.TransactionUtil;
-import org.eclipse.emf.workspace.util.WorkspaceSynchronizer;
-import org.eclipse.gef.ConnectionEditPart;
-import org.eclipse.gef.EditPart;
-import org.eclipse.gef.GraphicalEditPart;
+import org.eclipse.gendoc.bundle.acceleo.gmf.service.GMFDiagramRenderer;
+import org.eclipse.gendoc.bundle.acceleo.gmf.service.IDiagramRenderer;
+import org.eclipse.gendoc.bundle.acceleo.gmf.service.IDiagramRenderer.FileFormat;
 import org.eclipse.gendoc.documents.FileRunnable;
 import org.eclipse.gendoc.documents.IImageManipulationService;
 import org.eclipse.gendoc.documents.IImageManipulationServiceFactory;
 import org.eclipse.gendoc.services.GendocServices;
 import org.eclipse.gendoc.services.IGendocDiagnostician;
-import org.eclipse.gendoc.services.ILogger;
 import org.eclipse.gendoc.tags.handlers.Activator;
-import org.eclipse.gmf.runtime.common.core.util.Trace;
 import org.eclipse.gmf.runtime.diagram.core.preferences.PreferencesHint;
-import org.eclipse.gmf.runtime.diagram.core.util.ViewUtil;
-import org.eclipse.gmf.runtime.diagram.ui.editparts.DiagramEditPart;
 import org.eclipse.gmf.runtime.diagram.ui.image.ImageFileFormat;
-import org.eclipse.gmf.runtime.diagram.ui.parts.DiagramEditor;
-import org.eclipse.gmf.runtime.diagram.ui.render.clipboard.DiagramGenerator;
-import org.eclipse.gmf.runtime.diagram.ui.render.internal.DiagramUIRenderPlugin;
-import org.eclipse.gmf.runtime.diagram.ui.render.util.CopyToImageUtil;
-import org.eclipse.gmf.runtime.diagram.ui.util.DiagramEditorUtil;
 import org.eclipse.gmf.runtime.notation.Diagram;
-import org.eclipse.gmf.runtime.notation.View;
 import org.eclipse.jface.databinding.swt.SWTObservables;
 import org.eclipse.swt.widgets.Display;
-import org.eclipse.swt.widgets.Shell;
 
 public class GMFDiagramRunnable implements FileRunnable {
 
 	private Diagram diagram;
 
-	private enum FileFormat {
-		PNG, JPEG, GIF, BMP, JPG, SVG, EMF;
-		
-		public FileFormat getExtension(){
-			IImageManipulationServiceFactory imageManipulationServiceFactory = GendocServices.getDefault().getService(IImageManipulationServiceFactory.class);
-        	IImageManipulationService imageManipulationService = imageManipulationServiceFactory.getService(name().toLowerCase()) ;
-			return transformToFormat(imageManipulationService.renameExtension(name()));
-		}
-		
-		public String getFullExtension() {
-        	return "." + getExtension().name().toLowerCase();
-		}
-	}
-
 	/** The extension of diagram. */
 	private FileFormat extension;
 	private final List<EObject> visibleElements;
@@ -97,7 +61,7 @@
 	public GMFDiagramRunnable(Diagram diagram, String extension,
 			List<EObject> visibleElements) {
 		this.diagram = diagram;
-		this.extension = transformToFormat(extension);
+		this.extension = FileFormat.transformToFormat(extension);
 		if (visibleElements != null) {
 			this.visibleElements = visibleElements;
 		} else {
@@ -106,22 +70,6 @@
 
 	}
 
-	private static FileFormat transformToFormat(String ext) {
-		FileFormat format;
-		try {
-			format = FileFormat.valueOf(ext.toUpperCase());
-			return format;
-		} catch (IllegalArgumentException e) {
-			IGendocDiagnostician diagnostician = GendocServices.getDefault()
-					.getService(IGendocDiagnostician.class);
-			String message = "The format " + ext + " is not supported";
-			diagnostician.addDiagnostic(new BasicDiagnostic(Diagnostic.ERROR,
-					Activator.PLUGIN_ID, 0, message, null));
-			return FileFormat.valueOf(ext);
-		}
-
-	}
-
 	/**
 	 * Instantiates a new diagram runnable.
 	 * 
@@ -144,7 +92,10 @@
 	
 	protected void doRun(String resourceId, String outputResourceFolder) {
 		if (extension != null) {
-			MultiElementsCopytoImageUtils c = new MultiElementsCopytoImageUtils();
+			IDiagramRenderer diagramRenderer = GendocServices.getDefault().getService(IDiagramRenderer.class);
+			if (diagramRenderer == null)
+				diagramRenderer = new GMFDiagramRenderer();
+			
 			new File(outputResourceFolder).mkdirs();
 			IPath path = new Path(outputResourceFolder + "/" + resourceId
 					+ extension.getFullExtension());
@@ -163,16 +114,9 @@
 						}
 					}
 				}
-				if (visibleElements == null || visibleElements.isEmpty()) {
-					c.copyToImage(diagram, path, getImageFileFormat(extension.getExtension()),
-							new NullProgressMonitor(),
-							PreferencesHint.USE_DEFAULTS);
-				} else {
-					c.copyToImage(diagram, path, visibleElements,
-							getImageFileFormat(extension.getExtension()),
-							new NullProgressMonitor(),
-							PreferencesHint.USE_DEFAULTS);
-				}
+				
+				diagramRenderer.renderDiagram(diagram, visibleElements, path, extension, new NullProgressMonitor());
+
 				IImageManipulationServiceFactory imageManipulationServiceFactory = GendocServices.getDefault().getService(IImageManipulationServiceFactory.class);
 	        	IImageManipulationService imageManipulationService = imageManipulationServiceFactory.getService(extension.name().toLowerCase()) ;
 	        	imageManipulationService.transform(path);
@@ -196,314 +140,8 @@
 
 	}
 
-	private ImageFileFormat getImageFileFormat(FileFormat format) {
-		return ImageFileFormat.resolveImageFormat(format.name());
-	}
-
 	public String getFileExtension() {
 		return extension.toString().toLowerCase();
 	}
 
-	/**
-	 * A subclass to manage list of elements and diagram in parameter
-	 * 
-	 * @author tfaure
-	 * 
-	 */
-	public class MultiElementsCopytoImageUtils extends CopyToImageUtil {
-
-		public List copyToImage(Diagram diagram, IPath destination,
-				ImageFileFormat format, NullProgressMonitor monitor,
-				PreferencesHint preferencesHint)
-						throws CoreException {
-
-			Trace.trace(DiagramUIRenderPlugin.getInstance(),
-					"Copy diagram to Image " + destination + " as " + format); //$NON-NLS-1$ //$NON-NLS-2$
-
-			List partInfo = Collections.EMPTY_LIST;
-
-			DiagramEditor openedDiagramEditor = findOpenedDiagramEditor(diagram);
-			if (openedDiagramEditor != null) {
-				DiagramGenerator generator = copyToImage(openedDiagramEditor.getDiagramEditPart(),
-						destination, format, monitor);
-				partInfo = generator.getDiagramPartInfo(openedDiagramEditor.getDiagramEditPart());
-			} else {
-				Shell shell = new Shell();
-				try {
-					DiagramEditPart diagramEditPart = createDiagramEditPart(diagram,
-							shell, preferencesHint);
-					Assert.isNotNull(diagramEditPart);
-					DiagramGenerator generator = copyToImage(diagramEditPart,
-							destination, format, monitor);
-					partInfo = generator.getDiagramPartInfo(diagramEditPart);
-				} finally {
-					shell.dispose();
-				}
-			}
-
-			return partInfo;
-		}
-
-		private DiagramEditor findOpenedDiagramEditor(Diagram diagram) {
-			DiagramEditor result = DiagramEditorUtil.findOpenedDiagramEditorForID(ViewUtil.getIdStr(diagram));
-			if (result != null){
-				IPath iPathDiagEditor =getIPath(result.getDiagram());
-				IPath iPathDiag = getIPath(diagram) ;
-
-				if (iPathDiagEditor == null || iPathDiag == null || !iPathDiag.equals(iPathDiagEditor)){
-					((ILogger) GendocServices.getDefault().getService(ILogger.class)).log("Two diagrams in separate files " + iPathDiagEditor + " and " + iPathDiag + " have the same identifier", Status.WARNING);
-					return null ;
-				}
-			}
-			return result ;
-		}
-
-		private IPath getIPath(Diagram diagram) {
-			if (diagram != null){
-				Resource resource = diagram.eResource();
-				if (resource != null){
-					IFile file = WorkspaceSynchronizer.getUnderlyingFile(resource);
-					if (file != null){
-						return file.getFullPath();
-					}
-				}
-			}
-			return null;
-		}
-
-		public List copyToImage(Diagram diagram, IPath destination,
-				List<EObject> visibleElements, ImageFileFormat format,
-				NullProgressMonitor monitor, PreferencesHint preferencesHint)
-						throws CoreException {
-			Shell shell = null ;
-			try {
-				Trace.trace(DiagramUIRenderPlugin.getInstance(),
-						"Copy diagram to Image " + destination + " as " + format); //$NON-NLS-1$ //$NON-NLS-2$
-
-				List partInfo = Collections.EMPTY_LIST;
-
-				DiagramEditor openedDiagramEditor = findOpenedDiagramEditor(diagram);
-				DiagramEditPart diagramEditPart = null ;
-
-				if (openedDiagramEditor != null) {
-					diagramEditPart = openedDiagramEditor.getDiagramEditPart(); 
-				} else {
-					shell = new Shell();
-					diagramEditPart = createDiagramEditPart(
-							diagram, shell, preferencesHint);
-				}
-				Assert.isNotNull(diagramEditPart);
-				copyToImage(diagramEditPart,
-						getEditParts(visibleElements, diagramEditPart),
-						destination, format, monitor);
-				return partInfo;
-			} finally {
-				if (shell != null && !shell.isDisposed())
-				{
-					shell.dispose();
-				}
-			}
-		}
-
-	}
-
-	public List<?> getEditParts(List<EObject> visibleElements,
-			DiagramEditPart diagramEditPart) {
-		return getEditParts(visibleElements, diagramEditPart, true);
-	}
-
-	public List<?> getEditParts(List<EObject> visibleElements,
-			DiagramEditPart diagramEditPart, boolean includeConnections) {
-		List<GraphicalEditPart> result = new LinkedList<GraphicalEditPart>();
-		for (EObject e : visibleElements) {
-			Object model = diagramEditPart.getModel();
-			if (model instanceof Diagram) {
-				Diagram diagram = (Diagram) model;
-				for (TreeIterator<EObject> i = EcoreUtil.getAllProperContents(
-						diagram, true); i.hasNext();) {
-					EObject current = i.next();
-					if (current instanceof View) {
-						View view = (View) current;
-						if (equals(e, view.getElement())) {
-							Object part = diagramEditPart.getViewer()
-									.getEditPartRegistry().get(view);
-							if (part instanceof GraphicalEditPart) {
-								result.add((GraphicalEditPart) part);
-							}
-						}
-					}
-				}
-			}
-		}
-		if (includeConnections)
-		{
-			// the process is made twice but copying the code 
-			// in this case is better than overriding to avoid maintenance problem
-			ArrayList<GraphicalEditPart> tmp = new ArrayList<GraphicalEditPart>(result);
-			for(GraphicalEditPart g : tmp)
-			{
-				result.addAll(findConnectionsToPaint(g, result));
-			}
-		}
-		return result;
-	}
-
-	protected boolean equals(EObject e, EObject fromView) {
-		boolean result = false ;
-		if (fromView == e)
-		{
-			result = true ;
-		}
-		else
-		{
-			// if the diagram editor is the opened one the eobjects are not the same
-			if (e.eResource() != null && fromView.eResource() != null)
-			{
-				Resource eResource =  e.eResource();
-				Resource viewResoure =  fromView.eResource();
-				if (eResource.getURI() != null && eResource.getURI().equals(viewResoure.getURI()))
-				{
-					result = (eResource.getURIFragment(e) != null && eResource.getURIFragment(e).equals(viewResoure.getURIFragment(fromView)));
-				}
-			}
-		}
-		return result ;
-	}
-
-	/**
-	 * Collects all connections contained within the given edit part
-	 * Code copy from {@link CopyToImageUtil}
-	 * @param editPart
-	 *            the container editpart
-	 * @return connections within it
-	 */
-	protected Collection<ConnectionEditPart> findConnectionsToPaint(
-			GraphicalEditPart editPart, List<GraphicalEditPart> relatedEditParts) {
-		/*
-		 * Set of node editparts contained within the given editpart
-		 */
-		HashSet<GraphicalEditPart> editParts = new HashSet<GraphicalEditPart>();
-
-		/*
-		 * All connection editparts that have a source contained within the
-		 * given editpart
-		 */
-		HashSet<ConnectionEditPart> connectionEPs = new HashSet<ConnectionEditPart>();
-
-		/*
-		 * Connections contained within the given editpart (or just the
-		 * connections to paint
-		 */
-		HashSet<ConnectionEditPart> connectionsToPaint = new HashSet<ConnectionEditPart>();
-
-		/*
-		 * Populate the set of node editparts
-		 */
-		getNestedEditParts(editPart, editParts);
-
-		/*
-		 * Populate the set of connections whose source is within the given
-		 * editpart
-		 */
-		for (GraphicalEditPart gep : editParts) {
-			connectionEPs.addAll(getAllConnectionsFrom(gep));
-		}
-
-		/*
-		 * Populate the set of connections whose source is the given editpart
-		 */
-		connectionEPs.addAll(getAllConnectionsFrom(editPart));
-
-		/*
-		 * Create a set of connections constained within the given editpart
-		 */
-		while (!connectionEPs.isEmpty()) {
-			/*
-			 * Take the first connection and check whethe there is a path
-			 * through that connection that leads to the target contained within
-			 * the given editpart
-			 */
-			Stack<ConnectionEditPart> connectionsPath = new Stack<ConnectionEditPart>();
-			ConnectionEditPart conn = connectionEPs.iterator().next();
-			connectionEPs.remove(conn);
-			connectionsPath.add(conn);
-
-			/*
-			 * Initialize the target for the current path
-			 */
-			EditPart target = conn.getTarget();
-			while (connectionEPs.contains(target)) {
-				/*
-				 * If the target end is a connection, check if it's one of the
-				 * connection's whose target is a connection and within the
-				 * given editpart. Append it to the path if it is. Otherwise
-				 * check if the target is within the actual connections or nodes
-				 * contained within the given editpart
-				 */
-				ConnectionEditPart targetConn = (ConnectionEditPart) target;
-				connectionEPs.remove(targetConn);
-				connectionsPath.add(targetConn);
-
-				/*
-				 * Update the target for the new path
-				 */
-				target = targetConn.getTarget();
-			}
-
-			/*
-			 * The path is built, check if it's target is a node or a connection
-			 * contained within the given editpart
-			 */
-			if (editParts.contains(target)
-					|| connectionsToPaint.contains(target)
-					|| relatedEditParts.contains(target)) {
-				connectionsToPaint.addAll(connectionsPath);
-			}
-		}
-		return connectionsToPaint;
-	}
-
-	/**
-	 * This method is used to obtain the list of child edit parts for shape
-	 * compartments.
-	 * 
-	 * @param childEditPart
-	 *            base edit part to get the list of children editparts
-	 * @param editParts
-	 *            list of nested shape edit parts
-	 */
-	protected void getNestedEditParts(GraphicalEditPart baseEditPart,
-			Collection<GraphicalEditPart> editParts) {
-
-		for (Object child : baseEditPart.getChildren()) {
-			if (child instanceof GraphicalEditPart) {
-				GraphicalEditPart childEP = (GraphicalEditPart) child;
-				editParts.add(childEP);
-				getNestedEditParts(childEP, editParts);
-			}
-		}
-	}
-
-	/**
-	 * Returns all connections orginating from a given editpart. All means that
-	 * connections originating from connections that have a source given
-	 * editpart will be included
-	 * 
-	 * @param ep
-	 *            the editpart
-	 * @return all source connections
-	 */
-	protected List<ConnectionEditPart> getAllConnectionsFrom(
-			GraphicalEditPart ep) {
-		LinkedList<ConnectionEditPart> connections = new LinkedList<ConnectionEditPart>();
-		for (Object sourceConnObj : ep.getSourceConnections()) {
-			if (sourceConnObj instanceof ConnectionEditPart) {
-				ConnectionEditPart sourceConn = (ConnectionEditPart) sourceConnObj;
-				connections.add(sourceConn);
-				connections.addAll(getAllConnectionsFrom(sourceConn));
-			}
-		}
-		return connections;
-	}
-
 }
diff --git a/plugins/org.eclipse.gendoc.bundle.acceleo.gmf/src/org/eclipse/gendoc/bundle/acceleo/gmf/impl/GMFEditPartUtils.java b/plugins/org.eclipse.gendoc.bundle.acceleo.gmf/src/org/eclipse/gendoc/bundle/acceleo/gmf/impl/GMFEditPartUtils.java
new file mode 100644
index 0000000..b96d8aa
--- /dev/null
+++ b/plugins/org.eclipse.gendoc.bundle.acceleo.gmf/src/org/eclipse/gendoc/bundle/acceleo/gmf/impl/GMFEditPartUtils.java
@@ -0,0 +1,223 @@
+package org.eclipse.gendoc.bundle.acceleo.gmf.impl;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Stack;
+
+import org.eclipse.emf.common.util.TreeIterator;
+import org.eclipse.emf.ecore.EObject;
+import org.eclipse.emf.ecore.resource.Resource;
+import org.eclipse.emf.ecore.util.EcoreUtil;
+import org.eclipse.gef.ConnectionEditPart;
+import org.eclipse.gef.EditPart;
+import org.eclipse.gef.GraphicalEditPart;
+import org.eclipse.gmf.runtime.diagram.ui.editparts.DiagramEditPart;
+import org.eclipse.gmf.runtime.diagram.ui.render.util.CopyToImageUtil;
+import org.eclipse.gmf.runtime.notation.Diagram;
+import org.eclipse.gmf.runtime.notation.View;
+
+public class GMFEditPartUtils {
+	public static List<?> getEditParts(List<EObject> visibleElements,
+			DiagramEditPart diagramEditPart) {
+		return getEditParts(visibleElements, diagramEditPart, true);
+	}
+
+	public static List<?> getEditParts(List<EObject> visibleElements,
+			DiagramEditPart diagramEditPart, boolean includeConnections) {
+		List<GraphicalEditPart> result = new LinkedList<GraphicalEditPart>();
+		for (EObject e : visibleElements) {
+			Object model = diagramEditPart.getModel();
+			if (model instanceof Diagram) {
+				Diagram diagram = (Diagram) model;
+				for (TreeIterator<EObject> i = EcoreUtil.getAllProperContents(
+						diagram, true); i.hasNext();) {
+					EObject current = i.next();
+					if (current instanceof View) {
+						View view = (View) current;
+						if (equals(e, view.getElement())) {
+							Object part = diagramEditPart.getViewer()
+									.getEditPartRegistry().get(view);
+							if (part instanceof GraphicalEditPart) {
+								result.add((GraphicalEditPart) part);
+							}
+						}
+					}
+				}
+			}
+		}
+		if (includeConnections)
+		{
+			// the process is made twice but copying the code 
+			// in this case is better than overriding to avoid maintenance problem
+			ArrayList<GraphicalEditPart> tmp = new ArrayList<GraphicalEditPart>(result);
+			for(GraphicalEditPart g : tmp)
+			{
+				result.addAll(findConnectionsToPaint(g, result));
+			}
+		}
+		return result;
+	}
+
+	protected static boolean equals(EObject e, EObject fromView) {
+		boolean result = false ;
+		if (fromView == e)
+		{
+			result = true ;
+		}
+		else
+		{
+			// if the diagram editor is the opened one the eobjects are not the same
+			if (e.eResource() != null && fromView.eResource() != null)
+			{
+				Resource eResource =  e.eResource();
+				Resource viewResoure =  fromView.eResource();
+				if (eResource.getURI() != null && eResource.getURI().equals(viewResoure.getURI()))
+				{
+					result = (eResource.getURIFragment(e) != null && eResource.getURIFragment(e).equals(viewResoure.getURIFragment(fromView)));
+				}
+			}
+		}
+		return result ;
+	}
+
+	/**
+	 * Collects all connections contained within the given edit part
+	 * Code copy from {@link CopyToImageUtil}
+	 * @param editPart
+	 *            the container editpart
+	 * @return connections within it
+	 */
+	protected static Collection<ConnectionEditPart> findConnectionsToPaint(
+			GraphicalEditPart editPart, List<GraphicalEditPart> relatedEditParts) {
+		/*
+		 * Set of node editparts contained within the given editpart
+		 */
+		HashSet<GraphicalEditPart> editParts = new HashSet<GraphicalEditPart>();
+
+		/*
+		 * All connection editparts that have a source contained within the
+		 * given editpart
+		 */
+		HashSet<ConnectionEditPart> connectionEPs = new HashSet<ConnectionEditPart>();
+
+		/*
+		 * Connections contained within the given editpart (or just the
+		 * connections to paint
+		 */
+		HashSet<ConnectionEditPart> connectionsToPaint = new HashSet<ConnectionEditPart>();
+
+		/*
+		 * Populate the set of node editparts
+		 */
+		getNestedEditParts(editPart, editParts);
+
+		/*
+		 * Populate the set of connections whose source is within the given
+		 * editpart
+		 */
+		for (GraphicalEditPart gep : editParts) {
+			connectionEPs.addAll(getAllConnectionsFrom(gep));
+		}
+
+		/*
+		 * Populate the set of connections whose source is the given editpart
+		 */
+		connectionEPs.addAll(getAllConnectionsFrom(editPart));
+
+		/*
+		 * Create a set of connections constained within the given editpart
+		 */
+		while (!connectionEPs.isEmpty()) {
+			/*
+			 * Take the first connection and check whethe there is a path
+			 * through that connection that leads to the target contained within
+			 * the given editpart
+			 */
+			Stack<ConnectionEditPart> connectionsPath = new Stack<ConnectionEditPart>();
+			ConnectionEditPart conn = connectionEPs.iterator().next();
+			connectionEPs.remove(conn);
+			connectionsPath.add(conn);
+
+			/*
+			 * Initialize the target for the current path
+			 */
+			EditPart target = conn.getTarget();
+			while (connectionEPs.contains(target)) {
+				/*
+				 * If the target end is a connection, check if it's one of the
+				 * connection's whose target is a connection and within the
+				 * given editpart. Append it to the path if it is. Otherwise
+				 * check if the target is within the actual connections or nodes
+				 * contained within the given editpart
+				 */
+				ConnectionEditPart targetConn = (ConnectionEditPart) target;
+				connectionEPs.remove(targetConn);
+				connectionsPath.add(targetConn);
+
+				/*
+				 * Update the target for the new path
+				 */
+				target = targetConn.getTarget();
+			}
+
+			/*
+			 * The path is built, check if it's target is a node or a connection
+			 * contained within the given editpart
+			 */
+			if (editParts.contains(target)
+					|| connectionsToPaint.contains(target)
+					|| relatedEditParts.contains(target)) {
+				connectionsToPaint.addAll(connectionsPath);
+			}
+		}
+		return connectionsToPaint;
+	}
+
+	/**
+	 * This method is used to obtain the list of child edit parts for shape
+	 * compartments.
+	 * 
+	 * @param childEditPart
+	 *            base edit part to get the list of children editparts
+	 * @param editParts
+	 *            list of nested shape edit parts
+	 */
+	protected static void getNestedEditParts(GraphicalEditPart baseEditPart,
+			Collection<GraphicalEditPart> editParts) {
+
+		for (Object child : baseEditPart.getChildren()) {
+			if (child instanceof GraphicalEditPart) {
+				GraphicalEditPart childEP = (GraphicalEditPart) child;
+				editParts.add(childEP);
+				getNestedEditParts(childEP, editParts);
+			}
+		}
+	}
+
+	/**
+	 * Returns all connections orginating from a given editpart. All means that
+	 * connections originating from connections that have a source given
+	 * editpart will be included
+	 * 
+	 * @param ep
+	 *            the editpart
+	 * @return all source connections
+	 */
+	protected static List<ConnectionEditPart> getAllConnectionsFrom(
+			GraphicalEditPart ep) {
+		LinkedList<ConnectionEditPart> connections = new LinkedList<ConnectionEditPart>();
+		for (Object sourceConnObj : ep.getSourceConnections()) {
+			if (sourceConnObj instanceof ConnectionEditPart) {
+				ConnectionEditPart sourceConn = (ConnectionEditPart) sourceConnObj;
+				connections.add(sourceConn);
+				connections.addAll(getAllConnectionsFrom(sourceConn));
+			}
+		}
+		return connections;
+	}
+
+
+}
diff --git a/plugins/org.eclipse.gendoc.bundle.acceleo.gmf/src/org/eclipse/gendoc/bundle/acceleo/gmf/service/GMFDiagramRenderer.java b/plugins/org.eclipse.gendoc.bundle.acceleo.gmf/src/org/eclipse/gendoc/bundle/acceleo/gmf/service/GMFDiagramRenderer.java
new file mode 100644
index 0000000..dbf4116
--- /dev/null
+++ b/plugins/org.eclipse.gendoc.bundle.acceleo.gmf/src/org/eclipse/gendoc/bundle/acceleo/gmf/service/GMFDiagramRenderer.java
@@ -0,0 +1,152 @@
+package org.eclipse.gendoc.bundle.acceleo.gmf.service;
+
+import java.util.Collections;
+import java.util.List;
+
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.runtime.Assert;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IPath;
+import org.eclipse.core.runtime.NullProgressMonitor;
+import org.eclipse.core.runtime.Status;
+import org.eclipse.emf.ecore.EObject;
+import org.eclipse.emf.ecore.resource.Resource;
+import org.eclipse.emf.workspace.util.WorkspaceSynchronizer;
+import org.eclipse.gef.EditPart;
+import org.eclipse.gendoc.bundle.acceleo.gmf.impl.GMFEditPartUtils;
+import org.eclipse.gendoc.services.GendocServices;
+import org.eclipse.gendoc.services.ILogger;
+import org.eclipse.gmf.runtime.diagram.core.preferences.PreferencesHint;
+import org.eclipse.gmf.runtime.diagram.core.util.ViewUtil;
+import org.eclipse.gmf.runtime.diagram.ui.editparts.DiagramEditPart;
+import org.eclipse.gmf.runtime.diagram.ui.image.ImageFileFormat;
+import org.eclipse.gmf.runtime.diagram.ui.parts.DiagramEditor;
+import org.eclipse.gmf.runtime.diagram.ui.render.clipboard.DiagramGenerator;
+import org.eclipse.gmf.runtime.diagram.ui.render.util.CopyToImageUtil;
+import org.eclipse.gmf.runtime.diagram.ui.util.DiagramEditorUtil;
+import org.eclipse.gmf.runtime.notation.Diagram;
+import org.eclipse.swt.widgets.Shell;
+
+public class GMFDiagramRenderer extends CopyToImageUtil implements IDiagramRenderer {
+	
+	@Override
+	public void clear() {
+	}
+
+	@Override
+	public String getServiceId() {
+		return id;
+	}
+
+	@Override
+	public void setServiceId(String serviceId) {
+		this.id = serviceId;
+	}
+
+	@Override
+	public List<EditPart> renderDiagram(Diagram diagram, List<EObject> visibleElements, IPath path, FileFormat extension, NullProgressMonitor monitor) throws CoreException {
+		if (visibleElements == null || visibleElements.isEmpty()) {
+			return copyToImage(diagram, path, getImageFileFormat(extension),
+					new NullProgressMonitor(),
+					PreferencesHint.USE_DEFAULTS);
+		} else {
+			return copyToImage(diagram, path, visibleElements,
+					getImageFileFormat(extension),
+					new NullProgressMonitor(),
+					PreferencesHint.USE_DEFAULTS);
+		}
+	}
+
+	public List<EditPart> copyToImage(Diagram diagram, IPath destination,
+			ImageFileFormat format, NullProgressMonitor monitor,
+			PreferencesHint preferencesHint)
+					throws CoreException {
+
+		List<?> partInfo = Collections.EMPTY_LIST;
+
+		DiagramEditor openedDiagramEditor = findOpenedDiagramEditor(diagram);
+		if (openedDiagramEditor != null) {
+			DiagramGenerator generator = copyToImage(openedDiagramEditor.getDiagramEditPart(),
+					destination, format, monitor);
+			partInfo = generator.getDiagramPartInfo(openedDiagramEditor.getDiagramEditPart());
+		} else {
+			Shell shell = new Shell();
+			try {
+				DiagramEditPart diagramEditPart = createDiagramEditPart(diagram,
+						shell, preferencesHint);
+				Assert.isNotNull(diagramEditPart);
+				DiagramGenerator generator = super.copyToImage(diagramEditPart,
+						destination, format, monitor);
+				partInfo = generator.getDiagramPartInfo(diagramEditPart);
+			} finally {
+				shell.dispose();
+			}
+		}
+
+		return (List)partInfo;
+	}
+
+	private DiagramEditor findOpenedDiagramEditor(Diagram diagram) {
+		DiagramEditor result = DiagramEditorUtil.findOpenedDiagramEditorForID(ViewUtil.getIdStr(diagram));
+		if (result != null){
+			IPath iPathDiagEditor =getIPath(result.getDiagram());
+			IPath iPathDiag = getIPath(diagram) ;
+
+			if (iPathDiagEditor == null || iPathDiag == null || !iPathDiag.equals(iPathDiagEditor)){
+				((ILogger) GendocServices.getDefault().getService(ILogger.class)).log("Two diagrams in separate files " + iPathDiagEditor + " and " + iPathDiag + " have the same identifier", Status.WARNING);
+				return null ;
+			}
+		}
+		return result ;
+	}
+
+	private IPath getIPath(Diagram diagram) {
+		if (diagram != null){
+			Resource resource = diagram.eResource();
+			if (resource != null){
+				IFile file = WorkspaceSynchronizer.getUnderlyingFile(resource);
+				if (file != null){
+					return file.getFullPath();
+				}
+			}
+		}
+		return null;
+	}
+
+	public List<EditPart> copyToImage(Diagram diagram, IPath destination,
+			List<EObject> visibleElements, ImageFileFormat format,
+			NullProgressMonitor monitor, PreferencesHint preferencesHint)
+					throws CoreException {
+		Shell shell = null ;
+		try {
+			List partInfo = Collections.EMPTY_LIST;
+
+			DiagramEditor openedDiagramEditor = findOpenedDiagramEditor(diagram);
+			DiagramEditPart diagramEditPart = null ;
+
+			if (openedDiagramEditor != null) {
+				diagramEditPart = openedDiagramEditor.getDiagramEditPart(); 
+			} else {
+				shell = new Shell();
+				diagramEditPart = createDiagramEditPart(
+						diagram, shell, preferencesHint);
+			}
+			Assert.isNotNull(diagramEditPart);
+			copyToImage(diagramEditPart,
+					GMFEditPartUtils.getEditParts(visibleElements, diagramEditPart),
+					destination, format, monitor);
+			return partInfo;
+		} finally {
+			if (shell != null && !shell.isDisposed())
+			{
+				shell.dispose();
+			}
+		}
+	}
+
+	private ImageFileFormat getImageFileFormat(FileFormat format) {
+		return ImageFileFormat.resolveImageFormat(format.name());
+	}
+
+	private String id;
+}
diff --git a/plugins/org.eclipse.gendoc.bundle.acceleo.gmf/src/org/eclipse/gendoc/bundle/acceleo/gmf/service/IDiagramRenderer.java b/plugins/org.eclipse.gendoc.bundle.acceleo.gmf/src/org/eclipse/gendoc/bundle/acceleo/gmf/service/IDiagramRenderer.java
new file mode 100644
index 0000000..ae2485c
--- /dev/null
+++ b/plugins/org.eclipse.gendoc.bundle.acceleo.gmf/src/org/eclipse/gendoc/bundle/acceleo/gmf/service/IDiagramRenderer.java
@@ -0,0 +1,64 @@
+package org.eclipse.gendoc.bundle.acceleo.gmf.service;
+
+import java.util.List;
+
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IPath;
+import org.eclipse.core.runtime.NullProgressMonitor;
+import org.eclipse.emf.common.util.BasicDiagnostic;
+import org.eclipse.emf.common.util.Diagnostic;
+import org.eclipse.emf.ecore.EObject;
+import org.eclipse.gef.EditPart;
+import org.eclipse.gendoc.documents.IImageManipulationService;
+import org.eclipse.gendoc.documents.IImageManipulationServiceFactory;
+import org.eclipse.gendoc.services.GendocServices;
+import org.eclipse.gendoc.services.IGendocDiagnostician;
+import org.eclipse.gendoc.services.IService;
+import org.eclipse.gendoc.tags.handlers.Activator;
+import org.eclipse.gmf.runtime.notation.Diagram;
+
+public interface IDiagramRenderer extends IService {
+	public enum FileFormat {
+		PNG, JPEG, GIF, BMP, JPG, SVG, EMF;
+		
+		public FileFormat getExtension(){
+			IImageManipulationServiceFactory imageManipulationServiceFactory = GendocServices.getDefault().getService(IImageManipulationServiceFactory.class);
+        	IImageManipulationService imageManipulationService = imageManipulationServiceFactory.getService(name().toLowerCase()) ;
+			return transformToFormat(imageManipulationService.renameExtension(name()));
+		}
+		
+		public String getFullExtension() {
+        	return "." + getExtension().name().toLowerCase();
+		}
+
+		public static FileFormat transformToFormat(String ext) {
+			FileFormat format;
+			try {
+				format = FileFormat.valueOf(ext.toUpperCase());
+				return format;
+			} catch (IllegalArgumentException e) {
+				IGendocDiagnostician diagnostician = GendocServices.getDefault()
+						.getService(IGendocDiagnostician.class);
+				String message = "The format " + ext + " is not supported";
+				diagnostician.addDiagnostic(new BasicDiagnostic(Diagnostic.ERROR,
+						Activator.PLUGIN_ID, 0, message, null));
+				return FileFormat.valueOf(ext);
+			}
+
+		}
+	}
+
+	/**
+	 * Render the diagram into a image.
+	 * 
+	 * @param diagram the diagram to render
+	 * @param visibleElements a list with the visible elements in the diagram
+	 * @param destination the path where the image will be stored
+	 * @param imageFormat the format of the image
+	 * @param monitor the progress monitor
+	 * @return a list with the top level visible 
+	 * @throws CoreException
+	 * 
+	 */
+	public List<EditPart> renderDiagram(Diagram diagram, List<EObject> visibleElements, IPath destination, FileFormat imageFormat, NullProgressMonitor monitor) throws CoreException;
+}
diff --git a/plugins/org.eclipse.gendoc.bundle.acceleo.sirius/META-INF/MANIFEST.MF b/plugins/org.eclipse.gendoc.bundle.acceleo.sirius/META-INF/MANIFEST.MF
index 16f7b91..e5c00b7 100644
--- a/plugins/org.eclipse.gendoc.bundle.acceleo.sirius/META-INF/MANIFEST.MF
+++ b/plugins/org.eclipse.gendoc.bundle.acceleo.sirius/META-INF/MANIFEST.MF
@@ -19,11 +19,15 @@
  org.eclipse.gendoc.services,
  org.eclipse.gendoc.process,
  org.eclipse.gendoc.tags.handlers,
- org.eclipse.sirius.diagram;bundle-version="2.0.4",
- org.eclipse.gendoc.bundle.acceleo.commons;bundle-version="0.6.0",
- org.eclipse.sirius.table;bundle-version="4.1.3",
+ org.eclipse.sirius.diagram,
+ org.eclipse.gendoc.bundle.acceleo.commons,
+ org.eclipse.sirius.table,
  org.eclipse.gendoc.documents,
- org.eclipse.gendoc.table
+ org.eclipse.gendoc.table,
+ org.eclipse.gef,
+ org.eclipse.gmf.runtime.diagram.ui.render,
+ org.eclipse.gmf.runtime.diagram.ui,
+ org.eclipse.sirius.diagram.ui
 Bundle-RequiredExecutionEnvironment: J2SE-1.5
 Bundle-ActivationPolicy: lazy
 Eclipse-LazyStart: true
diff --git a/plugins/org.eclipse.gendoc.bundle.acceleo.sirius/plugin.xml b/plugins/org.eclipse.gendoc.bundle.acceleo.sirius/plugin.xml
index 5d288ad..7ed18b5 100644
--- a/plugins/org.eclipse.gendoc.bundle.acceleo.sirius/plugin.xml
+++ b/plugins/org.eclipse.gendoc.bundle.acceleo.sirius/plugin.xml
@@ -9,5 +9,18 @@
             name="sirius">
       </service>
    </extension>
+   <extension
+         point="org.eclipse.gendoc.processes">
+      <process
+            id="org.eclipse.gendoc.bundle.acceleo.papyrus.factoryassignment"
+            label="Override Diagram renderer"
+            parallel="false"
+            priority="NORMAL"
+            processor="org.eclipse.gendoc.bundle.acceleo.sirius.service.SiriusDiagramRendererOverriding">
+         <successor
+               ref="countSteps">
+         </successor>
+      </process>
+   </extension>
 
 </plugin>
diff --git a/plugins/org.eclipse.gendoc.bundle.acceleo.sirius/src/org/eclipse/gendoc/bundle/acceleo/sirius/service/SiriusDiagramRenderer.java b/plugins/org.eclipse.gendoc.bundle.acceleo.sirius/src/org/eclipse/gendoc/bundle/acceleo/sirius/service/SiriusDiagramRenderer.java
new file mode 100644
index 0000000..40dd03b
--- /dev/null
+++ b/plugins/org.eclipse.gendoc.bundle.acceleo.sirius/src/org/eclipse/gendoc/bundle/acceleo/sirius/service/SiriusDiagramRenderer.java
@@ -0,0 +1,79 @@
+package org.eclipse.gendoc.bundle.acceleo.sirius.service;
+
+import org.eclipse.core.runtime.Assert;
+import org.eclipse.emf.transaction.util.TransactionUtil;
+import org.eclipse.gef.RootEditPart;
+import org.eclipse.gendoc.bundle.acceleo.gmf.service.GMFDiagramRenderer;
+import org.eclipse.gmf.runtime.diagram.core.listener.DiagramEventBroker;
+import org.eclipse.gmf.runtime.diagram.core.preferences.PreferencesHint;
+import org.eclipse.gmf.runtime.diagram.ui.editparts.DiagramEditPart;
+import org.eclipse.gmf.runtime.diagram.ui.editparts.IDiagramPreferenceSupport;
+import org.eclipse.gmf.runtime.diagram.ui.parts.DiagramCommandStack;
+import org.eclipse.gmf.runtime.diagram.ui.parts.DiagramEditDomain;
+import org.eclipse.gmf.runtime.diagram.ui.parts.DiagramGraphicalViewer;
+import org.eclipse.gmf.runtime.diagram.ui.services.editpart.EditPartService;
+import org.eclipse.gmf.runtime.notation.Diagram;
+import org.eclipse.jface.preference.IPreferenceStore;
+import org.eclipse.sirius.diagram.ui.tools.internal.part.SiriusDiagramGraphicalViewer;
+import org.eclipse.swt.widgets.Shell;
+
+public class SiriusDiagramRenderer extends GMFDiagramRenderer {
+	public SiriusDiagramRenderer() {
+		super();
+	}
+
+	@Override
+	public DiagramEditPart createDiagramEditPart(Diagram diagram, Shell shell, PreferencesHint preferencesHint) {
+		if (isSiriusDiagram(diagram)) {
+			DiagramGraphicalViewer customViewer = new SiriusDiagramGraphicalViewer();
+	        customViewer.createControl(shell);
+	
+	        DiagramEditDomain editDomain = new DiagramEditDomain(null);
+	        editDomain.setCommandStack(
+	            new DiagramCommandStack(editDomain));
+	
+	        customViewer.setEditDomain(editDomain);
+	
+	        // hook in preferences
+	        RootEditPart rootEP = EditPartService.getInstance().createRootEditPart(
+	            diagram);
+	        if (rootEP instanceof IDiagramPreferenceSupport) {
+	            if (preferencesHint == null) {
+	                preferencesHint = ((IDiagramPreferenceSupport) rootEP)
+	                    .getPreferencesHint();
+	            } else {
+	                ((IDiagramPreferenceSupport) rootEP)
+	                    .setPreferencesHint(preferencesHint);
+	            }
+	            customViewer
+	                .hookWorkspacePreferenceStore((IPreferenceStore) preferencesHint
+	                    .getPreferenceStore());
+	        }
+	        
+	        customViewer.setRootEditPart(rootEP);
+	
+	        customViewer.setEditPartFactory(EditPartService.getInstance());
+	
+	        DiagramEventBroker.startListening(TransactionUtil.getEditingDomain(diagram));
+	        
+	        customViewer.setContents(diagram);
+	        customViewer.flush();
+	        
+	        Assert.isTrue(customViewer.getContents() instanceof DiagramEditPart);
+	        
+	    	/*
+	    	 * We need to flush all the deferred updates. 
+	    	 */
+	   		while (shell.getDisplay().readAndDispatch()) {
+	   			// nothing
+	   		}
+	        
+	        return (DiagramEditPart) customViewer.getContents();
+		}
+		return super.createDiagramEditPart(diagram, shell, preferencesHint);
+	}
+	
+	private boolean isSiriusDiagram(Diagram diagram) {
+		return SiriusServices.isSiriusDiagram(diagram);
+	}
+}
diff --git a/plugins/org.eclipse.gendoc.bundle.acceleo.sirius/src/org/eclipse/gendoc/bundle/acceleo/sirius/service/SiriusDiagramRendererOverriding.java b/plugins/org.eclipse.gendoc.bundle.acceleo.sirius/src/org/eclipse/gendoc/bundle/acceleo/sirius/service/SiriusDiagramRendererOverriding.java
new file mode 100644
index 0000000..83ca168
--- /dev/null
+++ b/plugins/org.eclipse.gendoc.bundle.acceleo.sirius/src/org/eclipse/gendoc/bundle/acceleo/sirius/service/SiriusDiagramRendererOverriding.java
@@ -0,0 +1,22 @@
+package org.eclipse.gendoc.bundle.acceleo.sirius.service;
+
+import org.eclipse.gendoc.bundle.acceleo.gmf.service.IDiagramRenderer;
+import org.eclipse.gendoc.process.AbstractProcess;
+import org.eclipse.gendoc.services.GendocServices;
+import org.eclipse.gendoc.services.exception.GenDocException;
+
+public class SiriusDiagramRendererOverriding extends AbstractProcess {
+
+	@Override
+	protected void doRun() throws GenDocException {
+        SiriusDiagramRenderer diagramRenderer = new SiriusDiagramRenderer();
+        diagramRenderer.setServiceId("DiagramRenderer");//$NON-NLS-1$
+        GendocServices.getDefault().setService(IDiagramRenderer.class, diagramRenderer);
+	}
+
+	@Override
+	protected int getTotalWork() {
+		return 1;
+	}
+
+}
diff --git a/plugins/org.eclipse.gendoc.bundle.acceleo.sirius/src/org/eclipse/gendoc/bundle/acceleo/sirius/service/SiriusServices.java b/plugins/org.eclipse.gendoc.bundle.acceleo.sirius/src/org/eclipse/gendoc/bundle/acceleo/sirius/service/SiriusServices.java
index 00eeac5..238d946 100644
--- a/plugins/org.eclipse.gendoc.bundle.acceleo.sirius/src/org/eclipse/gendoc/bundle/acceleo/sirius/service/SiriusServices.java
+++ b/plugins/org.eclipse.gendoc.bundle.acceleo.sirius/src/org/eclipse/gendoc/bundle/acceleo/sirius/service/SiriusServices.java
@@ -114,4 +114,12 @@
 		}
 		return results;
 	}
+	
+	static boolean isSiriusDiagram(Diagram diagram) {
+		if (!(diagram.eContainer() instanceof AnnotationEntry))
+			return false;
+		
+		AnnotationEntry e = (AnnotationEntry)diagram.eContainer();
+		return CustomDataConstants.GMF_DIAGRAMS.equals(e.getSource()) && e.getData() == diagram;
+	}
 }