我正在编写一个eclipse插件,用于绘制有限状态系统。由于它可能很大,我想附加一些现有的图形布局算法(例如分层布局、基于力的布局等),这些算法可以自动优化系统可视化。
有没有一种方法可以集成我正在编写的插件(使用GEF编写),以便按照一些常见的图形布局算法将生成的编辑部分放置在编辑器区域?
我发现这篇文章很有趣,但它没有优化编辑零件的可视化,而是专注于绘制一个全新的图形。
到目前为止,我正在添加以下代码(基于Zest1)
private static void createNewGraph(String autName) {
Shell tmpShell = new Shell();
currGraph = new Graph(tmpShell, SWT.NONE);
mapStateAndNodes = new HashMap<State, GraphNode>();
}
private static void addGraphNode(State currState)
{
GraphNode newNode = new GraphNode(currGraph, SWT.NONE, currState.getName());
mapStateAndNodes.put(currState, newNode);
}
private static void addGraphConnection(Transition currTrans)
{
GraphNode source = mapStateAndNodes.get(currTrans.getOrigState());
GraphNode dest = mapStateAndNodes.get(currTrans.getDestState());
GraphConnection newConn = new GraphConnection(currGraph, SWT.NONE, source, dest);
}
private static void completeGraph()
{
currGraph.setLayoutAlgorithm(new SpringLayoutAlgorithm(LayoutStyles.NO_LAYOUT_NODE_RESIZING), true);
}
在构建模型的同时,我还调用了createNewGraph(...)
、addGraphNode(...)
、addGraphConnection(...)
和completeGraph(...)
。问题是:在currGraph.setLayoutAlgorithm(..., true)
之后,true
意味着它应该应用算法并按"正确"的顺序放置对象。在这一点上(正如一些读者所建议的那样),可以通过GraphNode.getLocation()
方法提取计算出的坐标。不幸的是,在设置并应用布局之后,所有状态都将Point(0,0)
作为它们的位置。我还发现了这样的评论:
/**
* Runs the layout on this graph. It uses the reveal listener to run the
* layout only if the view is visible. Otherwise it will be deferred until
* after the view is available.
*/
public void applyLayout() {
...
}
在org.eclipse.zest.core.widgets.Graph
来源:-[对我来说,我似乎无法使用zestGraph库来完成这项工作。我错了吗?有其他选择吗?
如有任何帮助,我们将不胜感激:)
简而言之,Zest并不直接支持这种场景。
但是,您可以在内存中构建您编辑的零件的表示,这些零件可以使用Zest进行布局。在Zest 1.0中,您必须手动提供对Graph Nodes和Graph
附加想法:开源项目Spray支持Graphiti图的Zest布局(Graphiti扩展了GEF——也许有一些想法可以重用)。请参阅以下代码文件:http://code.google.com/a/eclipselabs.org/p/spray/source/browse/plugins/org.eclipselabs.spray.runtime.graphiti.zest/src/org/eclipselabs/spray/runtime/graphiti/zest/features/ZestLayoutDiagramFeature.java一些想法。
编辑:我查看了电脑中的相关代码;我们设法以以下方式在Zest 1.0中实现了这样的布局:
- 每个节点都有一个GraphNode,节点之间的每个弧都有一条Connection。您可以将它们收集到两个不同的数组中;例如
SimpleNode[] nodes; SimpleRelationship[] relationships;
- 实例化一个算法类,并根据需要设置可选参数
- 致电
algorithm.applyLayout(nodes, relationships, 0, 0, diagram.width, diagram.height, false, false)
-对不起,没有可用的Zest 1.0安装来检查参数的确切含义;前两个是使用的节点和关系;然后接下来的四套画板;老实说,我不知道最后两个是干什么的D
进一步澄清:Zest使用节点和关系作为一个术语——用关系代替我以前的弧线。