10

I've been playing with the Zest GraphViewer for over a week now trying to discover what it can do for my application, but I haven't been able to get it's behaviour in-line with my requirements thus far.

I'm hoping that someone can point me to the resources I need, becasue I just can't find all that much of use with Google, or can tell me if what I want is possible.

Version

I've got Zest core 1.3.0 and Zest layout 1.1.0 in my dependencies for the RCP project. This came from the download site I took from the Zest site.

Requirements

  • Single Node/Edge selection.
  • De-selection of node/edge when white-space is selected (which may be a bug?)
  • Right click functionality to change when over a node. (detect when mouse is over a node)

The right click functionality could come from the single selection since I could have the popup anywhere but base it on the current selected node, but I'd rather not do that.

Without being able to do this, due to the nature or our application and users, I may also have a requirement to find another RCP/SWT based Graph drawing package that does have this functionality.

Any help on any of these issues would be greatly appreciated.

Glen x

Link19
  • 586
  • 1
  • 18
  • 47

1 Answers1

7

Based on the Zest tutorial by Vogella, I came up with this:

public static void main(String[] args) throws FontFormatException, IOException
{
    Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());

    final Graph graph = new Graph(shell, SWT.NONE);
    GraphNode node1 = new GraphNode(graph, SWT.NONE, "Jim");
    GraphNode node2 = new GraphNode(graph, SWT.NONE, "Jack");
    GraphNode node3 = new GraphNode(graph, SWT.NONE, "Joe");
    GraphNode node4 = new GraphNode(graph, SWT.NONE, "Bill");

    /* Context menu */
    graph.addMenuDetectListener(new MenuDetectListener()
    {
        @Override
        public void menuDetected(MenuDetectEvent e)
        {
            Point point = graph.toControl(e.x, e.y);
            IFigure fig = graph.getFigureAt(point.x, point.y);

            if (fig != null)
            {
                Menu menu = new Menu(shell, SWT.POP_UP);
                MenuItem exit = new MenuItem(menu, SWT.NONE);
                exit.setText("Hello! This is " + ((GraphLabel) fig).getText());
                menu.setVisible(true);
            }
            else
            {
                Menu menu = new Menu(shell, SWT.POP_UP);
                MenuItem exit = new MenuItem(menu, SWT.NONE);
                exit.setText("Nothing here...");
                menu.setVisible(true);
            }
        }
    });
    /* Lets have a directed connection */
    new GraphConnection(graph, ZestStyles.CONNECTIONS_DIRECTED, node1, node2);
    /* Lets have a dotted graph connection */
    new GraphConnection(graph, ZestStyles.CONNECTIONS_DOT, node2, node3);
    /* Standard connection */
    new GraphConnection(graph, SWT.NONE, node3, node1);
    /* Change line color and line width */
    GraphConnection graphConnection = new GraphConnection(graph, SWT.NONE, node1, node4);
    graphConnection.changeLineColor(shell.getDisplay().getSystemColor(SWT.COLOR_GREEN));
    /* Also set a text */
    graphConnection.setText("This is a text");
    graphConnection.setHighlightColor(shell.getDisplay().getSystemColor(SWT.COLOR_RED));
    graphConnection.setLineWidth(3);

    graph.setLayoutAlgorithm(new SpringLayoutAlgorithm(LayoutStyles.NO_LAYOUT_NODE_RESIZING), true);
    graph.addSelectionListener(new SelectionAdapter()
    {
        @Override
        public void widgetSelected(SelectionEvent e)
        {
            System.out.println(e.item);
            /* Make sure that only the newest item is selected */
            graph.setSelection(new GraphItem[]{(GraphItem)e.item});
        }

    });

    shell.pack();
    shell.open();
    shell.setSize(400, 300);

    while (!shell.isDisposed())
    {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

It supports single node/edge selection, de-selection and right-click functionality as requested.

Looks like this:

enter image description here


If you use the GraphViewer example of the tutorial and add this to the View code, it still works fine:

final Graph graph = viewer.getGraphControl();
graph.addSelectionListener(new SelectionAdapter()
{
    @Override
    public void widgetSelected(SelectionEvent e)
    {
        System.out.println(e.item);
        graph.setSelection(new GraphItem[]{(GraphItem)e.item});
    }

});
Baz
  • 36,440
  • 11
  • 68
  • 94
  • you still can do multi select with MOD keys ( shift, cntrl). I see a bug with Graph that SWT.SINGLE / SWT.MULTI are not supported. – sambi reddy Feb 14 '14 at 16:33
  • @sambireddy Ok, updated the code. It's now not possible to select more than one item. – Baz Feb 14 '14 at 17:02
  • This wont work if GraphViewer is used. It has to be handled at Graph underlying code. If you keep on setting selection on latest selected item, it will fire selection event multiple times. – sambi reddy Feb 18 '14 at 20:01
  • @sambireddy Edited my answer. It still works with `GraphViewer`. – Baz Feb 18 '14 at 20:39
  • it works only if you create Graph first and then set Graph as control in GraphViewer. it depends on order of selection listeners ( listener registered by Viewer class) – sambi reddy Feb 18 '14 at 21:24
  • @sambireddy So in the end you found a configuration that works for you? – Baz Feb 18 '14 at 21:59
  • I was expecting the Graph to handle single/multiple selection like how SWT Table/Tree does. Yes, as i always use Viewer classes, i was expecting proper solution that works with Viewer. – sambi reddy Feb 18 '14 at 22:17
  • @sambireddy So is that a 'yes'? – Baz Feb 18 '14 at 22:22
  • 1
    There is just one Single issue. If your Graph is bigger and you have to scroll. Then graph.getFigureAt(point.x, point.y); does not translate properly. So I used for my Problem graph.getViewport().findFigureAt(point.x, point.y); and explicit instanceOf Check to be GraphLabel, PolylineConnection and FreeformViewport (for No selection). – daniel Oct 23 '14 at 10:28