Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » GMF (Graphical Modeling Framework) » Generate Code Back From Diagram
icon9.gif  Generate Code Back From Diagram [message #1736840] Sat, 02 July 2016 17:39 Go to next message
Leed Ch is currently offline Leed ChFriend
Messages: 2
Registered: July 2016
Junior Member
Greetings

I'm new to both EMF and GMF and I've been assigned a task to create an EMF metamodel then a GMF one based on it to generate graphical diagrams.

Then i need to recreate code or classes from the generated diagram. Take the diagram attachment as an example

What i need to get is code or classes that holds the values and whatever inputs i have in the diagram

is this possible ??? how to accomplish this ???

Regards

Leed

  • Attachment: Pic1.jpg
    (Size: 200.44KB, Downloaded 57 times)
Re: Generate Code Back From Diagram [message #1737615 is a reply to message #1736840] Mon, 11 July 2016 09:54 Go to previous messageGo to next message
Niklas Klipphahn is currently offline Niklas KlipphahnFriend
Messages: 7
Registered: July 2016
Junior Member
Sorry, I have no solution yet, but I need to do the same for my assignment.
Wouldn't it be sufficient to get a reference to the domain model element instance? All other elements should be saved in its attributes.

If I find a way to that I will post here again.
Re: Generate Code Back From Diagram [message #1737766 is a reply to message #1737615] Tue, 12 July 2016 16:01 Go to previous messageGo to next message
Niklas Klipphahn is currently offline Niklas KlipphahnFriend
Messages: 7
Registered: July 2016
Junior Member
This could be relevant:
https://www.eclipse.org/forums/index.php/t/361642/
Re: Generate Code Back From Diagram [message #1738732 is a reply to message #1737766] Fri, 22 July 2016 17:12 Go to previous messageGo to next message
Leed Ch is currently offline Leed ChFriend
Messages: 2
Registered: July 2016
Junior Member
Hi Niklas, thanks for your help.

do you know where to add this code exactly ???? in the plugin ? in a class that i create ??? i don't know really where to start.
Re: Generate Code Back From Diagram [message #1738851 is a reply to message #1738732] Mon, 25 July 2016 12:59 Go to previous messageGo to next message
Niklas Klipphahn is currently offline Niklas KlipphahnFriend
Messages: 7
Registered: July 2016
Junior Member
I haven't had time to look into this yet, but I have an approach in my mind.

For my project I will create an extra Button (don't know how yet, but I'm sure there's a way to do it). The code for that will be packaged somewhere within the plugin.
Then I'll assign an On-click action for that button that finds the "top element" of the active editor. (the "top element" is your "canvas" and "owns" all other elements)
Quote:
1. Get your actual workbench (IWorkbench workbench = MYGMFPROGRAM.getInstance().getWorkbench()Wink
2. Get your actual editor (editor = workbench.getActiveWorkbenchWindow().getActivePage().getActiveEditor()Wink
3. Casting the editor to a DiagramEditor (diagramEditor = (DiagramEditor) page.getActiveEditor();
diagram = diagramEditor.getDiagram()
4. Get the top of your diagram with: diagram.getElement();


Then I'll create a "CodeGen.java" class somewhere that gets the "top element" as argument in its constructor. This class will visit all elements and generate code from them.
I don't know how to generate code, but it should be easily found in google.

Sorry, but I can't help you more than that at the moment.
Re: Generate Code Back From Diagram [message #1743440 is a reply to message #1738732] Thu, 15 September 2016 13:41 Go to previous message
Niklas Klipphahn is currently offline Niklas KlipphahnFriend
Messages: 7
Registered: July 2016
Junior Member
I have finished the code generation and it works.

What I did was creating an eclipse plugin that creates an additional button in the eclipse GUI.
You can read how to do that here:http://www.vogella.com/tutorials/EclipseCommands/article.html

Following those steps I created a Handler class which handles button presses. I created a class XXXGenerator, which the handler creates an instance of.

The handler can then get the active diagram:
IEditorPart ieditorpart = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();		
		if(ieditorpart instanceof DiagramEditor){
			DiagramEditor editor = (DiagramEditor) ieditorpart;
			EObject object = editor.getDiagram().getElement();

then for example:
if(object instanceof DscProject){
						DscProject project = (DscProject) object;
DscCodeGen generator = new DscCodeGenJava();
						DirectoryDialog dialog =  new DirectoryDialog(window.getShell());
						dialog.setText("Enter directory for code generation");
						String url;
						url = dialog.open();
						if(url != null){
						generator.generate(url, project);
						MessageDialog.openInformation(
								window.getShell(),
								"Generator",
								"Code for " + project.getName() + "has been created"); 


The generator class then has access to all model elements via
project.getXXElement();
project.getXXEdge();
.
You can iterate over all your diagram elements. Don't forget to check the type and cast if needed.

I used JCodeModel to generate my code.

I hope it helps if you aren't already done with your assignment.
If you have further questions, just ask them.
Previous Topic:news.eclipse.org is shutting down.
Next Topic:Simple constraint using OCL
Goto Forum:
  


Current Time: Sun Aug 20 04:24:23 GMT 2017

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

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