Generate Code Back From Diagram [message #1736840] |
Sat, 02 July 2016 17:39  |
Eclipse User |
|
|
|
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 264 times)
|
|
|
Re: Generate Code Back From Diagram [message #1737615 is a reply to message #1736840] |
Mon, 11 July 2016 09:54   |
Eclipse User |
|
|
|
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 #1738851 is a reply to message #1738732] |
Mon, 25 July 2016 12:59   |
Eclipse User |
|
|
|
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  |
Eclipse User |
|
|
|
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.
|
|
|
Powered by
FUDForum. Page generated in 0.03478 seconds