Manipulate the nickname of a diagram and model element using Open API

tomcat-thumbYou might need to create diagrams in multilingual for different team members around the globe. Instead of maintaining multiple set of diagrams of the same models for several languages, the nickname feature in Visual Paradigm enables you to define multiple languages for a single set of diagrams, allowing you to switch between different languages in just a matter of a few clicks. The nickname can be manipulated directly from the GUI, or through the use of the Open API. This article shows you how develop a plugin yourself by swapping the original name of the diagram and model element with the current selected nickname.

Step 1: Retrieve diagrams and model elements in your project

Firstly, let’s retrieve the diagrams and model elements form the project. We can use the ApplicationManager.getProjectManager() to obtain the ProjectManager in VP, then obtain the Project object from by using ProjectManager.getProject().

// Obtain project from ProjectManager
IProject project = ApplicationManager.instance().getProjectManager().getProject();

Once we the project has been loaded, we can then retrieve the diagrams into an array using the IProject.toDiagramArray() method. After that, we loop through the array and pass it to the method explained in step 2 for making the “swapping”.

IDiagramUIModel[] diagrams = project.toDiagramArray();
if (diagrams != null && diagrams.length > 0) {
    for (int i = 0; i < diagrams.length; i++) {
        doSwap(diagrams[i]);
    }
}

Next we retrieve the model elements into an array using IProject.toModelElementArray(). Similarly we then loop through the array and pass the model element to the method explained in step 3 to make the “swapping”.

IModelElement[] models = project.toModelElementArray();
if (models != null && models.length > 0) {
    for (int i = 0; i < models.length; i++) {
        doSwap(model[i]);
    }
}

Step 2: Swapping the original name & nickname for the diagrams

There are 3 properties needed to be handled when swapping the nickname and the original name, they are the name of the diagram/model element, description in plain text and the description in HTML. We can make the swapping on these properties one by one using the getter and setter methods.

private void doSwap(IDiagramUIModel diagram) {

    // retrieve name property from original name and nickname
    String origName = diagram.getName();
    String nickName = diagram.getNickname();

    // swap the original name and nickname
    diagram.setName(nickName);
    diagram.setNickname(origName);

    // retrieve the original and nickname plain text description
    String origDescTxt = diagram.getDocumentationWithReferenceModels();
    String nickDescTxt = diagram.getHtmlDocumentationWithReferenceModels();

    // swap the original plain text description with nickname plain text description
    diagram.setDocumentation(nickDescTxt);
    diagram.setNickDocumentation(origDescTxt);

    // retrieve the original and nickname HTML description
    String origDescHtml = diagram.getHtmlDocumentationWithReferenceModels();
    String nickDescHtml = diagram.getNickHtmlDocumentationWithReferenceModels();

    // swap the original HTML description with nickname HTML description
    diagram.setHtmlDocumentation(nickDescHtml);
    diagram.setNickHtmlDocumentation(origDescHtml);
}

Step 3: Swapping the original name & nickname for the model elements

Similar to the diagram, the model element can perform the same way to swap the nickname with the original name. But since some of the model elements returned from IProject.toModelElementArray() are for internal use only, which may not have the name or description properties. To skip the process on those properties we need to put a try-catch block surrounding it.

private void doSwap(IModelElement model) {

    try {
        String origName = model.getName();
        String nickName = model.getNickname();

        model.setName(nickName);
        model.setNickname(origName);
    } catch (Exception e) {
    }

    try {
        String origDescTxt = model.getDescriptionWithReferenceModels();
        String nickDescTxt = model.getNickDescriptionWithReferenceModels();

        model.setDescription(nickDescTxt);
        model.setNickDescription(origDescTxt);
    } catch (Exception e) {
    }

    try {
        String origDescHtml = model.getHTMLDescriptionWithReferenceModels();
        String nickDescHtml = model.getNickHTMLDescriptionWithReferenceModels();

        model.setHTMLDescription(nickDescHtml);
        model.setNickHTMLDescription(origDescHtml);
    } catch (Exception e) {
    }

Finally, we retrieve the child element from the current working model element, and pass it back to the swap method for avoiding skipping the handling of the child elements.

    IModelElement[] children = model.toChildArray();
    if (children != null && children.length > 0) {
        for (int i = 0; i < children.length; i++) {
            doSwap(children[i]);
        }
    }
}

Sample Plugin

The sample plugin is attached to demonstrate how to swap the diagrams and model elements’ original name with nickname. After deploy your plugin into Visual Paradigm you can open your project and switch to your nickname, then click the Swap Nickname button for performing the swapping.

Change the project to the nickname and trigger the plugin to perform the swapping

Change the project to the nickname and trigger the plugin to perform the swapping

Download Sample Plugin

You can click this link to download the sample plugin.

Related Know-how

Related Link

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply