Creating Diagram and model file programmatically [message #809903] |
Wed, 29 February 2012 12:25  |
ganesh ram Messages: 26 Registered: January 2012 |
Junior Member |
|
|
Hi,
Am trying to create the Diagram and model file programmatically.
I used the code similar to what is there in XXDiagramEditorUtil.createDiagram().
In the normal case (when i have an empty domain ie nothing is set into the domain object), the diagram and the domain file gets created and works fine.
Assume my model is of type XX, and that has a list of YY.
i create YY and set it inside XX.
Now when i create the domain file, the <YY> is set correctly inside <XX>.
But in the diagram file, i dont see the <children/> tags corresponding to the YY.
How would the appropriate <children/> tags be generated ?
i tried diagram.createChild(yy.eClass()), but the diagram file does not get generated.
Pls help.
AbstractTransactionalCommand command = new AbstractTransactionalCommand(editingDomain,
Messages.XXXDiagramEditorUtil_CreateDiagramCommandLabel,
Collections.EMPTY_LIST) {
protected CommandResult doExecuteWithResult(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
XXX model = createInitialModel();
YYY yyy = XXXFactory.eINSTANCE.createYYY();
yyy.setAttribute1("2");
yyy.setAttribute2("name");
model.getYYYs().add(yyy);
attachModelToResource(model, modelResource);
Diagram diagram = ViewService.createDiagram(model, XXXEditPart.MODEL_ID,XXXEditorPlugin.DIAGRAM_PREFERENCES_HINT);
if (diagram != null) {
diagramResource.getContents().add(diagram);
diagram.setName(diagramName);
diagram.setElement(model);
//diagram.createChild(yyy.eClass());
//diagram.insertChild(arg0);
}
try {
modelResource.save(.getSaveOptions());
diagramResource.save(getSaveOptions());
} catch (IOException e) {
}
return CommandResult.newOKCommandResult();
}
};
[Updated on: Wed, 29 February 2012 12:26] Report message to a moderator
|
|
|
Re: Creating Diagram and model file programmatically [message #809914 is a reply to message #809903] |
Wed, 29 February 2012 12:37   |
Andreas Muelder Messages: 73 Registered: July 2011 |
Member |
|
|
Hi ganesh,
you have two options to get it working.
The first one is using a CanonicalEditPolicy, which creates all Nodes and Edges for you based on your semantic model. This is easy to implement, but it has some drawbacks. Your editor will get dirty after opening it, because your Notation Model is changes and no layout information is available.
The better way is to create Nodes and Edges for each semantic element you created.
For your code snippet it might look like this:
XXX model = createInitialModel();
YYY yyy = XXXFactory.eINSTANCE.createYYY();
yyy.setAttribute1("2");
yyy.setAttribute2("name");
model.getYYYs().add(yyy);
attachModelToResource(model, modelResource);
Diagram diagram = ViewService.createDiagram(model, XXXEditPart.MODEL_ID,XXXEditorPlugin.DIAGRAM_PREFERENCES_HINT);
Node yyyView = ViewService.createNode(diagram, yyy, SemanticHints.YYY,
,XXXEditorPlugin.DIAGRAM_PREFERENCES_HINT);
//Set some layout information assuming you use a XYLayout
Bounds bounds = NotationFactory.eINSTANCE.createBounds();
bounds.setX(70);
bounds.setY(20);
yyyView.setLayoutConstraint(bounds);
...
Regards,
Andreas
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.02021 seconds