Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » GMF (Graphical Modeling Framework) » Programmatically navigate and export Diagram to PNG file
Programmatically navigate and export Diagram to PNG file [message #1008623] Wed, 13 February 2013 10:54 Go to next message
Gabriele Pongelli is currently offline Gabriele PongelliFriend
Messages: 9
Registered: February 2013
Junior Member
Hi all,
I'm working with ecorediag file and I have files with more than one Diagram per file.
I need to export each diagram programmatically to PNG file (like manually doing "right button -> file -> save as image file..." per diagram).

First question.
How is it possible to access to each diagram in java?
A diagram's java class is "org.eclipse.gmf.runtime.notation.Diagram" but how can I obtain them from ecorediag file?

I've found EcoreDiagramEditor class, having a getDiagram() method but it returns only one Diagram.
If this is the correct method, how can I navigate to get the others?
I've tried getNavigationManager().next() to an EcoreDiagramEditor object after opening a IWokbenchPage, but it doesn't change diagram.


Second question.
Having a Diagram object, how can I export it to a PNG file?



Thank you!
Re: Programmatically navigate and export Diagram to PNG file [message #1008962 is a reply to message #1008623] Thu, 14 February 2013 08:13 Go to previous messageGo to next message
Ralph Gerbig is currently offline Ralph GerbigFriend
Messages: 702
Registered: November 2009
Senior Member
Hi,

you should start at the following class: org.eclipse.gmf.runtime.diagram.ui.render.internal.dialogs.CopyToImageDialog and take a look what it expects as input. Then you should take a look on how to get the expected input for the operations which are done in this dialog.

Ralph
Re: Programmatically navigate and export Diagram to PNG file [message #1009521 is a reply to message #1008962] Fri, 15 February 2013 09:18 Go to previous messageGo to next message
Gabriele Pongelli is currently offline Gabriele PongelliFriend
Messages: 9
Registered: February 2013
Junior Member
Thank you, I'll see that class to export my diagrams.
Do you know how it's possible to navigate ecorediag's diagram ?
Re: Programmatically navigate and export Diagram to PNG file [message #1009704 is a reply to message #1009521] Fri, 15 February 2013 14:53 Go to previous messageGo to next message
Ralph Gerbig is currently offline Ralph GerbigFriend
Messages: 702
Registered: November 2009
Senior Member
Hi,

what do you mean by navigate ecore diagrams?

Ralph
Re: Programmatically navigate and export Diagram to PNG file [message #1009737 is a reply to message #1009704] Fri, 15 February 2013 16:03 Go to previous messageGo to next message
Gabriele Pongelli is currently offline Gabriele PongelliFriend
Messages: 9
Registered: February 2013
Junior Member
I've ecorediag file having more than one diagram inside it, similar to
<?xml version="1.0" encoding="UTF-8"?>
<xmi:XMI xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore" xmlns:notation="http://www.eclipse.org/gmf/runtime/1.0.2/notation">
  <notation:Diagram xmi:id="_TnwF0HeGEeKGu7kx0XPfRQ" type="EcoreTools" name="first.ecorediag" measurementUnit="Pixel">
    <children xmi:type="notation:Node" xmi:id="_ToOm8HeGEeKGu7kx0XPfRQ" type="1001">
      ...
    <edges xmi:type="notation:Edge" xmi:id="_To-N0neGEeKGu7kx0XPfRQ" type="3002" source="_ToOm8HeGEeKGu7kx0XPfRQ" target="_TowLbneGEeKGu7kx0XPfRQ">
      ...
    </edges>
  </notation:Diagram>
  <notation:Diagram xmi:id="_VuV-4HeGEeKGu7kx0XPfRQ" type="EcoreTools" name="test.ecorediag" measurementUnit="Pixel">
    <children xmi:type="notation:Node" xmi:id="_WQXG0HeGEeKGu7kx0XPfRQ" type="1001">
...
    </children>
    <edges xmi:type="notation:Edge" xmi:id="_W85csHeGEeKGu7kx0XPfRQ" type="3003" source="_W8raQHeGEeKGu7kx0XPfRQ" target="_WQXG0HeGEeKGu7kx0XPfRQ">
..
    </edges>
  </notation:Diagram>
</xmi:XMI>

As you can see, there are two diagram "first.ecorediag" and "test.ecorediag", I can see them from Outline view of ecorediag editor.

I wish save them separately, so I need one of them at time to load and, after that, use CopyToImageDialog class.

I've tried
IDE.openEditor(
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(), 
new URIEditorInput(URI.createFileURI(ecoreDiagramFilepath), 
EcoreDiagramEditor.ID);

with this code I can open ecorediag file showing "first.ecorediag" but I can't open "test.ecorediag" .

Another test I've done was about use NavigationManager, returned by EcoreDiagramEditor, to call its next() method, but after opening the editor on first.ecorediag, this method does anything.
I could call NavigationManager.set() but I need a Diagram object, that I don't know how I can obtain from ecorediag file.


EcoreDiagramOutlinePage, to loead other selected diagram, does
((EcoreDiagramEditor) getEditor()).setDiagram((Diagram) selectedObject);

Again I don't know how to obtain an object from ecorediag file.



Thank you!
Re: Programmatically navigate and export Diagram to PNG file [message #1010789 is a reply to message #1009737] Mon, 18 February 2013 08:49 Go to previous messageGo to next message
Ralph Gerbig is currently offline Ralph GerbigFriend
Messages: 702
Registered: November 2009
Senior Member
Hi,

you could load both with EMF save them in different files and print them. Did you try this?

Ralph
Re: Programmatically navigate and export Diagram to PNG file [message #1010801 is a reply to message #1010789] Mon, 18 February 2013 09:08 Go to previous messageGo to next message
Gabriele Pongelli is currently offline Gabriele PongelliFriend
Messages: 9
Registered: February 2013
Junior Member
Hi Ralph,
I've not tried because I don't know how it's possible to load both with EMF .
Can you tell me more details ?


Thank you.
Re: Programmatically navigate and export Diagram to PNG file [message #1010942 is a reply to message #1010801] Mon, 18 February 2013 14:18 Go to previous messageGo to next message
Ralph Gerbig is currently offline Ralph GerbigFriend
Messages: 702
Registered: November 2009
Senior Member
Hi,

you can see here how to do it http://andy.ekiwi.de/?p=829

Ralph
Re: Programmatically navigate and export Diagram to PNG file [message #1011087 is a reply to message #1008623] Mon, 18 February 2013 20:36 Go to previous messageGo to next message
Andreas Muelder is currently offline Andreas MuelderFriend
Messages: 73
Registered: July 2011
Member
Hi Gabriele,

if you loaded the resource you can iterate over its contents and filter for instances of Diagram. For each Diagram, you can use the DiagramRenderUtil to render an SWT Image.
Image image = DiagramRenderUtil.renderToSWTImage(diagram);

Then you can save the image to your file system.

Best regards,

Andreas
Re: Programmatically navigate and export Diagram to PNG file [message #1011346 is a reply to message #1011087] Tue, 19 February 2013 11:20 Go to previous messageGo to next message
Gabriele Pongelli is currently offline Gabriele PongelliFriend
Messages: 9
Registered: February 2013
Junior Member
@Ralph Thank you, I'm reading that post.

@Andreas Thank you, but these are some exception I get running that code
LogFilter.isLoggable threw a non-fatal unchecked exception as follows:
java.lang.NullPointerException
	at org.eclipse.core.internal.runtime.Log.isLoggable(Log.java:101)
	at org.eclipse.equinox.log.internal.ExtendedLogReaderServiceFactory.safeIsLoggable(ExtendedLogReaderServiceFactory.java:59)
	at org.eclipse.equinox.log.internal.ExtendedLogReaderServiceFactory.logPrivileged(ExtendedLogReaderServiceFactory.java:164)
	at org.eclipse.equinox.log.internal.ExtendedLogReaderServiceFactory.log(ExtendedLogReaderServiceFactory.java:150)
	at org.eclipse.equinox.log.internal.ExtendedLogServiceFactory.log(ExtendedLogServiceFactory.java:65)
	at org.eclipse.equinox.log.internal.ExtendedLogServiceImpl.log(ExtendedLogServiceImpl.java:87)
	at org.eclipse.equinox.log.internal.LoggerImpl.log(LoggerImpl.java:54)
	at org.eclipse.core.internal.runtime.Log.log(Log.java:60)
	at org.eclipse.ui.statushandlers.WorkbenchErrorHandler.handle(WorkbenchErrorHandler.java:80)
	at org.eclipse.ui.internal.ide.IDEWorkbenchErrorHandler.handle(IDEWorkbenchErrorHandler.java:108)
	at org.eclipse.ui.internal.WorkbenchErrorHandlerProxy.handle(WorkbenchErrorHandlerProxy.java:36)
	at org.eclipse.ui.statushandlers.StatusManager.handle(StatusManager.java:189)
	at org.eclipse.ui.statushandlers.StatusManager.handle(StatusManager.java:231)
	at org.eclipse.ui.statushandlers.StatusManager.handle(StatusManager.java:242)
	at org.eclipse.ui.application.WorkbenchAdvisor.eventLoopException(WorkbenchAdvisor.java:326)
	at org.eclipse.ui.internal.ExceptionHandler.handleException(ExceptionHandler.java:65)
	at org.eclipse.jface.window.Window.runEventLoop(Window.java:829)
	at org.eclipse.jface.window.Window.open(Window.java:801)
	at org.eclipse.jface.dialogs.MessageDialog.open(MessageDialog.java:334)
	at org.eclipse.jface.dialogs.MessageDialog.open(MessageDialog.java:364)
	at org.eclipse.jface.dialogs.MessageDialog.openInformation(MessageDialog.java:446)

This openInformation is called inside my code.
It' strange because I've a big try-catch(Exception) around all my function, while this seems to not be catched.



Another fatal exception on renderToSWTImage
java.lang.NullPointerException
	at org.eclipse.gmf.runtime.diagram.core.listener.DiagramEventBroker.initializeDiagramEventBroker(DiagramEventBroker.java:243)
	at org.eclipse.gmf.runtime.diagram.core.listener.DiagramEventBroker.startListening(DiagramEventBroker.java:230)
	at org.eclipse.gmf.runtime.diagram.ui.OffscreenEditPartFactory.createDiagramEditPart(OffscreenEditPartFactory.java:126)
	at org.eclipse.gmf.runtime.diagram.ui.OffscreenEditPartFactory.createDiagramEditPart(OffscreenEditPartFactory.java:77)
	at org.eclipse.gmf.runtime.diagram.ui.render.util.DiagramRenderUtil.renderToSWTImage(DiagramRenderUtil.java:79)

Thrown by renderToSWTImage; I pass a Diagram to this function, retrieved as resource.getContents().get(0) from correctly read ecorediag file.


Thank you!
Re: Programmatically navigate and export Diagram to PNG file [message #1011381 is a reply to message #1011346] Tue, 19 February 2013 12:58 Go to previous messageGo to next message
Andreas Muelder is currently offline Andreas MuelderFriend
Messages: 73
Registered: July 2011
Member
Hi,
the second NPE seems to be thrown because the editing domain is null.
Try to load the resource with an editing domain:

TransactionalEditingDomain editingDomain = DiagramEditingDomainFactory.getInstance().createEditingDomain();
		editingDomain.getResourceSet().getResource(...)


Regards,

Andreas
Re: Programmatically navigate and export Diagram to PNG file [message #1011490 is a reply to message #1011381] Tue, 19 February 2013 16:21 Go to previous messageGo to next message
Gabriele Pongelli is currently offline Gabriele PongelliFriend
Messages: 9
Registered: February 2013
Junior Member
Hi Andreas, really thanks, you're right!
What about first NPE ?

Best Regards.

[Updated on: Tue, 19 February 2013 16:21]

Report message to a moderator

Re: Programmatically navigate and export Diagram to PNG file [message #1011505 is a reply to message #1011490] Tue, 19 February 2013 16:58 Go to previous message
Andreas Muelder is currently offline Andreas MuelderFriend
Messages: 73
Registered: July 2011
Member
The first Exception has nothing to do with GMF I guess Smile
Google for it you will find a lot of Eclipse Bugs for this topic.

Best regards,

Andreas

Previous Topic:Generated Code of ViewMap
Next Topic:Change generated DiagramFile.gif icon automatically
Goto Forum:
  


Current Time: Fri Apr 19 05:01:32 GMT 2024

Powered by FUDForum. Page generated in 0.02574 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top