为Zest Graph添加单选和上下文敏感的右键点击功能

10

我已经使用Zest GraphViewer玩了一个星期,试图发现它对我的应用程序有什么作用,但到目前为止我还没有能够使其行为符合我的要求。

我希望有人可以指引我需要的资源,因为我在Google上找不到太多有用的东西,或者告诉我我想要的是否可能实现。

版本

我在RCP项目的依赖项中使用了Zest core 1.3.0和Zest layout 1.1.0。这是我从Zest网站下载的。

需求

  • 单个节点/边缘选择。
  • 当选择空白区域时取消选择节点/边缘(这可能是一个错误?)
  • 右键单击功能会在鼠标悬停在节点上时更改。(检测鼠标是否悬停在节点上)

由于单选功能可以让弹出菜单出现在任何地方,但是基于当前选定的节点,因此右键单击功能可以来自单选功能,但我宁愿不这样做。

如果不能做到这一点,由于我们应用程序和用户的性质,我可能还需要找另一个具有此功能的基于RCP/SWT的图形绘制包。

对于任何这些问题的帮助将不胜感激。

Glen x

1个回答

7

根据Vogella的Zest教程,我得出了以下内容:

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

它支持单个节点/边缘的选择、取消选择和右键功能,正如您所要求的。

看起来是这样的:

enter image description here


如果您在教程的GraphViewer示例中使用此代码添加到View代码中,它仍然可以正常工作:

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

});

如果使用GraphViewer,这将无法工作。必须在Graph底层代码中处理它。如果您继续在最新选择的项目上设置选择,则会多次触发选择事件。 - sambi reddy
@sambireddy,最终你找到了适合自己的配置吗? - Baz
我原本期望Graph能够像SWT Table/Tree一样处理单选/多选。是的,由于我经常使用Viewer类,我期望有一个适用于Viewer的合适解决方案。 - sambi reddy
@sambireddy 那是一个“是”吗? - Baz
1
只有一个小问题。如果你的图形更大,需要滚动,那么 graph.getFigureAt(point.x, point.y); 就无法正确翻译。所以我使用了 graph.getViewport().findFigureAt(point.x, point.y); 和显式的 instanceOf 检查来解决我的问题,其中包括 GraphLabel、PolylineConnection 和 FreeformViewport(用于没有选择的情况)。 - daniel
显示剩余4条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接