@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:
In this example we use EuGENia to implement a GMF editor with end labels in connections.
.emf files are Ecore metamodels expressed using the Emfatic textual syntax.
More examples are available in the examples folder of the SVN repository.