我正在使用带有 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));