我正在制作一个使用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级
谢谢。
答案 0 :(得分:1)
无需比较TotalDuration[k]+= ResReqRAM[j];
个文字,只需通过以下方式检查项目的级别:检查它是否有父母。
TreeItem
这将显示级别0和1的不同菜单,而不显示级别2的菜单。
答案 1 :(得分:0)
我已通过以下方式解决了我的问题。 如果您确实看到了我的代码,则有以下几行:
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
}
}
我得到了我的树数据结构的第一级,然后我验证了我的SWT树的根名称是否等于树数据结构的根。
使用相同的方法,这将导致SWT Tree的菜单监听器结束:
variantTree.addMenuDetectListener(new MenuDetectListener(){
@Override
public void menuDetected(MenuDetectEvent event) {
// TODO Auto-generated method stub
for(Node element : TestControl.getTree().getChildren()){
if(variantTree.getSelection()[0].getText().equals(element.getName())){
event.doit = true;
break;//need this, because it will propagate only to the last item, or it will have a strange behaviour
}else{
event.doit = false;
}
}
}
});
其中element
是来自我的树数据结构的节点。
希望它可以帮助任何有同样问题的人。