You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@myfaces.apache.org by Ma...@sanofi-aventis.com on 2008/01/09 17:00:25 UTC

How to reset tree state

Hi,

 

I'm relatively new to JSF. The project I'm working on required use of
the tree2 component. I've had to do some customizations to this and I've
also incorporated AJAX into it using Ajax4JSF. I followed this,
http://www.jroller.com/plainoldweblog/entry/use_tomahawk_tree2_and_ajax4
jsf, not exactly the same but I adapted his example. I've also rewritten
the renderer based on the existing one. I got rid of all those tables.
It would be really nice to see an implementation that doesn't rely on
tables. Mine doesn't, but it's incomplete ... it works for what we need
it for but it's far from a replacement to the one that comes with
Tomahawk. If I have time (unlikely) maybe I'll write a complete one. So
onto the problem:

 

I've extended the TreeModelBase and TreeNodeBase to meet our needs here.
He have some backend data that will be loaded into the tree. We don't
want a round trip to the server every time, and loading everything up
front would be too slow (sound familiar?), hence AJAX. I was able to get
that part working quite well. Only problem is that it requires use of a
session managed bean instead of a request bean. I can live with this.
Now we have a drop down list of sites, or I guess you could call them
topics, not important, but when a user changes topics the tree needs to
be reloaded with a different set of data. Here's where that session bean
became a problem. Since it was a session bean, it kept the same data
from before. I came up with a fix for this, I was able to use an
application context variable to detect when a user changes sites, and
then just reinitialize the data model when this happens. That part works
fine. Now for my actual problem... the data model initializes fine, but
the tree state does not reset itself. So the end result is nodes already
expanded in the new tree. They're not fully expanded since the child
nodes aren't actually loaded, but they appear expanded, and a user would
have to click twice to actually expand them. First click collapses the
node, then second click re-expands it and actually loads the data.
Stepping through the renderer code I found that the call:

 

TreeState state = tree.getDataModel().getTreeState();

 

Is returning a previously stored TreeState. Here's my code:

 

protected void buildTree(List list, String type) {

    TreeNode rootNode ;

    if (model == null) {

        rootNode = new MenuTreeNode(Constants.TREE_ROOT_NODE,

                null, "root", false);

 

        model = new MenuTreeModel(rootNode);

        model.setTreeState(new TreeStateBase()); // this isn't
necessary, was just using for testing

    } else {

        rootNode = model.getRootNode();

    }

    this.addChildNodes(rootNode, list, type);    

}

 

If I use the debugger and stop in the above method, I can see it gets
executed before the renderer, and comparing the object ids (visually via
the debugger) I can see they're different. Basically to reset the tree I
just set it null which forces a call to this method here. 

 

A bit of further browser of the tree2 source, I found that the above
mentioned call... tree.getDataModel().getTreeState() is doing something
like this...

 

if (_cachedModel != null)

{

    return _cachedModel;

}

 

What I don't understand is why the tree state is cached yet the model is
not, since it loads the new model properly. So... how can get it to use
a clean TreeState?

 

Thanks for any help on this.