|
Re: Initializing Diagrams Programmatically [message #1323879 is a reply to message #1323649] |
Wed, 30 April 2014 13:45 |
Soeren M Messages: 77 Registered: September 2011 |
Member |
|
|
Do you mean something like that:
X eobject = XFactory.eINSTANCE.createX();
eobject.setID(...);
eobject.setDelayText(...);
eobject.setDescriptionText(...);
((ContainerShape) targetContainer).eResource().getContents().add(eobject);
AddContext addContext = new AddContext();
addContext.setNewObject(eobject);
addContext.setLocation(...);
addContext.setTargetContainer(...);
IAddFeature addFeature = fp.getAddFeature(addContext);
IAddFeature addFeature = fp.getAddFeature(addContext);
if (addFeature.canAdd(addContext)) {
Pe pe = addFeature.add(addContext);
}
It creates an eObject plus the graphical representation...
[Updated on: Wed, 30 April 2014 13:45] Report message to a moderator
|
|
|
|
Re: Initializing Diagrams Programmatically [message #1324159 is a reply to message #1323948] |
Wed, 30 April 2014 16:44 |
Soeren M Messages: 77 Registered: September 2011 |
Member |
|
|
That's not the way to initialize a diagram, what you want to do is to create a business object for your diagram.
For that issue you can do the following:
PictogramLink link = diagram.getLink();
if (link == null) {
EditingDomain editingDomain = AdapterFactoryEditingDomain.getEditingDomainFor(diagram);
Command command;
// Create yout object (eObject)
X diagramBO = XFactory.eINSTANCE.createX();
// Add the x object to the diagram resource
command = new AddCommand(editingDomain, diagram.eResource().getContents(), diagramBO);
editingDomain.getCommandStack().execute(command);
// Create the pictogram link object
link = PictogramsFactory.eINSTANCE.createPictogramLink();
link.getBusinessObjects().add(diagramBO);
// Set the pictogram link object for the diagram object
command = new SetCommand(editingDomain, diagram, PictogramsPackage.eINSTANCE.getDiagram()
.getEStructuralFeature(PictogramsPackage.DIAGRAM__LINK), link);
editingDomain.getCommandStack().execute(command);
}
if (link.getBusinessObjects().size() == 1 && link.getBusinessObjects().get(0) instanceof CASPA) {
// Return the diagram object
return (X) link.getBusinessObjects().get(0);
} else {
throw new IllegalStateException();
}
I dont know if there exists a better way, I've found this way in this forum as well...
Anyways, if you create a methode with that code, you always get your root element and you are able to add elements to it.
|
|
|
Re: Initializing Diagrams Programmatically [message #1337244 is a reply to message #1324159] |
Tue, 06 May 2014 19:48 |
Hernan Gonzalez Messages: 188 Registered: October 2010 Location: Buenos Aires, Argentina |
Senior Member |
|
|
In case this helps, this is how (schematically) I create a Diagram programatically, loading only the model:
==========================================================================
1) Create the domain with the two empty resources (diagram and model)
- from inside DiagramEditor.init()...
TransactionalEditingDomain domain (created blank)
Resource diagres = domain.getResourceSet().createResource(diagramUri);
Resource modelres = domain.getResourceSet().createResource(modelUri);
==========================================================================
2) Load the model plus an associated empty -to be filled- diagram:
- from DiagramEditor.setInput() -> diagramBehaviour.setInput() ->
persistentcyBehaviour.loadDiagram()... {
domain = diagramBehavior.getEditingDomain();
uridiagRes = domain.getResourceSet().getResources().get(0).getURI();
modelres = domain.getResourceSet().getResource(urimodel, false);
diagres = domain.getResourceSet().getResource(uridiagRes, false);
modelres.load(Collections.EMPTY_MAP);
diagram = Graphiti.getCreateService().createDiagram(...);
commandStack = domain.getCommandStack();
commandStack.execute(new RecordingCommand(domain) {
protected void doExecute() {
diagres.getContents().add(diagram); // adds the diagram EMF object to the resource
}
});
domain.getCommandStack().flush();
}
================================================
3) Create DTP and CP and wire the DTP to the model
- inside DiagramEditor.setInput() :
dtp = GraphitiUi.getExtensionManager().createDiagramTypeProvider(:..)
dtp.init(diagram, this);
IConfigurationProviderInternal configurationProvider = new ConfigurationProvider(this, dtp )
setConfigurationProvider(configurationProvider);
================================================
4) Link the root model object to the diagram and fill the diagram
fillDiagram(dtp, domain, diagram, modelres) {
rootObj= modelres.getContents().get(0);
featureProvider = dtp.getFeatureProvider();
stack = domain.getCommandStack();
stack.execute(new RecordingCommand(domain) {
protected void doExecute() {
// Link root object with diagram
PictogramLink ret = PictogramsFactory.eINSTANCE.createPictogramLink();
ret.setPictogramElement(diagram);
ret.getBusinessObjects().add(rootObj);
diagram.getPictogramLinks().add(ret);
// fill diagram objects from model
featureProvider.fillFullDiagram(diagram); // navigate all the model objects and call the corresponding AddFeatures
}
});
domain.getCommandStack().flush(); // without this, it would be dirty
}
|
|
|
|
Powered by
FUDForum. Page generated in 0.03968 seconds