|
|
|
Re: Initializing Diagrams Programmatically [message #1324159 is a reply to message #1323948] |
Wed, 30 April 2014 12:44   |
Eclipse User |
|
|
|
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 15:48   |
Eclipse User |
|
|
|
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
}
|
|
|
Re: Initializing Diagrams Programmatically [message #1341405 is a reply to message #1337244] |
Thu, 08 May 2014 09:19  |
Eclipse User |
|
|
|
Oliver,
that are all valid solutions, besides there is an implementation in the
package org.eclipse.graphiti.examples.tutorial (part of the finished
tutorial plugin in the SDK). Basically it contains 2 classes:
- the first one is an Eclipse handler that is registered for a context menu
entry in plugin.xml; but it actually does not matter from where you call
this coding, it will simply require an Eclipse project to contain the
diagram resource.
- the second one is an EMF Transactions command that creates content on the
new diagram.
Have you yeen that?
Michael
|
|
|
Powered by
FUDForum. Page generated in 0.06023 seconds