Print all GMF diagrams with a single click

We know that GMF gives only three types of Print Options for the Print Range.

a. Current Diagram

b. All diagrams from open models

c. Selected diagrams.

Suppose consider your projects contain a large number of diagrams and you wish to Print all the diagrams. What are the options left? You need to open up each of the diagram and click print. This is a hard task when you have a large number of diagrams. I have found a better way by automating it.

Please note that there will be a lot of methods to accomplish this. This is one method I am going to detail. Since GMF doesn’t allow us to do this directly I am customizing four GMF internal classes. The classes we need to customize are

DiagramWithPrintGlobalActionHandlerProvider class

DiagramWithPrintGlobalActionHandler class

EnhancedPrintActionHelper class

DiagramPrinterUtil class

DiagramWithPrintGlobalActionHandlerProvider is the class that provides GlobalActionHandlerProvider for print functionality. This will be defined in our plugin’s plugin.xml

Before we start we need to make these four classes as custom ones. I am doing this because we cannot edit the source code of dependent plugins. If we have attached source for these classes in out target platform definition we can view the code while debugging but We cannot the edit the code for our own customization.

As the first step create four new classes in a package of your choice with names

Copy the original code itself in to these classes from its respective original counterparts. Once done that open up your plugin’s plugin.xml and change the GlobalActionHandlerProvider definition to as shown below.

<extension point=””>




<Priority name=”High”/>

<ViewId id=”mypackage.CustomDiagramEditorID”>

<ElementType class=”org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart”>

<GlobalActionId actionId=”print”/>





Once you are done with this, start your application using the Run configuration. Yep, We haven’t changed anything in our custom classes, but We want to make sure it works in the default way. If this works fine we can make sure that the print mechanism is actually referencing our custom classes instead of the GMF internal classes. Still having doubts? put some break points in the code and see whether our class is called while running in eclipse DEBUG mode.

Make sure this works fine. ie. The print options given above should work the same way as it existed because we have copied the original code itself to our custom classes.

So whats our ultimate AIM? We want to automate the printing of all diagrams with a single click. For that we need to make a lot of changes in code for these 4 classes that we have just made.

Step 1: Open up and make changes shown below.


if (!getHandlerList().containsKey(context.getActivePart())) {


new CustomDiagramWithPrintGlobalActionHandler());


return (CustomDiagramWithPrintGlobalActionHandler) getHandlerList().get(context.getActivePart());


Step 2:Open up CustomDiagramWithPrintGlobalActionHandler and make changes as shown below.

protected void doPrint(IGlobalActionContext cntxt) {

IPrintActionHelper helper = new CustomEnhancedPrintActionHelper();




Step 3:open up

This is the class we are going to do a lot of coding to attain our goal.

What we are going to do is

#1.Get all the diagram files from the Workspace using our diagram file’s extension as an Array list.

#2.Ina loop we Will Open a diagram,Print the Diagram and Close the diagram. This loop will iterate for n times where, n is the size of the array list described in #1.

#1.I find all the diagram files present in my workspace using my diagram’s extension.

private static Collection getWorkspaceFiles(String extension)


ArrayList files = new ArrayList();

IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();

IProject[] projects = root.getProjects();

for (int i=0;i<projects.length;i++)


try {

IResource[] resources = projects[i].members();

for (int j=0;j<resources.length;j++)


getChildren(resources[j], files, extension);



catch (CoreException ex)



return files;


private static void getChildren(IResource res, ArrayList files, String extension)


if (res instanceof IFile)


if (extension == null ||( res.getFileExtension()!=null && res.getFileExtension().endsWith(extension) ))



if (res instanceof IFolder)


try {

IResource[] resources = ((IFolder)res).members();

for (int j=0;j<resources.length;j++)


getChildren(resources[j], files, extension);



catch (CoreException ex)




#2.Ina loop we Will Open a diagram,Print the Diagram and Close the diagram.

For anyone there will be a doubt where to put this loop and call the function calls. In my case I put this inside the code block that gets called when we select “All diagrams from open models” in the print dialog.


else if (helper.getDlgDiagramPrintRangeAll()) {


ArrayList files = (ArrayList)getWorkspaceFiles(“mydomain_diagram”);

System.out.println(“Number of diagrams to be printed is”+files.size());

for(int i=0;i<files.size();i++){

System.out.println(“Diagram opened is”+files.get(i));






And the Functions for Opening and Printing and Closing are as follows:

//Method for Opening the Diagram

private static void openDiagram(String DiagramName){

URI diagramModelFilename = URI.createURI(DiagramName);

GMFResource myGMFDiagram = new GMFResource(diagramModelFilename);

try {



} catch (IOException e1) {


} catch (PartInitException e) {

System.out.println(“Error opening diagram editor”+e.getStatus());




//Method for Printing the Diagram


private static void PrintDiagram(DiagramPrinter diagramPrinter,IPrintHelper helper){

DiagramEditor dEditor = null;

List diagramEditors = EditorService.getInstance().getRegisteredEditorParts();

Iterator it = diagramEditors.iterator();

while (it.hasNext()) {

Object obje =;

if (obje instanceof DiagramEditor) { //DiagramDocumentEditor

dEditor = (DiagramEditor) obje;




printDiagrams(diagramPrinter, helper);

System.out.println(“File Printed”);



//Method for Closing the Diagram


private static void closeDiagram(){

IWorkbench workbench = PlatformUI.getWorkbench();

if (workbench != null) {

if (workbench.getWorkbenchWindows() != null

&&workbench.getWorkbenchWindows()[0].getActivePage() != null) {

IWorkbenchPage page = workbench.getWorkbenchWindows()[0].getActivePage();

if (page != null&& page.getEditorReferences().length > 0) {

ArrayList<Resource> resource = new ArrayList<Resource>();

for (IEditorReference editorReference : page.getEditorReferences()) {

TransactionalEditingDomain editingDomain = ((SgmoduleDiagramEditor) editorReference



if (editingDomain != null) {

ResourceSet resourceSet = editingDomain


for (Resource element : resourceSet

.getResources()) {



for (Resource element : resource) {





((SgmoduleDiagramEditor) editorReference






System.out.println(“Diagram closed”);


This works perfectly for me. But When you have lots of diagrams to be printed in a single stretch, make sure You allot more memory for your application else it may throw a java heap memory exception.

Hope this helps some one. Queries/Suggestions Welcome

Anoop Menon

Confianz Global Inc. develop custom software application according to your business requirement. We help organizations with their web & mobile application development needs.

Talk to our experts now

    Talk To Our Experts Now