3 Replies Latest reply on Jun 3, 2008 3:56 PM by Yurii Kartsev

    TreeStateAdvisor doesn't work on navigation-rule

    Yurii Kartsev Apprentice

      Again, good afternoon, dear RichFaces gurus! ;)

      I've faced another problem.

      I have a tree(recursive). My treeMan managed bean scope is "request". And the problem started appearing just when I switched this scope from "session" to "request". The problem is that when I open my view which contains the tree bu myself(by clicking a link or pressing F5) - the tree nodes(all of them) are opened(expanded). But when this view is opened through a navigation-rule(navigation-case), the tree is all collapsed. Why does it happen and how to make it be always fully expanded?

      My tree code is:

      <rich:tree style="width:250px" id="structureTree" styleClass="tree" switchType="ajax"
       ajaxSubmitSelection="true" showConnectingLines="true"
       changeExpandListener="#{treeMan.onDepartmentTreeExpandStaff}"
       nodeSelectListener="#{treeMan.onDepartmentTreeSelectStaff}"
       stateAdvisor="#{departmentTreeStateAdvisor}" reRender="staffDataTable_Form">
       <rich:recursiveTreeNodesAdaptor roots="#{treeMan.departmentsTreeRoots}" var="item" nodes="#{item.nodes}" />
      </rich:tree>


      my departmentTreeStateAdvisor is:
      package hrd.tree;
      import org.richfaces.component.UITree;
      import org.richfaces.component.state.TreeStateAdvisor;
      import org.richfaces.model.TreeRowKey;
      
      public class DepartmentTreeStateAdvisor implements TreeStateAdvisor
      { // while the tree is painting, these two functions are invoked on every node
       // whether open current processing node, or not (TRUE - to open, null - not to open)
       public Boolean adviseNodeOpened(UITree tree)
       {
       if(!PostbackPhaseListener.isPostback())
       {
       Object key = tree.getRowKey();
       TreeRowKey treeRowKey = (TreeRowKey)key;
       if(treeRowKey == null || treeRowKey.depth() <= 2)
       {
       return Boolean.TRUE;
       }
       }
       return null;
       }
      
       // whether select current processing node, or not (TRUE - to select, null - not to select)
       public Boolean adviseNodeSelected(UITree tree)
       {
       return null;
       }
      }


      Thank you very much in advance!

        • 1. Re: TreeStateAdvisor doesn't work on navigation-rule
          Bernard Labno Master

          Maybe because of "request" scope. By the way, show treeMan code.

          • 2. Re: TreeStateAdvisor doesn't work on navigation-rule
            Yurii Kartsev Apprentice

            Here it is, if you need to:

            public class TreeManager extends ManagedBean
            {
             // ############################################################################################
             // ############################################################################################
             private static final long serialVersionUID = 1L;
            
             private DepartmentTreeNode[] departmentsTreeRoots;
            
             // ############################################################################################
             // ############################################################################################
             public TreeManager()
             {
             super();
             }
            
             // ############################################################################################
             public synchronized DepartmentTreeNode[] getDepartmentsTreeRoots()
             {
             if (departmentsTreeRoots == null)
             {
             ActionsManager am = (ActionsManager)TreeManager.getBean(WebConst.MAIN_MBEAN_NAME);
             departmentsTreeRoots = new DepartmentTreeNode(am.getAllTreeListMan().getDepartmentTypeLevel0()).getNodes();
             }
            
             return departmentsTreeRoots;
             }
            
             // ############################################################################################
             public synchronized void setDepartmentsTreeRoots(DepartmentTreeNode[] depTreeRoots)
             {
             this.departmentsTreeRoots = depTreeRoots;
             }
            
             // ############################################################################################
             public void onDepartmentTreeExpandPretenders(NodeExpandedEvent event)
             {
             UITree tree = (UITree) event.getComponent();
            
             System.out.println("################# >>>>> Tree node expanded");
            
             DepartmentTreeNode selectedNode = (DepartmentTreeNode)tree.getRowData();
             AllTree dep = (AllTree) selectedNode.getDepartmentAllTree();
            
             System.out.println("################# >>>>> Tree node expanded, name = " + dep.getName());
             System.out.println("################# >>>>> id = " + dep.getAllTreeId());
             }
            
             // ############################################################################################
             public void onDepartmentTreeExpandStaff(NodeExpandedEvent event)
             {
             UITree tree = (UITree) event.getComponent();
            
             System.out.println("################# >>>>> Tree node expanded");
            
             DepartmentTreeNode selectedNode = (DepartmentTreeNode)tree.getRowData();
             AllTree dep = (AllTree) selectedNode.getDepartmentAllTree();
            
             System.out.println("################# >>>>> Tree node expanded, name = " + dep.getName());
             System.out.println("################# >>>>> id = " + dep.getAllTreeId());
             }
            
             // ############################################################################################
             public void onDepartmentTreeSelectPretenders(NodeSelectedEvent event)
             {
             if(event == null) { System.out.println("############### >>>>> Event IS NULL !!!"); return; }
            
             UITree tree = (UITree) event.getComponent();
             System.out.println("################# >>>>> Tree node selected");
             if(tree == null) { System.out.println("############### >>>>> Tree IS NULL !!!"); return; }
             DepartmentTreeNode selectedNode = (DepartmentTreeNode)tree.getRowData();
             System.out.println("name = " + selectedNode.toString());
             DepartmentTreeNode[] depNodes = selectedNode.getNodes();
            
             System.out.println("######### >> depNodes length = " + depNodes.length);
             for(DepartmentTreeNode depTN: depNodes)
             {
             System.out.println("######### >> Next node, name: " + depTN.toString());
             System.out.println("####### >> IS Leaf: " + depTN.isLeaf());
             }
            
             System.out.println("################# >>>>> Tree node selected FINISH");
             }
            
             // ############################################################################################
             public void onDepartmentTreeSelectStaff(NodeSelectedEvent event)
             {
             if(event == null) { System.out.println("############### >>>>> Event IS NULL !!!"); return; }
             UITree tree = (UITree) event.getComponent();
             System.out.println("################# >>>>> Tree node selected");
             if(tree == null) { System.out.println("############### >>>>> Tree IS NULL !!!"); return; }
            
             // action
             DepartmentTreeNode selectedNode = (DepartmentTreeNode)tree.getRowData();
             System.out.println("name = " + selectedNode.toString());
             ActionsManager am = (ActionsManager)TreeManager.getBean(WebConst.MAIN_MBEAN_NAME);
             am.getEmployeeListMan().setParameter4Staff(selectedNode.getDepartmentAllTree());
             System.out.println("parameter setParameter4Staff SET to " + selectedNode.getDepartmentAllTree().getName());
             System.out.println("################# >>>>> Tree node selected FINISH");
             }
            
            }


            and here is DepartmentTreeNode class if you need it too:
            public class DepartmentTreeNode
            {
             // ############################################################################################
             // ############################################################################################
             private static AllTreeDAORemote allTreeDAORemote;
            
             private AllTree departmentAllTree;
             private static DepartmentTreeNode[] CHILDREN_ABSENT = new DepartmentTreeNode[0];
             private DepartmentTreeNode[] children;
             private String name;
            
             // ############################################################################################
             // ############################################################################################
             public DepartmentTreeNode(AllTree departmentAllTree)
             {
             this.departmentAllTree = departmentAllTree;
             this.name = departmentAllTree.getName();
             }
            
             // ############################################################################################
             public AllTree getDepartmentAllTree()
             {
             return this.departmentAllTree;
             }
            
             // ############################################################################################
             public void setDepartmentAllTree(AllTree departmentAllTree)
             {
             this.departmentAllTree = departmentAllTree;
             this.name = departmentAllTree.getName();
             }
            
             // ############################################################################################
             public synchronized DepartmentTreeNode[] getNodes()
             {
             if (children == null)
             {
             List<AllTree> childrenAllTreeList = this.getAllTreesByParent(departmentAllTree);
            
             if (childrenAllTreeList != null)
             {
             children = new DepartmentTreeNode[childrenAllTreeList.size()];
             int i = 0;
             for(AllTree dep: childrenAllTreeList)
             {
             children = new DepartmentTreeNode(dep);
             i++;
             }
             }
             else
             {
             children = CHILDREN_ABSENT;
             }
             }
            
             return children;
             }
            
            
             // ############################################################################################
             public String toString()
             {
             return this.name;
             }
            
             // ############################################################################################
             public boolean isLeaf()
             {
             return (this.children.length > 0) ? false : true;
             }
            
             // ############################################################################################
             public List<AllTree> getAllTreesByParent(AllTree parent)
             {
             List<AllTree> departmentTypes = null;
             InitialContext context;
            
             try
             {
             context = new InitialContext();
             allTreeDAORemote = (AllTreeDAORemote) context.lookup(WebConst.APP_NAME + "/" + AllTreeDAOBean.class.getSimpleName() + "/remote");
             departmentTypes = allTreeDAORemote.getByParent(parent);
             }
             catch (Exception ex)
             {
             ex.printStackTrace();
             }
            
             return departmentTypes;
             }
            
             }


            • 3. Re: TreeStateAdvisor doesn't work on navigation-rule
              Yurii Kartsev Apprentice

              Any suggestions? Is it a bug or something? Because I think it's not my fault... Tree managed bean should work as being in session scope, as in request, isn't it?