文档半岛外围网上直营>>Visual Paradigm教程>>Visual Paradigm教程:如何使用Open API更改子元素的顺序
Visual Paradigm教程:如何使用Open API更改子元素的顺序
模型资源管理器是在项目中显示整个模型集合以及它们的结构的地方。通过使用不同的排序算法,Visual Paradigm提供了展示模型中元素的不同方法,但即使是最好的排序算法有时也不适合你。在这种情况下,您可能需要以你想要的方式对排列元素手动排序。使用Open API,你可以通过创建插件来进行程序化控制。在本文中,我们将向你展示如何更改所选定的父模型元素中子元素的顺序。
获取需要更改子元素顺序的选定元素
首先,我们必须从模型资源管理器树中获取所选的元素。
// 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];
将子元素检索到数组中
一旦我们获得了选中的元素,我们就会将所有的子元素检索到一个数组中。
if (selectedObject instanceof IModelElement) {
IModelElement parent = (IModelElement) selectedObject;
// Obtain the child model element from the selected model element
IModelElement[] children = parent.toChildArray();
更改子元素的顺序
接下来,我们将子元素的顺序从一个数组移到另一个数组中。
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 element back to parent according to the shifted order
for (IModelElement child : newOrder) {
parent.addChild(child);
}
示例插件
示例插件展示了如何更改模型资源管理器中所选元素的子元素的顺序。当你将插件部署到Visual Paradigm之后,你可以从模型资源管理器(Model Explorer)树中选择元素然后点击插件按钮插件按钮来交换子元素的顺序。

相关:

QQ交谈
在线咨询

渝公网安备
50010702500608号

客服热线