6 Replies Latest reply on Aug 2, 2011 4:02 AM by Abdeslam NASRI

    How to keep rich:tree component state?

    Paul Ł. Newbie

      Hi,

      I found old discussion in Development part of forum abut rich:tree state: http://community.jboss.org/message/18354

       

      But the solution showed there uses TreeState class, which i cannot find in Richfaces4 M5 (maybe it was only in RF 3.x or is not migrated yet).

      There is no org.richfaces.component.state package ;/

       

      I would be grateful for any kind of information about it ;]

        • 1. How to keep rich:tree component state?
          Paul Ł. Newbie

          After downloading milestone 6 i have checked the packages and there is still no TreeState class in there.

          Has the class name changed or maybe there's another way to keep rich:tree component state between navigating through pages?

          • 2. How to keep rich:tree component state?
            Nick Belaevski Master

            Hi Paul,

             

            In RF4 you should bind 'expanded' and 'selection' to model beans explicitly using EL-expressions.

            • 3. How to keep rich:tree component state?
              Paul Ł. Newbie

              Hi Nick,

               

              Could you give me a simple example on how to use these attributes?

              • 4. Re: How to keep rich:tree component state?
                Mark Burkatzki Newbie

                I have also problems to keep the state with rich:tree and I would highly appreciate an example.

                Maybe it is a good idea to integrate this example with the richfaces showcase? Thank you very much in advance.

                • 5. Re: How to keep rich:tree component state?
                  Ilya Shaikovsky Master

                  sorry guys.. too much on the team plates prior to Final. so have only simple samples where selection is get from selection change event.

                  and filled https://issues.jboss.org/browse/RF-10701 for specific demo.

                  • 6. Re: How to keep rich:tree component state?
                    Abdeslam NASRI Newbie

                    I faced the same problem with refresh instability of rich:tree. I share with you so that may help anyway.

                     

                    My solution goes deep in reconsidernig the coding of some core java classes in richfaces UI components. Mainly 2 classes AbstractTree.java and TreeRange.java in the package org.richfaces.component had to change. I also added an interface NodeExpansionState that should be implemented by the variable that holds the tree state in the session bean. I attach the sources to this reply.

                     

                    First, define the tree node class  used by loadTree() method, for example:

                     

                    public class NamedNode extends DefaultMutableTreeNode 
                        implements NodeExpansionState {
                        protected String type;
                        protected String name;
                        protected boolean expanded =false;
                    
                        public String getName() {
                            return name;
                        }
                    
                        public void setName(String name) {
                            this.name = name;
                        }
                    
                        public String getType() {
                            return type;
                        }
                    
                        public void setType(String type) {
                            this.type = type;
                        }
                    
                        @Override
                        public String toString() {
                            return this.name;
                        }
                    
                    
                        public boolean isExpanded() {
                            return expanded;
                        }
                    
                        public void setExpanded(boolean expanded) {
                            this.expanded = expanded;
                        }
                    
                    
                    }
                    

                     

                    In the jsf page where you define your tree:

                     

                    <rich:tree id="tree"  var="node" keepSaved="true" stateVar="treeState" toggleType="ajax"  value="#{treeBean.rootNodes}" selectionType="ajax" nodeType="#{node.type}" toggleListener="#{treeBean.toggleNodeListener}">
                    
                        <rich:treeNode > 
                            #{node.name} 
                        </rich:treeNode> 
                    </rich:tree>
                    

                     

                    In your session bean, define the toggle listener:

                     

                    @ManagedBean
                    @SessionScoped
                    public class TreeBean implements Serializable {
                        private List<TreeNode> rootNodes = new ArrayList<TreeNode>();
                    
                    .....
                    
                        public void toggleNodeListener(TreeToggleEvent ev){
                            UITree tree = (UITree)ev.getSource();
                            NodeExpansionState nodeExp = ((NodeExpansionState)tree.getRowData());
                            nodeExp.setExpanded(!nodeExp.isExpanded());
                        }
                    
                    }
                    

                     

                    You can even decide to set some nodes expanded by default by simply set Expanded to true when you create the tree in addNodes(...) method.