7 Replies Latest reply on Feb 20, 2009 1:18 AM by Ingo Jobling

    Problem to keep rich:tree state

    Fábio Valente Newbie

      I'm using the Richfaces tree component, but it's collapsing when I go to a different page.
      I have this tree in several pages (I'm using the templating mechanism) and when I click on a link from the tree (I use <s:link> in every treeNode).


      I searched for a solution, but I couldn't find one.


      Here is the code of my tree:


      <h:form>
          <rich:tree switchType="ajax">
              <rich:recursiveTreeNodesAdaptor roots="#{hierarchicalClassifier.rootElements}" 
                      var="hierarchicalElement" nodes="#{hierarchicalElement.childrenElements}">
                  <rich:treeNode>
                      <s:link value="#{hierarchicalElement.title}" view="/viewElement.xhtml" />
                  </rich:treeNode>
              </rich:recursiveTreeNodesAdaptor>
          </rich:tree>
      </h:form>



      I saw that the tree component has attributes called stateVar and stateAdvisor. The problem is that I don't how to use them and I didn't find examples of how to use them.


      I tried the solution given by Roberson Ribeiro that can be found here but I couldn't even startup JBoss. I get java.lang.NoClassDefFoundError: org/ajax4jsf/component/UIDataAdaptor.


      Can anyone help me?

        • 1. Re: Problem to keep rich:tree state
          Bernard Labno Master

          Info on how to use stateAdvisor is in richfaces tlddoc.



          adviseNodeOpened : javax.el.MethodExpression
          (signature must match void adviseNodeOpened(org.richfaces.component.UITree))      MethodBinding pointing at a method accepting an org.richfaces.component.UITree with return of java.lang.Boolean type. If returned value is: java.lang.Boolean. TRUE, a particular treeNode is expanded; java.lang.Boolean.FALSE, a particular treeNode is collapsed; null, a particular treeNode saves the current state

          As to NoClassDefFoundError, you do not have richfaces-ui.jar in your classpath. Put all 3 richfaces jars in EAR/lib or if you use WAR only, put it in WEB-INF/lib.


          And one more thing. When you use your tree between conversations make your advisor session scoped.

          • 2. Re: Problem to keep rich:tree state
            Vladimir Kovalyuk Apprentice

            For what reason we should include richfaces-impl.jar into ear/lib if there is richfaces-api?

            • 3. Re: Problem to keep rich:tree state
              Fábio Valente Newbie

              I solved the problem.


              I used the attribute componentState from the tree component.


              I have to show several trees at the same time and keep the state of all of them.


              So I created a controller with scope SESSION and added a Map<HierarchicalClassifier, TreeState> with getter and setter (both passing a HierarchicalClassifier argument) for the map.


              Concerning the problem of NoClassDefFoundError, I added the richfaces-ui.jar in my classpath.


              []'s

              • 4. Re: Problem to keep rich:tree state
                safa said Newbie

                hi, fabio,


                I'm having the same problem,


                Can you, plz, give more details on how you resolve it??


                I really need your help


                Thx

                • 5. Re: Problem to keep rich:tree state
                  Fábio Valente Newbie

                  TreeStateController.java


                  @Name("treeStateController")
                  @Scope(ScopeType.SESSION)
                  @AutoCreate
                  public class TreeStateController extends Controller
                  {
                      private Map<Integer, TreeState> treeStateMap = new HashMap<Integer, TreeState>();
                      
                      public TreeState getTreeState(Integer key)
                      {
                          TreeState treeState = treeStateMap.get(key); 
                          if (treeState == null)
                          {
                              treeState = new TreeState();
                              treeStateMap.put(key, treeState);
                          }
                          
                          return treeState;
                      }
                      
                      public void setTreeState(Integer key, TreeState treeState)
                      {
                          treeStateMap.put(key, treeState);
                      }
                  }



                  inc_tree.xhtml


                  <!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
                                        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
                  <ui:composition xmlns="http://www.w3.org/1999/xhtml"
                                  xmlns:s="http://jboss.com/products/seam/taglib"
                                  xmlns:ui="http://java.sun.com/jsf/facelets"
                                  xmlns:f="http://java.sun.com/jsf/core"
                                  xmlns:h="http://java.sun.com/jsf/html"
                                  xmlns:rich="http://richfaces.org/rich"
                                  xmlns:a4j="http://richfaces.org/a4j">
                  
                  <rich:tree switchType="client" 
                          componentState="#treeStateController.getTreeState(tree.id)}">
                  
                      <rich:recursiveTreeNodesAdaptor roots="#{tree.activeRootElements}" 
                              var="element" nodes="#{element.activeChildrenElements}">
                  
                          <rich:treeNode>
                  
                          <s:link view="/viewElement.xhtml">
                              <f:param value="#{element.id}" name="elementId"/>
                              <h:outputText value="#{element.title}"/>
                          </s:link>
                  
                          </rich:treeNode>
                  
                      </rich:recursiveTreeNodesAdaptor>
                  
                      <a4j:support event="oncollapse" ajaxSingle="true" />
                      <a4j:support event="onexpand" ajaxSingle="true" />
                  
                  </rich:tree>
                  
                  </ui:composition>
                  


                  • 6. Re: Problem to keep rich:tree state
                    Axel Osorio Newbie

                    Fabio . Thank you very much for your example, I've working on an app that requires the rich tree component state and your solution was very useful to me. Only want to say thank you.

                    • 7. Re: Problem to keep rich:tree state
                      Ingo Jobling Master

                      Fabio, Please accept my thanks also.  Your example is solid gold for me!