我不知道如何在TreeViewer(JFace)项目上添加鼠标右键单击侦听器?
这是我的源码我创建了一个从viewpart扩展的类树视图:
/*
* Creates the tree.
*
* @return the tree object and create part control
*/
private TreeObject CreateTree() {
TreeParent root = new TreeParent("");
DemoTreeModel ctModel = new DemoTreeModel();
List<String> listType = ctModel.getType();
for (String type : listType) {
TreeParent parentNode1 = new TreeParent(type);
parentNode1.setLevel(1);
List<String> listMachine = ctModel.getName(type);
if (listMachine != null) {
for (String machine : listMachine) {
TreeParent childNode1 = new TreeParent(machine);
childNode1.setLevel(2);
parentNode1.addChild(childNode1);
List<String> listVersion = ctModel.getVersion(machine);
if (listVersion != null) {
for (String ver : listVersion) {
TreeObject version = new TreeObject(ver);
version.setLevel(3);
version.setData(ver);
childNode1.addChild(version);
}
}
}
}
root.addChild(parentNode1);
}
return root;
}
答案 0 :(得分:0)
您可以使用以下内容:
MenuManager menuMgr = new MenuManager();
menuMgr.setRemoveAllWhenShown(true);
menuMgr.addMenuListener(new IMenuListener() {
@Override
public void menuAboutToShow(IMenuManager menuManager) {
... add items to menu manager
}
});
Control tree = treeViewer.getControl();
Menu menu = menuMgr.createContextMenu(tree);
tree.setMenu(menu);
这是使用IMenuListener
,每次显示菜单时都可以添加不同的菜单项,因此您可以适应所选的内容。