Find out source project of a model element via Open API

The project reference feature in Visual Paradigm allow users to reuse model elements from different projects. This not just saving effort in re-creating same model element again and again across projects, but also maintain the consistence of models as the changes on the model element automatically reflect on the projects which using it. In complex situation such as multiple levels of reference project sometimes it will be difficult to identify the origin of a particular model element, especially when the diagrams are develop by multiple users. By using Open API we can create plugin to find out the origin project of a model element. In this article we will show you how to create this plugin.

The basic

Our plugin will attach a popup menu to model element for lookup its origin project. To do this we have to develop a plugin action controller class which is implementing VPContextActionController.

public class FindOriginalProjectActionControl implements VPContextActionController {

Obtain the selected elements in diagram

Now let’s start implement the logic to obtain the origin project of the selected model elements. We do the implementation in the performAction() method. We can use the getSelectedDiagramElement() method from IDiagramUIModel to obtain the selection into an IDiagramElement array for further processing.

// Obtain the selected shapes on diagram
IDiagramElement[] selectedElements = ApplicationManager.instance().getDiagramManager().getActiveDiagram().getSelectedDiagramElement();

Find out its containing project

Once we obtained the selected elements we can work on it one by one to find out its containing project. We first create 2 variable to store the project obtained from model element and the project path. After that we obtain the actual model element (IModelElement) from IDiagramElement.

// Walk though the elements
for (int i = 0; i < selectedElements.length; i++) {
    IProject project = null;
    String projectPath = "current opening project";

    IDiagramElement element = selectedElements[i];

    // Retrieve the underlying model element of the selected shapes
    IModelElement model = element.getMetaModelElement();

Next we detect is the model element mirrored from reference project. If it is mirrored from reference project then we retrieve the project from mirrored model element. Otherwise we retrieve the project directly from the current working model element.

// Check is the model element mirrored from reference project.
IModelElement mirrorSource = model.getMirrorSource();

// If the mirrored source != null means it is from reference
// project. In this case we obtain the project from mirror source   
if (mirrorSource != null) {
    project = mirrorSource.getProject();
} else {
    // Otherwise the model element could be just local element, or
    // from reference project but without create mirror. In this case
    // we can simply obtain the project from the model element
    project = model.getProject();                                                   
}

Besides the above cases there is still one possible situation that user is working on a project that never being saved. In this case the project value will simply null.  If the project is not null then we retrieve its file path, otherwise leave the file path as default value “current opening project”.

// Obtain the project file path. If project == null means
// user is working on a new project which ever being save.
if (project != null) {
    File f = project.getProjectFile();
    if (f != null) {
        projectPath = f.getAbsolutePath();	
    }
}

Output the result to message pane

Finally we output the name of the selected model element and its project file path to message pane.

// Output the project information of the selected element to message pane    		ApplicationManager.instance().getViewManager().showMessage("Source project for " + model.getName() + ": " + projectPath);	

The Plugin XML

The plugin.xml is a configuration file inside the plugin, which instruct Visual Paradigm where the plugin menu should be added, and which action should be performed. As our plugin is to add a menu to element’s popup menu, we can specify the menuPath as “relatedElements/#”. This will allow us to create the popup menu under the Related Elements sub-menu.

<actionSets>
    <contextSensitiveActionSet id="com.vp.plugin.sample.findoriginalproject.actionet2">
        <contextTypes all="true"/>
        <action
              id="com.vp.plugin.sample.findoriginalproject.actions.Import2"
              label="Find Original Project"
              style="normal"
              menuPath="relatedElements/#">
            <actionController class="com.vp.plugin.sample.findoriginalproject.actions.FindOriginalProjectActionControl"/>
        </action>
    </contextSensitiveActionSet>
</actionSets>

Sample Plugin

The sample plugin attached to demonstrate how to find out the source project of a model element. After you deploy the plugin into Visual Paradigm you can right click on an element in diagram and select Related Elements > Find Original Project to find out where this element comes form.

Lookup source project of an element using the plugin

Lookup source project of an element using the plugin

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