2013-02-11 64 views
0

我在java gwt中實現了一個CellTree結構,當單擊單元格中的小箭頭時,它會打開它的子​​元素。當單擊單元格本身而不是明確指向箭頭時,是否可以打開子樹?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

你必須使用遞歸找到所選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; 
     } 
    } 
}