如何在包含 shell 的 ZEST 图表中滚动

How to scroll a ZEST Graph within its containing shell

我有一个相当大的 ZEST 树,显示一个 Hashtree (Merkletree)。由于它的大小和有限的可用性 space 它被压缩得无法再阅读了:

因此我希望能够抓取比实际 shell 更多的 space,并实现 scrolling/dragging 选项以使用鼠标移动。

但是,我找不到可以包含它的子元素,它没有填充到我拥有的 space 中。

我已经尝试过 SashForm (org.eclipse.swt.custom.SashForm),但它不能变得比 window 更大。

是否有可能实施我的计划,或者它在 SWT 中通常不受支持?

我对 Zest 知之甚少,所以你应该先看看 Zest 本身是否提供缩放 and/or 滚动。

如果没有内置支持,可以使用SWT的ScrolledComposite。请参阅此处了解更多信息:

通过将 Graph 的样式设置为 SWT.V_SCROLL | SWT.H_SCROLL,您可以使图形可滚动:

Graph graph = new Graph(shell, SWT.H_SCROLL | SWT.V_SCROLL);

一段时间后,我让它以一种体面的方式工作。我使用一个简单的 PaintListener 和方法 setSize。对于缩放,我使用 class org.eclipse.gef.editparts.ZoomManager。我发现了一个很大的缺点,这个 class 需要很多性能,当然还有其他解决方案。

我希望代码能说明原因和方式。

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
   }

}

注意:我没有包括进口