Insert Implementation Code to Operation via Open API

Visual Paradigm support defining implementation detail to operation model via the specification dialog. Besides you can also do this via Open API. In this example we will show you how to create a simple class model with an operation, and specify its implementation.

We first create a blank class diagram.

// create blank class diagram
DiagramManager diagramManager = ApplicationManager.instance().getDiagramManager();
IClassDiagramUIModel diagram = (IClassDiagramUIModel) diagramManager.createDiagram(DiagramManager.DIAGRAM_TYPE_CLASS_DIAGRAM);

Next we create a class model and make it on the diagram.

// create class model and shape
IClass myClass = IModelElementFactory.instance().createClass();
myClass.setName("MyClass");
IClassUIModel myClassShape = (IClassUIModel) diagramManager.createDiagramElement(diagram, myClass);
myClassShape.setBounds(100, 100, 110, 40);
myClassShape.setRequestResetCaption(true);

One the class is created we then create an operation to it.

// create operation to class model
IOperation operation = myClass.createOperation();
operation.setName("myOperation");
operation.setVisibility(IOperation.VISIBILITY_PUBLIC);

Now obtain the IJavaOperationCodeDetail from the operation. If it is null then we simply create one.

// retrieve Java code detail from operation
IJavaOperationCodeDetail javaDetail = operation.getJavaDetail();
if (javaDetail == null) {
  javaDetail = IModelElementFactory.instance().createJavaOperationCodeDetail();
}

Next we retrieve the IImplModel from IJavaOperationCodeDetail. Again, create one if it is null.

// retrieve implementation model from Java code detail
IImplModel implModel = javaDetail.getImplModel();
if (implModel == null) {
  implModel = IModelElementFactory.instance().createImplModel();
}

Now we can specify the implementation to the IImplModel.

// specify the implementation code
implModel.setCode("//To Do");

Finally we set the IImplModel back to IJavaOperationDetail, and set the IJavaOperationDetail back to the operation, then show up the diagram.

// set the implementation model to java detail
javaDetail.setImplModel(implModel);
// set the Java code detail to operation
operation.setJavaDetail(javaDetail);
// show up the diagram
diagramManager.openDiagram(diagram);

Sample Plugin

The sample plugin attached in this article demonstrates how you can insert implementation code to operation. After you deploy your plugin into Visual Paradigm you can click the Java Implementation button to create a class with operation, and having a comment code inserted to the operations.

Trigger sample plugin

Trigger sample plugin

Download Sample Plugin

You can click this link to download the sample plugin.

Related Know-how

Related Links

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply