Onkar
Onkar

Reputation: 662

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.

Upvotes: 0

Views: 126

Answers (2)

Onkar
Onkar

Reputation: 662

My way out!

private static class Folder
        {
            private final String        name;

            private final List<Folder>  folder  = new ArrayList<Folder>();

            public Folder(String name)
            {
                this.name = name;
            }

            public String getName()
            {
                return name;
            }

            public void addFolder(Folder p)
            {
                this.folder.add(p);
            }

            public List<Folder> getFolders()
            {
                return folder;
            }
        }

then in the CustomTreeModel that we create override the isLeaf as follows

        public boolean isLeaf(Object value)
        {
            if (value instanceof String || (value instanceof Folder && ((Folder) value).getFolders().isEmpty()))
            {
                return true;
            }
            return false;
        }

Upvotes: 0

subrunner
subrunner

Reputation: 404

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;
}

Upvotes: 1

Related Questions