{"id":69,"date":"2008-07-27T13:22:29","date_gmt":"2008-07-27T13:22:29","guid":{"rendered":"http:\/\/cyberpen.wordpress.com\/?p=69"},"modified":"2022-10-31T17:53:35","modified_gmt":"2022-10-31T17:53:35","slug":"print-all-gmf-diagrams-with-a-single-click","status":"publish","type":"post","link":"https:\/\/www.confianzit.com\/cit-blog\/print-all-gmf-diagrams-with-a-single-click\/","title":{"rendered":"Print all GMF diagrams with a single click"},"content":{"rendered":"<p>[et_pb_section fb_built=&#8221;1&#8243; _builder_version=&#8221;3.22&#8243;][et_pb_row _builder_version=&#8221;4.9.4&#8243; background_size=&#8221;initial&#8221; background_position=&#8221;top_left&#8221; background_repeat=&#8221;repeat&#8221; hover_enabled=&#8221;0&#8243; column_structure=&#8221;3_5,2_5&#8243; sticky_enabled=&#8221;0&#8243;][et_pb_column type=&#8221;3_5&#8243; _builder_version=&#8221;3.25&#8243; custom_padding=&#8221;|||&#8221; custom_padding__hover=&#8221;|||&#8221;][et_pb_text _builder_version=&#8221;4.9.4&#8243; background_size=&#8221;initial&#8221; background_position=&#8221;top_left&#8221; background_repeat=&#8221;repeat&#8221; hover_enabled=&#8221;0&#8243; module_class=&#8221;blog-left-content&#8221; sticky_enabled=&#8221;0&#8243;]<\/p>\n<p><!-- \t \t --><\/p>\n<p><span style=\"text-decoration: underline;\"><strong>Print all GMF diagrams with a single click<\/strong><\/span><\/p>\n<p>We know that GMF gives only three types of Print Options for the Print Range.<\/p>\n<p>a. Current Diagram<\/p>\n<p>b. All diagrams from open models<\/p>\n<p>c. Selected diagrams.<\/p>\n<p>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.<\/p>\n<p>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&#8217;t allow us to do this directly I am customizing four GMF internal classes. The classes we need to customize are<\/p>\n<p><strong>DiagramWithPrintGlobalActionHandlerProvider<\/strong> class<\/p>\n<p><strong>DiagramWithPrintGlobalActionHandler<\/strong> class<\/p>\n<p><strong>EnhancedPrintActionHelper<\/strong> class<\/p>\n<p><strong>DiagramPrinterUtil<\/strong> class<\/p>\n<p><strong>DiagramWithPrintGlobalActionHandlerProvider<\/strong> is the class that provides GlobalActionHandlerProvider for print functionality. This will be defined in our plugin&#8217;s plugin.xml<\/p>\n<p>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.<\/p>\n<p>As the first step create four new classes in a package of your choice with names<\/p>\n<p><strong>CustomDiagramWithPrintGlobalActionHandlerProvider.java <\/strong><\/p>\n<p><strong>CustomDiagramWithPrintGlobalActionHandler.java <\/strong><\/p>\n<p><strong>CustomEnhancedPrintActionHelper.java <\/strong><\/p>\n<p><strong>CustomDiagramPrinterUtil.java<\/strong><\/p>\n<p>Copy the original code itself in to these classes from its respective original counterparts. Once done that open up your plugin&#8217;s plugin.xml and change the <strong>GlobalActionHandlerProvider<\/strong> definition to as shown below.<\/p>\n<p>&lt;extension point=&#8221;org.eclipse.gmf.runtime.common.ui.services.action.globalActionHandlerProviders&#8221;&gt;<\/p>\n<p align=\"left\">&lt;GlobalActionHandlerProvider<\/p>\n<p align=\"left\">class=&#8221;<strong>mypackage.printing.CustomDiagramWithPrintGlobalActionHandlerProvider<\/strong>&#8220;<\/p>\n<p align=\"left\">id=&#8221;mydomainPresentationPrint&#8221;&gt;<\/p>\n<p align=\"left\">&lt;Priority name=&#8221;High&#8221;\/&gt;<\/p>\n<p align=\"left\">&lt;ViewId id=&#8221;mypackage.CustomDiagramEditorID&#8221;&gt;<\/p>\n<p align=\"left\">&lt;ElementType class=&#8221;org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart&#8221;&gt;<\/p>\n<p align=\"left\">&lt;GlobalActionId actionId=&#8221;print&#8221;\/&gt;<\/p>\n<p align=\"left\">&lt;\/ElementType&gt;<\/p>\n<p align=\"left\">&lt;\/ViewId&gt;<\/p>\n<p align=\"left\">&lt;\/GlobalActionHandlerProvider&gt;<\/p>\n<p align=\"left\">&lt;\/extension&gt;<\/p>\n<p>Once you are done with this, start your application using the Run configuration. Yep, We haven&#8217;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.<\/p>\n<p>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.<\/p>\n<p>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.<\/p>\n<p>Step 1: Open up <strong>CustomDiagramWithPrintGlobalActionHandlerProvider.java<\/strong> and make changes shown below.<\/p>\n<p>&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;<\/p>\n<p><strong>if<\/strong> (!getHandlerList().containsKey(context.getActivePart())) {<\/p>\n<p align=\"left\">getHandlerList().put(context.getActivePart(),<\/p>\n<p align=\"left\"><strong>new<\/strong> <strong>CustomDiagramWithPrintGlobalActionHandler()<\/strong>);<\/p>\n<p>&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;<\/p>\n<p align=\"left\"><strong>return<\/strong> (<strong>CustomDiagramWithPrintGlobalActionHandler<\/strong>) getHandlerList().get(context.getActivePart());<\/p>\n<p align=\"left\">&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;<\/p>\n<p align=\"left\">Step 2:Open up <strong>CustomDiagramWithPrintGlobalActionHandler <\/strong>and make changes as shown below.<\/p>\n<p align=\"left\"><strong>protected<\/strong> <strong>void<\/strong> doPrint(IGlobalActionContext cntxt) {<\/p>\n<p align=\"left\">IPrintActionHelper helper = <strong>new <\/strong><strong>CustomEnhancedPrintActionHelper()<\/strong>;<\/p>\n<p align=\"left\">helper.doPrint(cntxt.getActivePart());<\/p>\n<p align=\"left\">}<\/p>\n<p align=\"left\">&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;<\/p>\n<p align=\"left\">Step 3:open up <strong>CustomDiagramPrinterUtil.java<\/strong><\/p>\n<p align=\"left\">This is the class we are going to do a lot of coding to attain our goal.<\/p>\n<p>What we are going to do is<\/p>\n<p>#1.Get all the diagram files from the Workspace using our diagram file&#8217;s extension as an Array list.<\/p>\n<p>#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.<\/p>\n<p><span style=\"text-decoration: underline;\"><strong>#1.I find all the diagram files present in my workspace using my diagram&#8217;s extension.<\/strong><\/span><\/p>\n<p><strong>private<\/strong> <strong>static<\/strong> Collection getWorkspaceFiles(String extension)<\/p>\n<p align=\"left\">{<\/p>\n<p align=\"left\">ArrayList files = <strong>new<\/strong> ArrayList();<\/p>\n<p align=\"left\">IWorkspaceRoot root = ResourcesPlugin.<em>getWorkspace<\/em>().getRoot();<\/p>\n<p align=\"left\">IProject[] projects = root.getProjects();<\/p>\n<p align=\"left\"><strong>for<\/strong> (<strong>int<\/strong> i=0;i&lt;projects.length;i++)<\/p>\n<p align=\"left\">{<\/p>\n<p align=\"left\"><strong>try<\/strong> {<\/p>\n<p align=\"left\">IResource[] resources = projects[i].members();<\/p>\n<p align=\"left\"><strong>for<\/strong> (<strong>int<\/strong> j=0;j&lt;resources.length;j++)<\/p>\n<p align=\"left\">{<\/p>\n<p align=\"left\"><em>getChildren<\/em>(resources[j], files, extension);<\/p>\n<p align=\"left\">}<\/p>\n<p align=\"left\">}<\/p>\n<p align=\"left\"><strong>catch<\/strong> (CoreException ex)<\/p>\n<p align=\"left\">{\/*ignore*\/}<\/p>\n<p align=\"left\">}<\/p>\n<p align=\"left\"><strong>return<\/strong> files;<\/p>\n<p align=\"left\">}<\/p>\n<p align=\"left\"><strong>private<\/strong> <strong>static<\/strong> <strong>void<\/strong> getChildren(IResource res, ArrayList files, String extension)<\/p>\n<p align=\"left\">{<\/p>\n<p align=\"left\"><strong>if<\/strong> (res <strong>instanceof<\/strong> IFile)<\/p>\n<p align=\"left\">{<\/p>\n<p align=\"left\"><strong>if<\/strong> (extension == <strong>null<\/strong> ||( res.getFileExtension()!=<strong>null<\/strong> &amp;&amp; res.getFileExtension().endsWith(extension) ))<\/p>\n<p align=\"left\">files.add(res.getFullPath().toString());<\/p>\n<p align=\"left\">}<\/p>\n<p align=\"left\"><strong>if<\/strong> (res <strong>instanceof<\/strong> IFolder)<\/p>\n<p align=\"left\">{<\/p>\n<p align=\"left\"><strong>try<\/strong> {<\/p>\n<p align=\"left\">IResource[] resources = ((IFolder)res).members();<\/p>\n<p align=\"left\"><strong>for<\/strong> (<strong>int<\/strong> j=0;j&lt;resources.length;j++)<\/p>\n<p align=\"left\">{<\/p>\n<p align=\"left\"><em>getChildren<\/em>(resources[j], files, extension);<\/p>\n<p align=\"left\">}<\/p>\n<p align=\"left\">}<\/p>\n<p align=\"left\"><strong>catch<\/strong> (CoreException ex)<\/p>\n<p align=\"left\">{}<\/p>\n<p align=\"left\">}<\/p>\n<p align=\"left\">}<\/p>\n<p><span style=\"text-decoration: underline;\"><strong>#2.Ina loop we Will Open a diagram,Print the Diagram and Close the diagram.<\/strong><\/span><\/p>\n<p>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 &#8220;All diagrams from open models&#8221; in the print dialog.<\/p>\n<p>&#8230;&#8230;&#8230;&#8230;&#8230;.<\/p>\n<p><strong>else<\/strong> <strong>if<\/strong> (helper.getDlgDiagramPrintRangeAll()) {<\/p>\n<p align=\"left\">flag=1;<\/p>\n<p align=\"left\">ArrayList files = (ArrayList)<em>getWorkspaceFiles<\/em>(&#8220;mydomain_diagram&#8221;);<\/p>\n<p align=\"left\">System.<em>out<\/em>.println(&#8220;Number of diagrams to be printed is&#8221;+files.size());<\/p>\n<p align=\"left\"><strong>for<\/strong>(<strong>int<\/strong> i=0;i&lt;files.size();i++){<\/p>\n<p align=\"left\">System.<em>out<\/em>.println(&#8220;Diagram opened is&#8221;+files.get(i));<\/p>\n<p align=\"left\"><em>openDiagram<\/em>(files.get(i).toString());<\/p>\n<p align=\"left\"><em>PrintDiagram<\/em>(diagramPrinter,helper);<\/p>\n<p align=\"left\"><em>closeDiagram<\/em>();<\/p>\n<p align=\"left\">}<\/p>\n<p>&#8230;&#8230;&#8230;&#8230;<\/p>\n<p>And the Functions for Opening and Printing and Closing are as follows:<\/p>\n<p>\/\/Method for Opening the Diagram<\/p>\n<p><strong>private<\/strong> <strong>static<\/strong> <strong>void<\/strong> openDiagram(String DiagramName){<\/p>\n<p align=\"left\">URI diagramModelFilename = URI.<em>createURI<\/em>(DiagramName);<\/p>\n<p align=\"left\">GMFResource myGMFDiagram = <strong>new<\/strong> GMFResource(diagramModelFilename);<\/p>\n<p align=\"left\"><strong>try<\/strong> {<\/p>\n<p align=\"left\">myGMFDiagram.load(Collections.<em>EMPTY_MAP<\/em>);<\/p>\n<p align=\"left\"><strong>myDomainDiagramEditorUtil<\/strong>.<em>openDiagram<\/em>(myGMFDiagram);<\/p>\n<p align=\"left\">} <strong>catch<\/strong> (IOException e1) {<\/p>\n<p align=\"left\">e1.printStackTrace();<\/p>\n<p align=\"left\">} <strong>catch<\/strong> (PartInitException e) {<\/p>\n<p align=\"left\">System.<em>out<\/em>.println(&#8220;Error opening diagram editor&#8221;+e.getStatus());<\/p>\n<p align=\"left\">}<\/p>\n<p align=\"left\">}<\/p>\n<p>&#8230;&#8230;..<\/p>\n<p>\/\/Method for Printing the Diagram<\/p>\n<p>&#8230;&#8230;..<\/p>\n<p><strong>private<\/strong> <strong>static<\/strong> <strong>void<\/strong> PrintDiagram(DiagramPrinter diagramPrinter,IPrintHelper helper){<\/p>\n<p align=\"left\">DiagramEditor dEditor = <strong>null<\/strong>;<\/p>\n<p align=\"left\">List diagramEditors = EditorService.<em>getInstance<\/em>().getRegisteredEditorParts();<\/p>\n<p align=\"left\">Iterator it = diagramEditors.iterator();<\/p>\n<p align=\"left\"><strong>while<\/strong> (it.hasNext()) {<\/p>\n<p align=\"left\">Object obje = it.next();<\/p>\n<p align=\"left\"><strong>if<\/strong> (obje <strong>instanceof<\/strong> DiagramEditor) { \/\/DiagramDocumentEditor<\/p>\n<p align=\"left\">dEditor = (DiagramEditor) obje;<\/p>\n<p align=\"left\">}<\/p>\n<p align=\"left\">}<\/p>\n<p align=\"left\">diagramPrinter.setDiagrams(Collections.<em>singletonList<\/em>(dEditor.getDiagram()));<\/p>\n<p align=\"left\"><em>printDiagrams<\/em>(diagramPrinter, helper);<\/p>\n<p align=\"left\">System.<em>out<\/em>.println(&#8220;File Printed&#8221;);<\/p>\n<p align=\"left\">}<\/p>\n<p align=\"left\">&#8230;&#8230;..<\/p>\n<p align=\"left\">\/\/Method for Closing the Diagram<\/p>\n<p align=\"left\">&#8230;&#8230;..<\/p>\n<p align=\"left\"><strong>private<\/strong> <strong>static<\/strong> <strong>void<\/strong> closeDiagram(){<\/p>\n<p align=\"left\">IWorkbench workbench = PlatformUI.<em>getWorkbench<\/em>();<\/p>\n<p align=\"left\"><strong>if<\/strong> (workbench != <strong>null<\/strong>) {<\/p>\n<p align=\"left\"><strong>if<\/strong> (workbench.getWorkbenchWindows() != <strong>null<\/strong><\/p>\n<p align=\"left\">&amp;&amp;workbench.getWorkbenchWindows()[0].getActivePage() != <strong>null<\/strong>) {<\/p>\n<p align=\"left\">IWorkbenchPage page = workbench.getWorkbenchWindows()[0].getActivePage();<\/p>\n<p align=\"left\"><strong>if<\/strong> (page != <strong>null<\/strong>&amp;&amp; page.getEditorReferences().length &gt; 0) {<\/p>\n<p align=\"left\">ArrayList&lt;Resource&gt; resource = <strong>new<\/strong> ArrayList&lt;Resource&gt;();<\/p>\n<p align=\"left\"><strong>for<\/strong> (IEditorReference editorReference : page.getEditorReferences()) {<\/p>\n<p align=\"left\">TransactionalEditingDomain editingDomain = ((SgmoduleDiagramEditor) editorReference<\/p>\n<p align=\"left\">.getPart(<strong>false<\/strong>))<\/p>\n<p align=\"left\">.getEditingDomain();<\/p>\n<p align=\"left\"><strong>if<\/strong> (editingDomain != <strong>null<\/strong>) {<\/p>\n<p align=\"left\">ResourceSet resourceSet = editingDomain<\/p>\n<p align=\"left\">.getResourceSet();<\/p>\n<p align=\"left\"><strong>for<\/strong> (Resource element : resourceSet<\/p>\n<p align=\"left\">.getResources()) {<\/p>\n<p align=\"left\">resource.add(element);<\/p>\n<p align=\"left\">}<\/p>\n<p align=\"left\"><strong>for<\/strong> (Resource element : resource) {<\/p>\n<p align=\"left\">resourceSet.getResources()<\/p>\n<p align=\"left\">.remove(element);<\/p>\n<p align=\"left\">}<\/p>\n<p align=\"left\">}<\/p>\n<p align=\"left\">((SgmoduleDiagramEditor) editorReference<\/p>\n<p align=\"left\">.getPart(<strong>false<\/strong>)).close(<strong>false<\/strong>);<\/p>\n<p align=\"left\">}<\/p>\n<p align=\"left\">}<\/p>\n<p align=\"left\">}<\/p>\n<p align=\"left\">}<\/p>\n<p align=\"left\">System.<em>out<\/em>.println(&#8220;Diagram closed&#8221;);<\/p>\n<p align=\"left\">}<\/p>\n<p align=\"left\">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.<\/p>\n<p align=\"left\">Hope this helps some one. Queries\/Suggestions Welcome<\/p>\n<p align=\"left\">Anoop Menon<\/p>\n<p align=\"left\"><em>Confianz Global Inc.\u00a0<a href=\"https:\/\/www.confianzit.com\/about-us\" class=\"rank-math-link\">develop custom software application<\/a>\u00a0according to your business requirement. We help organizations with their web &amp;\u00a0<a href=\"https:\/\/www.confianzit.com\/mobile-app-development\" class=\"rank-math-link\">mobile application development<\/a>\u00a0needs.<\/em><\/p>\n<p>[\/et_pb_text][\/et_pb_column][et_pb_column type=&#8221;2_5&#8243; _builder_version=&#8221;3.25&#8243; custom_padding=&#8221;|||&#8221; custom_padding__hover=&#8221;|||&#8221;][et_pb_code _builder_version=&#8221;4.9.4&#8243; _module_preset=&#8221;default&#8221; locked=&#8221;off&#8221; global_module=&#8221;2151&#8243;]<\/p>\n<div class=\"blog-floating-form\"><!-- [et_pb_line_break_holder] -->  <\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_62 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title \" >Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.confianzit.com\/cit-blog\/print-all-gmf-diagrams-with-a-single-click\/#Talk_to_our_experts_now\" title=\"    Talk to our experts now  \">    Talk to our experts now  <\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.confianzit.com\/cit-blog\/print-all-gmf-diagrams-with-a-single-click\/#Talk_To_Our_Experts_Now\" title=\"Talk To Our Experts Now\n\t\">Talk To Our Experts Now\n\t<\/a><\/li><\/ul><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h1><span class=\"ez-toc-section\" id=\"Talk_to_our_experts_now\"><\/span><!-- [et_pb_line_break_holder] -->    Talk to our experts now<!-- [et_pb_line_break_holder] -->  <span class=\"ez-toc-section-end\"><\/span><\/h1>\n<p><!-- [et_pb_line_break_holder] -->  \n<div class=\"wpcf7 no-js\" id=\"wpcf7-f1888-o1\" lang=\"en-US\" dir=\"ltr\">\n<div class=\"screen-reader-response\"><p role=\"status\" aria-live=\"polite\" aria-atomic=\"true\"><\/p> <ul><\/ul><\/div>\n<form action=\"\/cit-blog\/wp-json\/wp\/v2\/posts\/69#wpcf7-f1888-o1\" method=\"post\" class=\"wpcf7-form init\" aria-label=\"Contact form\" novalidate=\"novalidate\" data-status=\"init\">\n<div style=\"display: none;\">\n<input type=\"hidden\" name=\"_wpcf7\" value=\"1888\" \/>\n<input type=\"hidden\" name=\"_wpcf7_version\" value=\"5.8.6\" \/>\n<input type=\"hidden\" name=\"_wpcf7_locale\" value=\"en_US\" \/>\n<input type=\"hidden\" name=\"_wpcf7_unit_tag\" value=\"wpcf7-f1888-o1\" \/>\n<input type=\"hidden\" name=\"_wpcf7_container_post\" value=\"0\" \/>\n<input type=\"hidden\" name=\"_wpcf7_posted_data_hash\" value=\"\" \/>\n<input type=\"hidden\" name=\"_wpcf7_recaptcha_response\" value=\"\" \/>\n<\/div>\n<div class=\"form-block\" style=\"    background: #fff;\">\n\t<h3 style=\"    background: #0C2464;\n    border-bottom: 5px solid #cecece;\n    border-radius: 5px 5px 90px 90px;\n    margin: 0 auto;\n    text-align: center;\n    padding: 20px;\n    color: #fff;    margin-bottom: 15px;\"><span class=\"ez-toc-section\" id=\"Talk_To_Our_Experts_Now\"><\/span><b>Talk To Our Experts Now<\/b>\n\t<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\t<div style=\"padding:20px;\">\n\t\t<p><span class=\"wpcf7-form-control-wrap\" data-name=\"your-name\"><input size=\"40\" class=\"wpcf7-form-control wpcf7-text wpcf7-validates-as-required your-name\" aria-required=\"true\" aria-invalid=\"false\" placeholder=\"Name\" value=\"\" type=\"text\" name=\"your-name\" \/><\/span>\n\t\t<\/p>\n\t\t<p><span class=\"wpcf7-form-control-wrap\" data-name=\"your-email\"><input size=\"40\" class=\"wpcf7-form-control wpcf7-email wpcf7-validates-as-required wpcf7-text wpcf7-validates-as-email your-email\" aria-required=\"true\" aria-invalid=\"false\" placeholder=\"Email\" value=\"\" type=\"email\" name=\"your-email\" \/><\/span>\n\t\t<\/p>\n\t\t<p><span class=\"wpcf7-form-control-wrap\" data-name=\"your-number\"><input size=\"40\" class=\"wpcf7-form-control wpcf7-tel wpcf7-validates-as-required wpcf7-text wpcf7-validates-as-tel your-number\" aria-required=\"true\" aria-invalid=\"false\" placeholder=\"Phone Number\" value=\"\" type=\"tel\" name=\"your-number\" \/><\/span>\n\t\t<\/p>\n\t\t<p><span class=\"wpcf7-form-control-wrap\" data-name=\"message\"><textarea cols=\"40\" rows=\"10\" class=\"wpcf7-form-control wpcf7-textarea wpcf7-validates-as-required form-message\" aria-required=\"true\" aria-invalid=\"false\" placeholder=\"Message\" name=\"message\"><\/textarea><\/span>\n\t\t<\/p>\n\t<span class=\"wpcf7-form-control-wrap recaptcha\" data-name=\"recaptcha\"><span data-sitekey=\"6LfFkQATAAAAAIYlZ_UH9UozO-OLkpAaWPWx6QtM\" class=\"wpcf7-form-control wpcf7-recaptcha g-recaptcha\"><\/span>\r\n<noscript>\r\n\t<div class=\"grecaptcha-noscript\">\r\n\t\t<iframe loading=\"lazy\" src=\"https:\/\/www.google.com\/recaptcha\/api\/fallback?k=6LfFkQATAAAAAIYlZ_UH9UozO-OLkpAaWPWx6QtM\" frameborder=\"0\" scrolling=\"no\" width=\"310\" height=\"430\">\r\n\t\t<\/iframe>\r\n\t\t<textarea name=\"g-recaptcha-response\" rows=\"3\" cols=\"40\" placeholder=\"reCaptcha Response Here\">\r\n\t\t<\/textarea>\r\n\t<\/div>\r\n<\/noscript>\r\n<\/span>\n\t\t<div class=\"form-buttons\">\n\t\t\t<p><input class=\"wpcf7-form-control wpcf7-submit has-spinner\" type=\"submit\" value=\"Get a free quote\" \/>\n\t\t\t<\/p>\n\t\t<\/div>\n\t<\/div>\n<\/div><div class=\"wpcf7-response-output\" aria-hidden=\"true\"><\/div>\n<\/form>\n<\/div>\n<!-- [et_pb_line_break_holder] --><\/div>\n<p>[\/et_pb_code][\/et_pb_column][\/et_pb_row][\/et_pb_section]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"on","_et_pb_old_content":"<!-- \t \t -->\n\n<span style=\"text-decoration:underline;\"><strong>Print all GMF diagrams with a single click<\/strong><\/span>\n\nWe know that GMF gives only three types of Print Options for the Print Range.\n\na. Current Diagram\n\nb. All diagrams from open models\n\nc. Selected diagrams.\n\nSuppose 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.\n\nPlease 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\n\n<strong>DiagramWithPrintGlobalActionHandlerProvider<\/strong> class\n\n<strong>DiagramWithPrintGlobalActionHandler<\/strong> class\n\n<strong>EnhancedPrintActionHelper<\/strong> class\n\n<strong>DiagramPrinterUtil<\/strong> class\n\n<strong>DiagramWithPrintGlobalActionHandlerProvider<\/strong> is the class that provides GlobalActionHandlerProvider for print functionality. This will be defined in our plugin's plugin.xml\n\nBefore 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.\n\nAs the first step create four new classes in a package of your choice with names\n\n<strong>CustomDiagramWithPrintGlobalActionHandlerProvider.java <\/strong>\n\n<strong>CustomDiagramWithPrintGlobalActionHandler.java <\/strong>\n\n<strong>CustomEnhancedPrintActionHelper.java <\/strong>\n\n<strong>CustomDiagramPrinterUtil.java<\/strong>\n\nCopy 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 <strong>GlobalActionHandlerProvider<\/strong> definition to as shown below.\n\n&lt;extension point=\"org.eclipse.gmf.runtime.common.ui.services.action.globalActionHandlerProviders\"&gt;\n<p align=\"left\">&lt;GlobalActionHandlerProvider<\/p>\n<p align=\"left\">class=\"<strong>mypackage.printing.CustomDiagramWithPrintGlobalActionHandlerProvider<\/strong>\"<\/p>\n<p align=\"left\">id=\"mydomainPresentationPrint\"&gt;<\/p>\n<p align=\"left\">&lt;Priority name=\"High\"\/&gt;<\/p>\n<p align=\"left\">&lt;ViewId id=\"mypackage.CustomDiagramEditorID\"&gt;<\/p>\n<p align=\"left\">&lt;ElementType class=\"org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart\"&gt;<\/p>\n<p align=\"left\">&lt;GlobalActionId actionId=\"print\"\/&gt;<\/p>\n<p align=\"left\">&lt;\/ElementType&gt;<\/p>\n<p align=\"left\">&lt;\/ViewId&gt;<\/p>\n<p align=\"left\">&lt;\/GlobalActionHandlerProvider&gt;<\/p>\n<p align=\"left\">&lt;\/extension&gt;<\/p>\nOnce 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.\n\nMake 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.\n\nSo 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.\n\nStep 1: Open up <strong>CustomDiagramWithPrintGlobalActionHandlerProvider.java<\/strong> and make changes shown below.\n\n...........................\n\n<strong>if<\/strong> (!getHandlerList().containsKey(context.getActivePart())) {\n<p align=\"left\">getHandlerList().put(context.getActivePart(),<\/p>\n<p align=\"left\"><strong>new<\/strong> <strong>CustomDiagramWithPrintGlobalActionHandler()<\/strong>);<\/p>\n...........................\n<p align=\"left\"><strong>return<\/strong> (<strong>CustomDiagramWithPrintGlobalActionHandler<\/strong>) getHandlerList().get(context.getActivePart());<\/p>\n<p align=\"left\">...........................<\/p>\n<p align=\"left\">Step 2:Open up <strong>CustomDiagramWithPrintGlobalActionHandler <\/strong>and make changes as shown below.<\/p>\n<p align=\"left\"><strong>protected<\/strong> <strong>void<\/strong> doPrint(IGlobalActionContext cntxt) {<\/p>\n<p align=\"left\">IPrintActionHelper helper = <strong>new <\/strong><strong>CustomEnhancedPrintActionHelper()<\/strong>;<\/p>\n<p align=\"left\">helper.doPrint(cntxt.getActivePart());<\/p>\n<p align=\"left\">}<\/p>\n<p align=\"left\"><\/p>\n<p align=\"left\">..............................<\/p>\n<p align=\"left\"><\/p>\n<p align=\"left\">Step 3:open up <strong>CustomDiagramPrinterUtil.java<\/strong><\/p>\n<p align=\"left\"><\/p>\n<p align=\"left\">This is the class we are going to do a lot of coding to attain our goal.<\/p>\nWhat we are going to do is\n\n#1.Get all the diagram files from the Workspace using our diagram file's extension as an Array list.\n\n#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.\n\n<span style=\"text-decoration:underline;\"><strong>#1.I find all the diagram files present in my workspace using my diagram's extension.<\/strong><\/span>\n\n<strong>private<\/strong> <strong>static<\/strong> Collection getWorkspaceFiles(String extension)\n<p align=\"left\">{<\/p>\n<p align=\"left\">ArrayList files = <strong>new<\/strong> ArrayList();<\/p>\n<p align=\"left\"><\/p>\n<p align=\"left\">IWorkspaceRoot root = ResourcesPlugin.<em>getWorkspace<\/em>().getRoot();<\/p>\n<p align=\"left\"><\/p>\n<p align=\"left\"><\/p>\n<p align=\"left\">IProject[] projects = root.getProjects();<\/p>\n<p align=\"left\"><\/p>\n<p align=\"left\"><\/p>\n<p align=\"left\"><strong>for<\/strong> (<strong>int<\/strong> i=0;i&lt;projects.length;i++)<\/p>\n<p align=\"left\">{<\/p>\n<p align=\"left\"><strong>try<\/strong> {<\/p>\n<p align=\"left\">IResource[] resources = projects[i].members();<\/p>\n<p align=\"left\"><strong>for<\/strong> (<strong>int<\/strong> j=0;j&lt;resources.length;j++)<\/p>\n<p align=\"left\">{<\/p>\n<p align=\"left\"><em>getChildren<\/em>(resources[j], files, extension);<\/p>\n<p align=\"left\">}<\/p>\n<p align=\"left\">}<\/p>\n<p align=\"left\"><strong>catch<\/strong> (CoreException ex)<\/p>\n<p align=\"left\">{\/*ignore*\/}<\/p>\n<p align=\"left\">}<\/p>\n<p align=\"left\"><strong>return<\/strong> files;<\/p>\n<p align=\"left\">}<\/p>\n<p align=\"left\"><\/p>\n<p align=\"left\"><strong>private<\/strong> <strong>static<\/strong> <strong>void<\/strong> getChildren(IResource res, ArrayList files, String extension)<\/p>\n<p align=\"left\">{<\/p>\n<p align=\"left\"><strong>if<\/strong> (res <strong>instanceof<\/strong> IFile)<\/p>\n<p align=\"left\">{<\/p>\n<p align=\"left\"><strong>if<\/strong> (extension == <strong>null<\/strong> ||( res.getFileExtension()!=<strong>null<\/strong> &amp;&amp; res.getFileExtension().endsWith(extension) ))<\/p>\n<p align=\"left\">files.add(res.getFullPath().toString());<\/p>\n<p align=\"left\">}<\/p>\n<p align=\"left\"><strong>if<\/strong> (res <strong>instanceof<\/strong> IFolder)<\/p>\n<p align=\"left\">{<\/p>\n<p align=\"left\"><strong>try<\/strong> {<\/p>\n<p align=\"left\">IResource[] resources = ((IFolder)res).members();<\/p>\n<p align=\"left\"><strong>for<\/strong> (<strong>int<\/strong> j=0;j&lt;resources.length;j++)<\/p>\n<p align=\"left\">{<\/p>\n<p align=\"left\"><em>getChildren<\/em>(resources[j], files, extension);<\/p>\n<p align=\"left\">}<\/p>\n<p align=\"left\">}<\/p>\n<p align=\"left\"><strong>catch<\/strong> (CoreException ex)<\/p>\n<p align=\"left\">{}<\/p>\n<p align=\"left\">}<\/p>\n<p align=\"left\">}<\/p>\n<span style=\"text-decoration:underline;\"><strong>#2.Ina loop we Will Open a diagram,Print the Diagram and Close the diagram.<\/strong><\/span>\n\nFor 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.\n\n................\n\n<strong>else<\/strong> <strong>if<\/strong> (helper.getDlgDiagramPrintRangeAll()) {\n<p align=\"left\">flag=1;<\/p>\n<p align=\"left\"><\/p>\n<p align=\"left\">ArrayList files = (ArrayList)<em>getWorkspaceFiles<\/em>(\"mydomain_diagram\");<\/p>\n<p align=\"left\">System.<em>out<\/em>.println(\"Number of diagrams to be printed is\"+files.size());<\/p>\n<p align=\"left\"><strong>for<\/strong>(<strong>int<\/strong> i=0;i&lt;files.size();i++){<\/p>\n<p align=\"left\">System.<em>out<\/em>.println(\"Diagram opened is\"+files.get(i));<\/p>\n<p align=\"left\"><em>openDiagram<\/em>(files.get(i).toString());<\/p>\n<p align=\"left\"><em>PrintDiagram<\/em>(diagramPrinter,helper);<\/p>\n<p align=\"left\"><em>closeDiagram<\/em>();<\/p>\n<p align=\"left\"><\/p>\n<p align=\"left\">}<\/p>\n............\n\nAnd the Functions for Opening and Printing and Closing are as follows:\n\n\/\/Method for Opening the Diagram\n\n<strong>private<\/strong> <strong>static<\/strong> <strong>void<\/strong> openDiagram(String DiagramName){\n<p align=\"left\"><\/p>\n<p align=\"left\"><\/p>\n<p align=\"left\">URI diagramModelFilename = URI.<em>createURI<\/em>(DiagramName);<\/p>\n<p align=\"left\">GMFResource myGMFDiagram = <strong>new<\/strong> GMFResource(diagramModelFilename);<\/p>\n<p align=\"left\"><strong>try<\/strong> {<\/p>\n<p align=\"left\">myGMFDiagram.load(Collections.<em>EMPTY_MAP<\/em>);<\/p>\n<p align=\"left\"><strong>myDomainDiagramEditorUtil<\/strong>.<em>openDiagram<\/em>(myGMFDiagram);<\/p>\n<p align=\"left\">} <strong>catch<\/strong> (IOException e1) {<\/p>\n<p align=\"left\">e1.printStackTrace();<\/p>\n<p align=\"left\">} <strong>catch<\/strong> (PartInitException e) {<\/p>\n<p align=\"left\">System.<em>out<\/em>.println(\"Error opening diagram editor\"+e.getStatus());<\/p>\n<p align=\"left\">}<\/p>\n<p align=\"left\">}<\/p>\n........\n\n\/\/Method for Printing the Diagram\n\n........\n\n<strong>private<\/strong> <strong>static<\/strong> <strong>void<\/strong> PrintDiagram(DiagramPrinter diagramPrinter,IPrintHelper helper){\n<p align=\"left\"><\/p>\n<p align=\"left\">DiagramEditor dEditor = <strong>null<\/strong>;<\/p>\n<p align=\"left\">List diagramEditors = EditorService.<em>getInstance<\/em>().getRegisteredEditorParts();<\/p>\n<p align=\"left\">Iterator it = diagramEditors.iterator();<\/p>\n<p align=\"left\"><strong>while<\/strong> (it.hasNext()) {<\/p>\n<p align=\"left\"><\/p>\n<p align=\"left\">Object obje = it.next();<\/p>\n<p align=\"left\"><strong>if<\/strong> (obje <strong>instanceof<\/strong> DiagramEditor) { \/\/DiagramDocumentEditor<\/p>\n<p align=\"left\">dEditor = (DiagramEditor) obje;<\/p>\n<p align=\"left\">}<\/p>\n<p align=\"left\">}<\/p>\n<p align=\"left\">diagramPrinter.setDiagrams(Collections.<em>singletonList<\/em>(dEditor.getDiagram()));<\/p>\n<p align=\"left\"><em>printDiagrams<\/em>(diagramPrinter, helper);<\/p>\n<p align=\"left\">System.<em>out<\/em>.println(\"File Printed\");<\/p>\n<p align=\"left\"><\/p>\n<p align=\"left\">}<\/p>\n<p align=\"left\"><\/p>\n<p align=\"left\">........<\/p>\n<p align=\"left\"><\/p>\n<p align=\"left\">\/\/Method for Closing the Diagram<\/p>\n<p align=\"left\"><\/p>\n<p align=\"left\">........<\/p>\n<p align=\"left\"><strong>private<\/strong> <strong>static<\/strong> <strong>void<\/strong> closeDiagram(){<\/p>\n<p align=\"left\"><\/p>\n<p align=\"left\">IWorkbench workbench = PlatformUI.<em>getWorkbench<\/em>();<\/p>\n<p align=\"left\"><strong>if<\/strong> (workbench != <strong>null<\/strong>) {<\/p>\n<p align=\"left\"><\/p>\n<p align=\"left\"><strong>if<\/strong> (workbench.getWorkbenchWindows() != <strong>null<\/strong><\/p>\n<p align=\"left\">&amp;&amp;workbench.getWorkbenchWindows()[0].getActivePage() != <strong>null<\/strong>) {<\/p>\n<p align=\"left\">IWorkbenchPage page = workbench.getWorkbenchWindows()[0].getActivePage();<\/p>\n<p align=\"left\"><strong>if<\/strong> (page != <strong>null<\/strong>&amp;&amp; page.getEditorReferences().length &gt; 0) {<\/p>\n<p align=\"left\">ArrayList&lt;Resource&gt; resource = <strong>new<\/strong> ArrayList&lt;Resource&gt;();<\/p>\n<p align=\"left\"><strong>for<\/strong> (IEditorReference editorReference : page.getEditorReferences()) {<\/p>\n<p align=\"left\"><\/p>\n<p align=\"left\">TransactionalEditingDomain editingDomain = ((SgmoduleDiagramEditor) editorReference<\/p>\n<p align=\"left\">.getPart(<strong>false<\/strong>))<\/p>\n<p align=\"left\">.getEditingDomain();<\/p>\n<p align=\"left\"><strong>if<\/strong> (editingDomain != <strong>null<\/strong>) {<\/p>\n<p align=\"left\">ResourceSet resourceSet = editingDomain<\/p>\n<p align=\"left\">.getResourceSet();<\/p>\n<p align=\"left\"><strong>for<\/strong> (Resource element : resourceSet<\/p>\n<p align=\"left\">.getResources()) {<\/p>\n<p align=\"left\">resource.add(element);<\/p>\n<p align=\"left\">}<\/p>\n<p align=\"left\"><strong>for<\/strong> (Resource element : resource) {<\/p>\n<p align=\"left\"><\/p>\n<p align=\"left\">resourceSet.getResources()<\/p>\n<p align=\"left\">.remove(element);<\/p>\n<p align=\"left\">}<\/p>\n<p align=\"left\">}<\/p>\n<p align=\"left\">((SgmoduleDiagramEditor) editorReference<\/p>\n<p align=\"left\">.getPart(<strong>false<\/strong>)).close(<strong>false<\/strong>);<\/p>\n<p align=\"left\"><\/p>\n<p align=\"left\">}<\/p>\n<p align=\"left\"><\/p>\n<p align=\"left\">}<\/p>\n<p align=\"left\"><\/p>\n<p align=\"left\">}<\/p>\n<p align=\"left\">}<\/p>\n<p align=\"left\"><\/p>\n<p align=\"left\">System.<em>out<\/em>.println(\"Diagram closed\");<\/p>\n<p align=\"left\">}<\/p>\n<p align=\"left\"><\/p>\n<p align=\"left\"><\/p>\n<p align=\"left\">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.<\/p>\n<p align=\"left\"><\/p>\n<p align=\"left\">Hope this helps some one. Queries\/Suggestions Welcome<\/p>\n<p align=\"left\"><\/p>\n<p align=\"left\">Anoop Menon<\/p>","_et_gb_content_width":"","footnotes":""},"categories":[4],"tags":[],"_links":{"self":[{"href":"https:\/\/www.confianzit.com\/cit-blog\/wp-json\/wp\/v2\/posts\/69"}],"collection":[{"href":"https:\/\/www.confianzit.com\/cit-blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.confianzit.com\/cit-blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.confianzit.com\/cit-blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.confianzit.com\/cit-blog\/wp-json\/wp\/v2\/comments?post=69"}],"version-history":[{"count":2,"href":"https:\/\/www.confianzit.com\/cit-blog\/wp-json\/wp\/v2\/posts\/69\/revisions"}],"predecessor-version":[{"id":2395,"href":"https:\/\/www.confianzit.com\/cit-blog\/wp-json\/wp\/v2\/posts\/69\/revisions\/2395"}],"wp:attachment":[{"href":"https:\/\/www.confianzit.com\/cit-blog\/wp-json\/wp\/v2\/media?parent=69"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.confianzit.com\/cit-blog\/wp-json\/wp\/v2\/categories?post=69"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.confianzit.com\/cit-blog\/wp-json\/wp\/v2\/tags?post=69"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}