3

我正在使用带有 SpaceTreeLayoutAlgorithm 的 GraphViewer,并在创建图形后尝试更新节点的标签/图标。

已经测试过的变种

viewer.refresh(node, true);

viewer.refresh(viewer.getGraphModelNode(node), true);

结合查看器或其图形控件的各种刷新/重绘方法,但没有成功。

有关查看器使用的更多详细信息:

GraphViewer viewer = new GraphViewer(parent, SWT.BORDER);
ArrayContentProvider content = new MyArrayContentProvider(..);

viewer.setContentProvider(content);
LabelProvider glp = new MyGraphLabelProvider(..);
viewer.setLabelProvider(glp);

List<MyNodeClass> nodes = .. //list of all node elements
viewer.setInput(nodes);

TriangleSubgraphFactory subGraphFactory = new TriangleSubgraphFactory();
SpaceTreeLayoutAlgorithm spaceTreeAlg = new SpaceTreeLayoutAlgorithm();

viewer.getGraphControl().setSubgraphFactory(subGraphFactory);
viewer.getGraphControl().setExpandCollapseManager(
    spaceTreeAlg.getExpandCollapseManager());
viewer.setLayoutAlgorithm(spaceTreeAlg, true);

代码按预期工作,除了我上面提到的刷新。

我想我可能做错了什么,因为 Fabian 推荐的调用 viewer.setInput(..) 会导致错误。我也没有设法将 SpaceTreeLayoutAlgorithm(int direction) 与 TOP_DOWN 以外的任何方向一起使用,否则我会得到 NPE。

稍后调用 viewer.setInput 会导致:

Caused by: java.util.NoSuchElementException
at java.util.HashMap$HashIterator.nextEntry(HashMap.java:813)
at java.util.HashMap$KeyIterator.next(HashMap.java:845)
at org.eclipse.zest.core.widgets.custom.TriangleSubgraph.updateFigure(TriangleSubgraph.java:169)
at org.eclipse.zest.core.widgets.custom.TriangleSubgraph$1.defaultHandle(TriangleSubgraph.java:147)
at org.eclipse.zest.layouts.algorithms.TreeLayoutObserver$TreeListener.nodeRemoved(TreeLayoutObserver.java:308)
at org.eclipse.zest.layouts.algorithms.TreeLayoutObserver$1.nodeRemoved(TreeLayoutObserver.java:344)
at org.eclipse.zest.core.widgets.InternalLayoutContext.fireNodeRemovedEvent(InternalLayoutContext.java:399)
at org.eclipse.zest.core.widgets.InternalNodeLayout.dispose(InternalNodeLayout.java:327)
at org.eclipse.zest.core.widgets.Graph.removeNode(Graph.java:977)
at org.eclipse.zest.core.widgets.GraphNode.dispose(GraphNode.java:188)
at org.eclipse.zest.core.viewers.AbstractStructuredGraphViewer.inputChanged(AbstractStructuredGraphViewer.java:494)
at org.eclipse.zest.core.viewers.GraphViewer.inputChanged(GraphViewer.java:136)
at org.eclipse.jface.viewers.ContentViewer.setInput(ContentViewer.java:280)
at org.eclipse.jface.viewers.StructuredViewer.setInput(StructuredViewer.java:1690)

以下用于刷新,但这不可能是预期的用途:

GraphLabelProvider glp
GraphNode n = viewer.getGraphModelNode(node);
n.setText(glp.getText(node));
n.setImage(glp.getImage(node));
4

1 回答 1

2

我不确定您是如何准确使用查看器的,但最简单的方法可能是将查看器的输入重置为您想要可视化的新数据(您在内容和标签提供程序中处理)。当您设置输入时,图表会自动更新:

viewer.setInput(myChangedDataToBeVisualized);
于 2012-05-24T16:01:05.200 回答