How to Create Containment Relationship with API

Open API not only allows you access the model data within the project, but also creating new model elements and connector. This is not only for standard connectors such as association, but also relationships such as containment. This article will show you how to create containment relationship using Open API.

Create Class Model and Shape

To demonstrate the creation of containment relationship, we need to create 2 models with which can contain the other. The simplest case is classes. By creating 2 classes, we can model the inner class relationship with containment connector. To create the parent class, you can use IModelElementFactory.instance().createClass(). This method will return an instance of the IClass.

Once the parent class has been created, you can then create the child class by calling lClass.createChild(IModelElementFactory.MODEL_TYPE_CLASS). This will create a child class model under the parent class.

After create the class models, you can then create class shape to diagram by calling DiagramManager.createDiagramElement(lDiagram, lClass).

Create Containment Connector Model

The containment relationship can be created by calling DiagramManager.createConnector(lDiagram, IClassDiagramUIModel.SHAPETYPE_CONTAINMENT, FromElement, ToElement, TurningPoints). In this example, the FromElement should the parent class, while the ToElement should be the child class. And as the simplest example, we just create a straight containment connector. Therefore we can pass null to the Turning points array. The complete method call should be as follow

DiagramManager.createConnector(lDiagram, IClassDiagramUIModel.SHAPETYPE_CONTAINMENT, lClassShape, lInnerClassShape, null);

Finally you can call the auto-layout function to layout the classes also the connector in your diagram.

IDiagramUIModel.layoutDiagramElement(new IDiagramElement[] {lClassShape, lInnerClassShape});

Sample Plugins

This sample plugin demonstrate how to create nested class with containment relationships. After deploy the plugin to VP applications, you can right click on the blank area of the class diagram and select Create Inner Class from popup menu to trigger it.

Plugin menu

Plugin menu

Download Sample Plugin

You can download the sample plugins at here.

Related Articles

Related Links

1532 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply