Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding Single Selection and Context Sensitive Right Clicking to Zest Graph

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

like image 585
Link19 Avatar asked Feb 11 '14 09:02

Link19


1 Answers

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

});
like image 180
Baz Avatar answered Sep 25 '22 13:09

Baz