【发布时间】:2016-08-30 06:44:51
【问题描述】:
我正在制作一个使用 java SWT 的 Tree 结构的程序。这棵树是建立在从外部文件和用户输入中读取的数据之上的。 在这棵树上,我还附加了一个右键菜单。
主要问题是这棵树的菜单跨越了他的所有项目,我希望它只在某个级别上显示。
我还想为不同的树级别设置不同的菜单或菜单选项。
所有树的创建都在一个函数中:
public void createTree(final Composite container){
try{
for(Object element : container.getChildren()){
if(element instanceof Tree){
((Tree) element).dispose();
}
}
// function to verify if the tree is still in my container composite, and if so, I'll dispose the tree, because I am going to construct a new one
final Tree variantTree = new Tree(container, SWT.CHECK | SWT.V_SCROLL | SWT.H_SCROLL);//creating new tree object
variantTree.setBounds(10, 65, 400, 400);//dimensions
//variantTree.setData("variant"); -- wanted to abuse this method to make a difference between the tree's level,
//but this attribute is shared across all the tree, and I cannot make a distinction between my levels
if(..){
//here I am using the data saved in a tree data structure to populate my SWT Tree
}
variantTree.addListener(SWT.Selection, new Listener(){
//this is a listener used for the tree's checkboxes, using them to select different items from my tree
});
final Menu treeMenu = new Menu(variantTree);
variantTree.setMenu(treeMenu);//adding the right click menu
treeMenu.addMenuListener(new MenuAdapter(){
//the menu's items are declared here: the buttons that I want to have and the their listeners
//i am disposing the menu's items if there are any
MenuItem newItem = new MenuItem(treeMenu, SWT.NONE);
newItem.setText("new button... ");
newItem.addListener(SWT.Selection, new Listener(){
@Override
public void handleEvent(Event event) {
String item = variantTree.getSelection()[0].getText();//can I use this to make different menus for different tree levels?
for(Node element : TestControl.getTree().getChildren()){//getting the tree data structure that I have saved in a static way via a class
if(element.getName().equals(item)){
//opening a file dialog here and doing some operations
}
}
});
});
//this is where I want to make the distinction between the levels of the tree, such as that I will have this menu show up only for the root of my tree, or the first level of the tree, since I will have multiple trees
variantTree.addMenuDetectListener(new MenuDetectListener(){
@Override
public void menuDetected(MenuDetectEvent event) {
// TODO Auto-generated method stub
if(/*condition*/){ //I messed around with the tree's methods and attributes, nothing so far has come in handy to help me make a distinction between my tree's levels
event.doit = true;
//System.out.println(variantTree.getSelectionCount()); --I don't know if this can help me either, I tried to see if this will return differently for every level of the tree, but the return values is always 0
//find some kind of handle
}else{
event.doit = false;
}
}
});
另外,我想为树的第二级显示另一个菜单,我可以使用String item = variantTree.getSelection()[0].getText();,以某种方式修改以满足我的需要吗?比方说,String item = variantTree.getSelection()[1].getText(); 用于第二级?
我的 SWT 树的每个根的最大深度为 3,它看起来像这样:
root1 - 只想要这个项目的菜单
|---孩子 - 1级
|------孩子-2级
root2 - 只想要这个项目的菜单
|---孩子 - 1级
|-------孩子 - 2级
谢谢。
【问题讨论】: