gwtcellbrowser

Add leaf as well as non-leaf nodes at the same level in a GWT CellBrowser or Cell Tree


I want to add non-leaf as well as leaf nodes at a same level in a GWT CellBrowser/ Cell Tree. Can i do it? if yes, how? Because while returning DefaultNodeInfo I am not getting an option to return both kind of ListDataProviders.


Solution

  • A simple solution would be to create a superclass or interface Node, which your NonLeafNode and your LeafNode class extend / implement:

    public class NonLeafNode extends Node{
    }
    

    or

    public class NonLeafNode implements Node{
    }
    

    Then you can give the CellBrowser or CellTree a single ListDataProvider which provides both types of node. In the underlying model, e.g. a TreeViewModel, you need to adjust the isLeaf(Object o) and the getNodeInfo(T value) functions as follows:

    public boolean isLeaf(Object value) {
        if (value instanceof NonLeafNode) return true;
        if (value instanceof LeafNode) return false;
        return false;
    }
    
    public <T> getNodeInfo(T value){
        if (value instanceof NonLeafNode) 
             // return node info for non-leaf-node
            ;
        else if (value instanceof LeafNode)
             // return node info for leaf node
            ;
        return null;
    }