Jface TreeViewer添加右键单击菜单,具体取决于单击的节点 [英] Jface TreeViewer add right click menu, depending on clicked node

查看:153
本文介绍了Jface TreeViewer添加右键单击菜单,具体取决于单击的节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于如何操作有好线程根据选择的项目正确地将右键单击菜单连接到Jface TreeViewer.

There is a good thread on how to correctly hook up a right-click menu to a Jface TreeViewer depending on the selected item.

我想显示右键菜单,具体取决于:右键单击是在节点上还是在空白空间"中.问题在于,如果您单击空白区域,TreeViewer不会自动清除选择.有什么干净的方法可以实现这一目标吗?

I would like to show the right click menu depending on: if the right-click was on a node or into "empty space". The problem is that TreeViewer does not automatically clear the selection if you click into empty space. Is there any clean way how to achieve this?

我当前的方法是使用以下mouseDown方法简单地将MouseListener挂接到树上:

My current approach would be to simply hook up a MouseListener to the tree with the following mouseDown method:

@Override
public void mouseDown(MouseEvent e) {
    TreeItem item = treeViewer.getTree().getItem(new Point(e.x, e.y));
    if (item == null) {
        treeViewer.getTree().deselectAll();
    }
}

这似乎效果很好.您如何看待?

This seems to work quite well. What do you think of this?

推荐答案

好,我发现了一个肮脏的解决方法.因此,如果您真的想这样做,可以采用以下解决方案:

Ok, I found a dirty workaround. So if you really want to do it, here is a possible solution:

final Tree tree = viewer.getTree();

final Menu menu = new Menu(tree);
tree.setMenu(menu);
menu.addMenuListener(new MenuAdapter()
{
    @Override
    public void menuShown(MenuEvent e)
    {
        Point point = tree.toControl(Display.getDefault().getCursorLocation());
        boolean found = false;
        for (TreeItem item : tree.getItems())
        {
            for (int i = 0; i < tree.getColumnCount(); i++)
                if (item.getBounds(i).contains(point))
                    found = true;
        }

        System.out.println(found);
    }
});

这篇关于Jface TreeViewer添加右键单击菜单,具体取决于单击的节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆