我有一个非常大的ZEST树,显示一个Hashtree(Merkletree)。由于它的大小和有限的可用空间,它变得如此压缩,你不能再读它了:
因此,我希望能够获得比实际shell更多的空间,并实现滚动/拖动选项以使用鼠标移动。
但是,我找不到一个可以包含它的子元素,它不会填充到我所拥有的空间中。
我已经尝试过SashForm
(org.eclipse.swt.custom.SashForm),但它不能变得比窗口大。
是否有可能实施我的计划,或者SWT通常不支持?
我对Zest知之甚少,因此你应该首先看看Zest本身是否提供缩放和/或滚动。
如果没有内置支持,您可以使用SWT的ScrolledComposite
。浏览此处获取更多信息:
通过将Graph
的样式设置为SWT.V_SCROLL | SWT.H_SCROLL
,您可以使图表可滚动:
Graph graph = new Graph(shell, SWT.H_SCROLL | SWT.V_SCROLL);
过了一段时间,我得到了一个体面的工作。我使用一个简单的PaintListener和方法setSize。对于缩放我使用类org.eclipse.gef.editparts.ZoomManager。我发现了一个很大的缺点,这个类需要很多性能,当然还有其他解决方案。
我希望代码能说明原因和方法。
public class ZoomableZestGraph extends Composite {
private GraphViewer graphViewer;
private Graph graph;
public ZoomableZestGraph(Composite parent, int style) {
super(parent, style);
this.setLayout(new GridLayout(1, true));
this.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1,1));
//create a GraphViewer and Graph
graphViewer = new GraphViewer(this, SWT.V_SCROLL | SWT.H_SCROLL);
graph = graphViewer.getGraphControl();
graph.setLayoutAlgorithm(new TreeLayoutAlgorithm(LayoutStyles.NO_LAYOUT_NODE_RESIZING), true);
graph.setHorizontalScrollBarVisibility(Graph.ALWAYS);
graph.setVerticalScrollBarVisibility(Graph.ALWAYS);
//Fill our graph with some nodes and connect them
GraphNode node1 = new GraphNode(graph, SWT.NONE, "Earendil");
GraphNode node2 = new GraphNode(graph, SWT.NONE, "Elros");
GraphNode node3 = new GraphNode(graph, SWT.NONE, "Elrond");
GraphNode node4 = new GraphNode(graph, SWT.NONE, "Elladan");
GraphNode node5 = new GraphNode(graph, SWT.NONE, "Elrohir");
GraphNode node6 = new GraphNode(graph, SWT.NONE, "Arwen");
new GraphConnection(graph, ZestStyles.CONNECTIONS_DIRECTED, node1, node2);
new GraphConnection(graph, ZestStyles.CONNECTIONS_DIRECTED, node1, node3);
new GraphConnection(graph, ZestStyles.CONNECTIONS_DIRECTED, node2, node4);
new GraphConnection(graph, ZestStyles.CONNECTIONS_DIRECTED, node2, node5);
new GraphConnection(graph, ZestStyles.CONNECTIONS_DIRECTED, node2, node6);
/*
This graphViewer consists of 2 components: the control and the graph (Figure)
We want to give the control a size by the layout and the graph a custom, bigger value.
For the control (graphViewer.getControl) I simply grab all available space
*/
GridDataFactory.fillDefaults().grab(true, true).applyTo(graphViewer.getControl());
//For the graph we have to create a PaintListener.
graph.addPaintListener(new PaintListener() {
@Override
public void paintControl(PaintEvent e) {
graph.setSize(1300, 1080);
}
});
//The Graph now fills the shell/parent composite,
//but the actual graph size can be set as we want in the paint //listener
//Zooming with the class org.eclipse.gef.editparts.ZoomManager
//As arguments we need a ScalableFigure which we receive by graph.getRootLayer and the Viewport.
ZoomManager zoomManager = new ZoomManager(graph.getRootLayer(), graph.getViewport());
//we bind the zoom mechanic to a simple mouse wheel listener
graph.addMouseWheelListener(new MouseWheelListener() {
@Override
public void mouseScrolled(MouseEvent e) {
if (e.count < 0) {
zoomManager.zoomOut();
} else {
zoomManager.zoomIn();
}
}
});
//We give the focus to our graphViewer, so it receives the MouseWheel Events
graphViewer.getControl().forceFocus();
}
@Override
protected void checkSubclass() {
//we are a composite subclass
}
}
注意:我没有包含导入