Changing Order of Child Elements using Open API
The Model Explorer is the place which showing entire model collection in your project as well as their structures. Visual Paradigm provide different ways for present the elements in Model Explore by using different sorting algorithms. But even the best sorting algorithms sometimes may not fit you need. In this case you may need to manually order the elements in the order you like. With Open API you can do this programmatically by creating a plugin. In this article we will show you how to change the order of child elements in the selected parent model element.
Obtain the selected element for changing child elements order
First we have to obtain the selected element from Model Explorer tree.
// Obtain the selected elements in Model Explorer tree Object[] selectedObjects = ApplicationManager.instance().getViewManager().getSelectedObjectsFromModelExplorer(); if (selectedObjects != null && selectedObjects.length > 0) { // Get the first element in selection Object selectedObject = selectedObjects[0];
Retrieve the child elements into array
Once we obtained the selected element we then retrieve all its children into an array.
if (selectedObject instanceof IModelElement) { IModelElement parent = (IModelElement) selectedObject; // Obtain the child model element from the selected model element IModelElement[] children = parent.toChildArray();
Shift the child elements order
Next we shift the order of child element by one and put them into another array.
if (children != null && children.length > 0) { // Put the child elements into another collection with order shifted for one // then remove the child element form parent IModelElement[] newOrder = new IModelElement[children.length]; for (int i = 0; i < children.length; i++) { if (children.length > i+1) { newOrder[i+1] = children[i]; } else { newOrder[0] = children[i]; } parent.removeChild(children[i]); }
Add the child elements back to parent in shifted order
Finally we add the child element in shifted order back to parent.
// Add the child element back to parent according to the shifted order for (IModelElement child : newOrder) { parent.addChild(child); }
Sample plugin
The sample plugin attached demonstrate how to change the order of child element of the selected element in Model Explorer. Once you deployed the plugin into Visual Paradigm you can select the element in Model Explorer tree then press the plugin button to swap the order of child elements.
Download sample plugin
You can click this link to download the sample plugin.
Related Know-how |
Related Link |
Leave a Reply
Want to join the discussion?Feel free to contribute!