我在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();
}
}
}
}
答案 0 :(得分:0)
这个解决方案不太直观,但这里有:
将LayoutStyles.NO_LAYOUT_NODE_RESIZING
作为样式传递给TreeLayoutAlgorithm
:
TreeLayoutAlgorithm layoutAlgorithm = new TreeLayoutAlgorithm(LayoutStyles.NO_LAYOUT_NODE_RESIZING);
将节点大小设置为setSize(-1, -1)
。
确保您有足够的可用空间,否则节点将重叠
以下是最终的代码段:
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();
}
}
}
看起来像这样: