0

I'm displaying a model using the Tree Layout within Zest. However, the text that I have assigned to be displayed in each node is too long. I want the text to move to the next line within the node so all of it can be displayed instead of having most of it cut off. I tried using setSize() for each node that I add to the graph but that doesn't seem to make any difference. Could anyone please tell me how I can achieve this ? Thanks. * I have added code that shows the current situation where words get cut off.

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();

                        }

                }

        }

} 
Asher
  • 811
  • 3
  • 10
  • 19

1 Answers1

0

The solution to this one is not quite intuitive, but here goes:

  1. Pass LayoutStyles.NO_LAYOUT_NODE_RESIZING as the style to the TreeLayoutAlgorithm:

    TreeLayoutAlgorithm layoutAlgorithm = new TreeLayoutAlgorithm(LayoutStyles.NO_LAYOUT_NODE_RESIZING);
    
  2. Set the size of your nodes to setSize(-1, -1).

  3. Make sure you have enough available space, otherwise the nodes will overlap

Here is the final code snippet:

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();
        }
    }
}

Looks like this:

enter image description here

Baz
  • 36,440
  • 11
  • 68
  • 94
  • Thank you very much. However the tree gets massively screwed up when all node elements are not more or less of the same length. Do you know of any solution specifically that allows text within the node just to go to next line, instead of just extending the width to accommodate? – Asher Jul 29 '14 at 16:13
  • @Asher [This](http://stackoverflow.com/questions/9395163/issue-drawing-aligned-text-using-textflow-and-flowpage) might help. – Baz Jul 30 '14 at 09:33