Saturday 7 July 2007

Follow up: Programmatic ADF Faces Tree holding state across requests

As a further follow up to my recent posts "Back to programming: Programmatic ADF Faces Tree component" and "Follow-up: Programmatic ADF Faces Tree in JDev 11g", this posts shows how to maintain the tree's state across multiple page requests or pages. The basic problem with the previous code was if the page was refreshed, or the tree component existed in different pages, the tree's current state in terms of what has been expanded and collapsed was lost.

To solve this issue, for the tree component in every page where you wish to include and maintain the tree's state, add a binding attribute like follows:

<af:tree var="node" value="#{treeModel.model}" binding="#{treeState.tree}">


In your faces-config.xml file add a new session level bean:

<managed-bean>
  <managed-bean-name>treeState</managed-bean-name>

  <managed-bean-class>TreeState</managed-bean-class>
  <managed-bean-scope>session</managed-bean-scope>
</managed-bean>


And finally define the bean class as:

import oracle.adf.view.faces.component.core.data.CoreTree;
import oracle.adf.view.faces.model.PathSet;

public class TreeState {
   private CoreTree tree;
   private PathSet treePathSet;

   public TreeState() {
   }

   public void setTree(CoreTree tree) {
     treePathSet = tree.getTreeState();
     this.tree = tree;
   }

   public CoreTree getTree() {
     if (treePathSet != null)
     tree.setTreeState(treePathSet);
   }
}


You'll note how the bean across the user's session saves and restores the tree's state each time the web page requests the CoreTree component.

The credit for the seeds of the solution for this post must goto Arjuna Wijeyekoon and his OTN JDev Forum post from 2004. As can be seen the OTN Forums are very valuable for finding solutions.

10 comments:

Unknown said...

Useful as this looks, I need to understand how a basic tree could be made and maintained. I haven't been able to figure out how to get the data (model's view) onto the tree.

Can anyone tell me where that information is? Preferably an 11g version but I'll take anything simple enough that I can understand it.

Chris Muir said...

Hi Thom

Can you elaborate please? The usual way to get a tree to bind to the model is by dragging and dropping a View Object from the Data Control Palette and selecting the Tree option. Is it the Tree Dialog you're having trouble with or are you referring to creating a tree in a different fashion?

Regards,

CM.

Unknown said...

hi Chris
i have implemented the code for maintaining the tree state across requests. I'm using Jdeveloper 11g and the problem i'm having is that i cannot use the following import statement : import oracle.adf.view.faces.rich.model.PathSet

can u please help me on how i can solve this problem...

Unknown said...

Hi Chris..

I'm using Adf faces tree. I'v implemented the code you have provided on the blog and it works fine..

But the problem I'm having is that i want to maintain the tree state across each request..I'm currently working with jdeveloper 11g and wen I use the following: "import oracle.adf.view.faces.rich.model.PathSet;", i get error "import oracle.adf.view.faces.rich.model.PathSet; not used or not found"

Can u help me out please???

Chris Muir said...

Hi chaj

Are you using the Trinidad component set, or the ADF Faces RC component set?

For the Trinidad component set, I found a typo in my original post which I've fixed.

It now correctly says you need to replace the following JDev 10.1.2 code with the following equivalent in JDev 11g:

import oracle.adf.view.faces.model.ChildPropertyTreeModel;
import oracle.adf.view.faces.model.TreeModel;

.... with ....

import org.apache.myfaces.trinidad.model.ChildPropertyTreeModel;
import org.apache.myfaces.trinidad.model.TreeModel;

If instead you're looking at the new ADF Faces RC component set, I can't at this time find an equivalent to the PathSet class. At this time I'll suggest you'll need to post on the JDev OTN Forums to see if this was deliberate and there is some other mechanism rather than PathSet. If you get a response I'd appreciate it if you could add a comment on this page to your OTN post to assist others in your predicament.

Good luck!

Regards,

CM.

Senthil said...

Any update on PAthSet class, please?...

Chris Muir said...

Can you please be more explicit Senthil? What about the PathSet class are you looking for? The ADF Faces RC equivalent? Or are you having some other sort of trouble?

CM.

Senthil said...

Yes i am looking at the ADF Faces RC equivalent for the same. In the other thread i came to know that it is possible through RowKeySet class. I am looking into that.

scuthxf said...

Seems the PathSet has droped in ADF11g.
Now we could use to RowKeySet keep track of which nodes are expanded.
For example:
In backing bean:
public RowKeySet getDisclosedRowKeys()
{
RowKeySet treeState = new RowKeySetTreeImpl();
// RowKeySet requires access to the TreeModel for currency.
TreeModel model = getTreeModel();
treeState.setCollectionModel(model);

return treeState;
}

treeState.setCollectionModel(model);

In Tree Component:

Chris Muir said...

Thanks for the update, that'll help other readers out.

CM>