0

我正在使用 Zest 中的树布局显示模型。但是,我指定在每个节点中显示的文本太长了。我希望文本移动到节点内的下一行,以便可以显示所有文本,而不是截断大部分文本。我尝试对添加到图中的每个节点使用 setSize() ,但这似乎没有任何区别。谁能告诉我如何实现这一目标?谢谢。* 我添加了显示当前单词被截断的情况的代码。

import org.eclipse.swt.SWT;

import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;

import org.eclipse.swt.widgets.Display;

import org.eclipse.swt.widgets.Shell;

import org.eclipse.zest.core.widgets.CGraphNode;
import org.eclipse.zest.core.widgets.Graph;

import org.eclipse.zest.core.widgets.GraphConnection;

import org.eclipse.zest.core.widgets.GraphNode;
import org.eclipse.draw2d.FigureCanvas;
import org.eclipse.draw2d.ImageFigure;
//import org.eclipse.zest.layout.algorithms.RadialLayoutAlgorithm;

//import org.eclipse.zest.layout.interfaces.LayoutAlgorithm;
import org.eclipse.zest.layouts.algorithms.TreeLayoutAlgorithm;



public class TreeLayoutExample {

        public static void main(String[] args) {

                // Create the shell

                Display d = new Display();

                Shell shell = new Shell(d);

                shell.setText("GraphSnippet1");

                shell.setLayout(new FillLayout());

                shell.setSize(500, 500);



                final Graph g = new Graph(shell, SWT.NONE);

                g.setSize(500, 500);

                GraphNode root = new GraphNode(g, SWT.NONE, "");
                root.setSize(1000, 1000);

                for (int i = 0; i < 3; i++) {

                        GraphNode n = new GraphNode(g, SWT.NONE, "GIANT LONG TEXT");

                        n.setSize(300, 300);

                        for (int j = 0; j < 3; j++) {

                                GraphNode n2 = new GraphNode(g, SWT.NONE, "MORE GIANT LONG TEXT");
                                n2.setSize(300, 300);
                                new GraphConnection(g, SWT.NONE, n, n2).setWeight(-1);

                        }

                        new GraphConnection(g, SWT.NONE, root, n);

                }



                final TreeLayoutAlgorithm layoutAlgorithm = new org.eclipse.zest.layouts.algorithms.TreeLayoutAlgorithm();



                g.setLayoutAlgorithm(layoutAlgorithm, true);

                shell.open();

                while (!shell.isDisposed()) {

                        while (!d.readAndDispatch()) {

                                d.sleep();

                        }

                }

        }

} 
4

1 回答 1

0

这个解决方案不是很直观,但这里有:

  1. 将样式传递LayoutStyles.NO_LAYOUT_NODE_RESIZINGTreeLayoutAlgorithm

    TreeLayoutAlgorithm layoutAlgorithm = new TreeLayoutAlgorithm(LayoutStyles.NO_LAYOUT_NODE_RESIZING);
    
  2. 将节点的大小设置为setSize(-1, -1).

  3. 确保你有足够的可用空间,否则节点会重叠

这是最终的代码片段:

public static void main(String[] args)
{
    Display d = new Display();
    Shell shell = new Shell(d);
    shell.setText("GraphSnippet1");
    shell.setLayout(new FillLayout());
    shell.setSize(1280, 500);

    final Graph g = new Graph(shell, SWT.NONE);
    g.setSize(-1, -1);

    GraphNode root = new GraphNode(g, SWT.NONE, "ROOT");
    root.setSize(-1, -1);

    for (int i = 0; i < 3; i++)
    {
        GraphNode n = new GraphNode(g, SWT.NONE, "LONG TEXT");
        n.setSize(-1, -1);

        for (int j = 0; j < 2; j++)
        {
            GraphNode n2 = new GraphNode(g, SWT.NONE, "EVEN LONGER TEXT");
            n2.setSize(-1, -1);
            new GraphConnection(g, SWT.NONE, n, n2).setWeight(-1);
        }

        new GraphConnection(g, SWT.NONE, root, n);
    }

    TreeLayoutAlgorithm layoutAlgorithm = new TreeLayoutAlgorithm(LayoutStyles.NO_LAYOUT_NODE_RESIZING);
    g.setLayoutAlgorithm(layoutAlgorithm, true);

    shell.open();

    while (!shell.isDisposed())
    {
        while (!d.readAndDispatch())
        {
            d.sleep();
        }
    }
}

看起来像这样:

在此处输入图像描述

于 2014-07-29T08:59:46.350 回答