Hiding class attributes using Open API

Visual Paradigm Open API provide Java programming interface for manipulate diagrams and models within the project. In this article we will show you how to hide out attribute from class shape in class diagram. Suppose we would like to hide out the name attribute from Customer class in current opening diagram:

Retrieve class shapes form diagram

First we have to retrieve the class shapes form current opening diagram.

// Retrieve active diagram, and proceed if it is a class diagram
IDiagramUIModel currentDiagram = ApplicationManager.instance().getDiagramManager().getActiveDiagram();
if (currentDiagram instanceof IClassDiagramUIModel) {
  IClassDiagramUIModel classDiagram = (IClassDiagramUIModel) currentDiagram;
  
  // Retrieve all class shape in the diagram
  IDiagramElement[] diagramElements = classDiagram.toDiagramElementArray(IClassDiagramUIModel.SHAPETYPE_CLASS);
  if (diagramElements.length > 0) {
    // Walk through the class shapes
    for (int i = 0; i < diagramElements.length; i++) {
      IClassUIModel classShape = (IClassUIModel) diagramElements[i];

Obtain class model from class shape

Once we retrieved the class shapes we walk through them one by one to obtain the class model, and see is it the Customer class we looking for.

// Retrieve class model from class shape 
IClass classModel = (IClass) diagramElements[i].getModelElement();
// Retrieve attributes if the class named "Customer"
if ("Customer".equals(classModel.getName())) {

Hide out the specified attribute

When the Customer class was found, retrieve the attributes from it and found out the name attribute.

IAttribute[] attributes = classModel.toAttributeArray();
for (int j = 0; j < attributes.length; j++) { 
  // Walk through all attributes to see is there any name named as "name"
  if ("name".equals(attributes[j].getName())) {

When the name attribute was found, we add it to hidden attribute model and set the class to customized attribute display mode.

// Hide away to attribute which named as "name"
classShape.setHiddenAttributeModels(new IAttribute[] {attributes[j]});
classShape.setShowAttributeType(IClassUIModel.ATTR_SHOW_TYPE_CUSTOMIZED);													

Sample Plugin

The sample plugin attached demonstrate how to hide out the name attribute from Customer class. After you deploy the plugin into Visual Paradigm you can open the class diagram and press the Hide Attribute button in Plugin tab to hide out the attribute.

Execute sample plugin to hide out the name attribute

Execute sample plugin to hide out the name attribute

Download Sample Plugin

You can click this link to download the sample plugin.

Related Know-how

Related Link