Example: Implement a GMF editor with end labels in connections using EuGENia


  • endlabels.emf
  • ECore2GMF.eol
  • Screenshot.png
  • Get it!
@namespace(uri="endlabels", prefix="endlabels")
package endlabels;

@gmf.diagram(foo="bar")
class Model {
   val Class[*] clases;
   val Association[*] asociations;
}

abstract class NamedElement {
   attr String name;
}

@gmf.node(label="name")
class Class extends NamedElement {
     
}

@gmf.link(source="source", target="target")
class Association extends NamedElement {
   ref Class source;
   ref Class target;
   attr String sourceLabel;
   attr String targetLabel;
}
var association = ECore!EClass.all.selectOne(c|c.name = 'Association');
association.createLinkEndLabel('sourceLabel', true);
association.createLinkEndLabel('targetLabel', false);

operation ECore!EClass createLinkEndLabel(attribute:String, source:Boolean) {
  
  var endName;
  if (source) {
    endName = 'Source';
  }
  else {
    endName = 'Target';
  }
  
  var labelName = self.name + endName + 'Label';
  
  -- Create the figure descriptor and the label
  var labelFigureDescriptor = createFigureDescriptor(labelName + 'Figure');
  var label = new GmfGraph!Label;
  label.name = labelName;
  label.text = endName;
  labelFigureDescriptor.actualFigure = label;
  
  -- Create the diagram label
  var diagramLabel = new GmfGraph!DiagramLabel;
  diagramLabel.figure = labelFigureDescriptor;
  diagramLabel.name = labelName;
  diagramLabel.elementIcon = false;
  GmfGraph!Canvas.all.first().labels.add(diagramLabel);
  
  -- Specify if the label will be placed at the beginning/end of the link
  var alignmentFacet = new GmfGraph!AlignmentFacet;
  if (source){
    alignmentFacet.alignment = GmfGraph!Alignment#END.instance;
  }
  else {
    alignmentFacet.alignment = GmfGraph!Alignment#BEGINNING.instance;
  }
  diagramLabel.facets.add(alignmentFacet);
  
  -- Specify how far the label should be from the line
  var labelOffsetFacet = new GmfGraph!LabelOffsetFacet;
  labelOffsetFacet.x = 5;
  labelOffsetFacet.y = 5;
  diagramLabel.facets.add(labelOffsetFacet);
  
  -- Create the label/attribute mapping in the .gmfmap model
  var featureLabelMapping = new GmfMap!FeatureLabelMapping;
  featureLabelMapping.diagramLabel = diagramLabel;
  
  featureLabelMapping.features.add(self.eAllStructuralFeatures.selectOne(sf|sf.name = attribute));
  featureLabelMapping.readOnly = false;
  var linkMapping = GmfMap!LinkMapping.all.selectOne(lm|lm.domainMetaElement = self);
  linkMapping.labelMappings.add(featureLabelMapping);

}

operation createFigureDescriptor(name : String) {
  var figureDescriptor = new GmfGraph!FigureDescriptor;
  figureDescriptor.name = name;
  GmfGraph!FigureGallery.all.first().descriptors.add(figureDescriptor);
  return figureDescriptor;
}

There are two ways to get the code of this example:

  1. download the following zip archive(s), extract them and import them as new Eclipse projects
  2. or check out the code from the SVN
    • go to the SVN repository
    • navigate to trunk/examples
    • check out the org.eclipse.epsilon.eugenia.examples.endlabels project