我正在使用带有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(..)会导致错误。我也没有设法使用除TOP_DOWN之外的任何方向的SpaceTreeLayoutAlgorithm(int direction),否则我得到一个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));
答案 0 :(得分:2)
我不确定您是如何准确使用查看器的,但最简单的方法可能是将查看器的输入重置为您想要显示的新数据(您在内容和标签提供程序中处理的数据)。设置输入时,图表会自动更新:
viewer.setInput(myChangedDataToBeVisualized);