Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » Graphiti » Initializing Diagrams Programmatically
icon10.gif  Initializing Diagrams Programmatically [message #1323649] Wed, 30 April 2014 11:11 Go to next message
Oliver Stadie is currently offline Oliver StadieFriend
Messages: 2
Registered: April 2014
Junior Member
Hi everyone.

I worked through the tutorial and like how Graphiti works. Unfortunatelly the graphiti documentation behond the tutorial is very poor. So i have to ask it here.

Given my EMF Domain Objects, a diagram type, a diagram type provider and feature provider, I want to get to the point where I can show a diagram in the editor programmatically.

In my RCP application there is no projects view and no default "File->New..."-Entry and thus no way to start the "new diagram wizard". Also I dont want the user to interact with the wizard at all.

I don't know where to start. What objects and components do i have to create and how to connect them, to get the the same result, as if the end-user would have used the "new diagram wizard"?

Thanks for your help,
oli
Re: Initializing Diagrams Programmatically [message #1323879 is a reply to message #1323649] Wed, 30 April 2014 13:45 Go to previous messageGo to next message
Soeren M is currently offline Soeren MFriend
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 #1323948 is a reply to message #1323879] Wed, 30 April 2014 14:25 Go to previous messageGo to next message
Oliver Stadie is currently offline Oliver StadieFriend
Messages: 2
Registered: April 2014
Junior Member
I'm not sure what I mean myself, since my understanding of graphiti and RCP is very basic yet.

Your Code may be part of the solution, but my problem starts earlier. I don't have a targetContainer object. I found out, i can create one using
Diagram diagram = Graphiti.getPeCreateService().createDiagram(diagramTypeId, diagramName, true);


But if i do so, diagram.eResource() returns null.
Re: Initializing Diagrams Programmatically [message #1324159 is a reply to message #1323948] Wed, 30 April 2014 16:44 Go to previous messageGo to next message
Soeren M is currently offline Soeren MFriend
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 Go to previous messageGo to next message
Hernan Gonzalez is currently offline Hernan GonzalezFriend
Messages: 181
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
}


Re: Initializing Diagrams Programmatically [message #1341405 is a reply to message #1337244] Thu, 08 May 2014 13:19 Go to previous message
Michael Wenz is currently offline Michael WenzFriend
Messages: 1640
Registered: July 2009
Location: Walldorf, Germany
Senior Member
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
Previous Topic:Palette disappears
Next Topic:DiagramComposite in Eclipse 4
Goto Forum:
  


Current Time: Sat Dec 20 03:56:31 GMT 2014

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

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