java gwt celltree

时间:2013-02-11 13:05:40

标签: java gwt tree

我在java gwt中实现了一个CellTree结构,当单击单元格中包含的小箭头时,它会打开它的子​​项。是否可以在单击单元格本身时打开子树而不是明确地在箭头上打开?

public class SomeCellTree extends Composite {


    private SelectionModel selectionModel;
    private CellTree cellTree;



    private class SomeTreeViewModel implements TreeViewModel {
         @Override
         public <T> NodeInfo<?> getNodeInfo(T value) {
             ...
         }

         @Override
         public boolean isLeaf(Object value) {
             ...
         }  
    }



    public SomeCellTree(...) {

       this.selectionModel = new SingleSelectionModel();
       this.selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
           @Override
           public void onSelectionChange(SelectionChangeEvent event) {
                         ...
           }
       });

       cellTree = new CellTree(new SomeTreeViewModel(),null,RESOURCES);
       initWidget(cellTree);

    }


}

1 个答案:

答案 0 :(得分:1)

您必须使用递归来查找所选DTO的节点,然后您必须调用setChildOpen来打开子树。像这样:

this.selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
    @Override
    public void onSelectionChange(SelectionChangeEvent event) {
         T selectedObj = selectionModel.getSelectedObject();
         TreeNode rootNode = cellTree.getRootTreeNode();
         openTree(rootNode,T);
    }
});


private void openTree(rootNode node,T object) {
    for (int i = 0; i < node.getChildCount(); i++) {
        if (!node.isChildLeaf(i)) {
            find(node.setChildOpen(i, true));
            if (node.getValue().equals(T)) 
                return;
        }
    }
}