0

我在 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);

    }


}
4

1 回答 1

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;
        }
    }
}
于 2013-02-12T09:09:09.763 回答