GMF - programmatically create connections without commands

Posted by user1437515 on Stack Overflow See other posts from Stack Overflow or by user1437515
Published on 2012-06-07T16:39:12Z Indexed on 2012/06/07 16:40 UTC
Read the original article Hit count: 525

Filed under:
|
|
|

Hi I am developing an graphical editor with GMF and want to create a set of nodes (Resources in my project) and connections between them upon intialization of a new diagram. I do not want to use commands here because without the code is much slimmer, easier to read and also faster it seems to me.

There is no problem creating nodes by calling my XXXFactory.eInstance.createResource() and adding them to the diagram model. My connection is contained as source-/targetConnections feature in the resource class. So I added a similarly created connection as source/target to the resources. But it will not show up in the diagram even though it exists in the ResourceImpl structural feature. Maybe I need to add it additionally to the diagram but since its contained by a feature contained by the diagram, I dont know how.

Am i doing something wrong or missing something or is it just not possible to do this without commands. Any help would be much appreciated. Sample code is below. The output of it is the two nodes but no connection.

Thanks Lars

Example createInitialModel method:

private static RDFEditor.ShapesDiagram createInitialModel() {

  ShapesDiagram diagram = >RDFEditor.RDFEditorFactory.eINSTANCE.createShapesDiagram();
  RDFEditor.Resource res = RDFEditorFactory.eINSTANCE.createResource();
  RDFEditor.Resource res2 = RDFEditorFactory.eINSTANCE.createResource();
  Connection con = RDFEditorFactory.eINSTANCE.createConnection();

  EStructuralFeature target = >res.eClass().getEStructuralFeature("targetConnections");
  EStructuralFeature source = >res.eClass().getEStructuralFeature("sourceConnections");

  res2.setName("rdfs:Resource");
  res.setName("rdfs:Class");
  con.setName("rdfs:type");

  con.setSource(res);
  con.setTarget(res2);

  res.getSourceConnections().add(con);
  res.getTargetConnections().add(con);    


  //res2.eSet(target, con);   
  //res.eSet(source, con);

  List<? extends Shape> resList = Arrays.asList(res,res2);

  EStructuralFeature shapes = >diagram.eClass().getEStructuralFeature("shapes");
  diagram.eSet(shapes, resList);

  return diagram;

}

© Stack Overflow or respective owner

Related posts about java

Related posts about eclipse