Hi all. I will try first to explain my use case as that might shad some
light on the problem that I encountered here.

I have a tree that always has 4 main nodes, 2 nodes under each main one and
the then rest of the nodes are created from the db. So something like

category1
    subcategory11
    subcategory12

category2
    subcategory21
    subcategory22

category3
    subcategory31
    subcategory32

category4
    subcategory41
    subcategory42

So since I didnt wanted to load the complex data structure during the page
load I was quite happy to read that tree has ajax events called on node
expansion.

So all was cool, I wrote my node structure to and it looked like everything
was work however what I noticed is that the *findById * method in
DefaultTreeModel was trying to access each node in the tree to find the
correct one.

Example

User clicks on category2 the DefaultTreeModel will load all nodes from
category1.

Looking into the code of the before mentioned class I could see the problem

private TreeNode<T> findById(String id)
    {
        TreeNode<T> result = cache.get(id);

        if (result != null)
            return result;

        LinkedList<TreeNode<T>> queue = new LinkedList<TreeNode<T>>(roots);

        while (!queue.isEmpty())
        {
            TreeNode<T> node = queue.removeFirst();

            String nodeId = node.getId();

            cache.put(nodeId, node);

            if (nodeId.equals(id))
                return node;

            if (!node.isLeaf() && node.getHasChildren())
            {
                for (TreeNode<T> child : node.getChildren())
                {
                    queue.addFirst(child);
                }
            }
        }

        return null;
    }

There is no call to the encoder method

         toValue(String clientValue)

In this method I have "smart" logic that wont iterate through all of the
nodes but only through the loaded ones.


The implementation is quite simple

private TreeNode<T> findById(String id) {
        TreeNode<T> result = cache.get(id);

        if (result != null) {
            return result;
        }

        LinkedList<TreeNode<T>> queue = new LinkedList<TreeNode<T>>(roots);

        T toValue = encoder.toValue(id);
        if (toValue != null) {
            DefaultTreeNode node = new
FlowTreeModel.DefaultTreeNode(toValue);
            cache.put(node.getId(), node);
            return node;
        }

        return null;
}

So my question is why does DefaultTreeModel doesnt use encoder to get the
node but it instead it tries to iterate through all of the nodes?

Cheers
-- 
Sincerely
*Boris Horvat*

Reply via email to