I figured out how to expand all nodes the first time tree2 is rendered.  For those interested, below is the source of two java files and snippets from a tld and faces configuration file.  Constructive criticism is appreciated.

package org.apache.myfaces.custom.tree3;

import javax.faces.context.FacesContext;

import org.apache.myfaces.custom.tree2.TreeNode;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * Child of tree2 UI component; this control will repect the
 * expandAllInit attribute.
 *
 */
public class HtmlTree extends org.apache.myfaces.custom.tree2.HtmlTree{
       
        private boolean justCreated = true;
       
    public boolean isJustCreated() {
                return justCreated;
        }
   
        public void setJustCreated(boolean justCreated) {
                this.justCreated = justCreated;
        }
       
    // see superclass for documentation
    public void restoreState(FacesContext context, Object state)
    {
            this.setJustCreated(false);
            super.restoreState(context, state);
    }

    /**
     * Indicates whether or not the current [EMAIL PROTECTED] TreeNode} is expanded.
     *
     * if this is before a restoreState call, and the expandAllInit att. is set to true
     * and this is not a leaf, and the node is not expanded ... otherwise it will behave
     * as the parent would.
     *  
     * @return boolean
     */
    public boolean isNodeExpanded()
    {
            boolean ret = super.isNodeExpanded();
           
            if(justCreated && getExpandAllInit() && getNode().getChildCount() != 0 && ! ret){
                        toggleExpanded();
                        return true;
            }
            return ret;
    }
   
    /**
     * Iterates through the prop.s of the component, determines if all nodes are
     * expanded the first time they are rendered.
     *
     * @return boolean
     */
   
        protected boolean getExpandAllInit() {
                String key = null;
                Boolean value = null;
                Map map = null;
                Set set = null;
                Iterator iterator = null;
               
                map = getAttributes();
                iterator = map.keySet().iterator();
               
                while(iterator.hasNext()){
                        key = (String) iterator.next();
                        if(TreeTag.EXPAND_ALL_INIT.equals(key) ){
                                value = (Boolean) map.get(key);
                                return value.booleanValue();
                        }
                }
               
                return false;
        }
   
}

package org.apache.myfaces.custom.tree3;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.el.ValueBinding;

import org.apache.myfaces.renderkit.JSFAttr;

public class TreeTag extends org.apache.myfaces.custom.tree2.TreeTag {
       
    private String _value;
    private String _var;
    private String _varNodeToggler;
    private String _showLines;
    private String _showNav;
    private String _clientSideToggle;
    private String _showRootNode;
       
        private String expandAllInit = "false";
        public static String EXPAND_ALL_INIT = "org.apache.myfaces.tree.EXPAND_ALL_INIT";
       
        private static String COMPONENT_TYPE = "org.apache.myfaces.HtmlTree3";
       
        public String getExpandAllInit() {
                return expandAllInit;
        }
       
        public void setExpandAllInit(String expandAllInit) {
                this.expandAllInit = expandAllInit;
        }
       
        public String getComponentType() {
                return COMPONENT_TYPE;
        }
       
    public void setValue(String value)
    {
        _value = value;
    }

    /**
     * @param var The var to set.
     */
    public void setVar(String var)
    {
        _var = var;
    }

    public void setVarNodeToggler(String varNodeToggler)
    {
        _varNodeToggler = varNodeToggler;
    }

    public void setShowLines(String showLines)
    {
        _showLines = showLines;
    }

    public void setShowNav(String showNav)
    {
        _showNav = showNav;
    }

    public void setClientSideToggle(String clientSideToggle)
    {
        _clientSideToggle = clientSideToggle;
    }
   
    public void setShowRootNode(String showRootNode)
    {
        _showRootNode = showRootNode;
    }

          protected void setProperties(UIComponent component)
            {
                super.setProperties(component);
               
                FacesContext context = getFacesContext();

                if (_value != null)
                {
                    ValueBinding vb = context.getApplication().createValueBinding(_value);
                    component.setValueBinding("value", vb);
                }
               
                if (_var != null)
                {
                    ((HtmlTree)component).setVar(_var);
                }
               
                if (_varNodeToggler != null)
                {
                    ((HtmlTree)component).setVarNodeToggler(_varNodeToggler);
                }
               
                setBooleanProperty(component, JSFAttr.SHOW_NAV, _showNav);
                setBooleanProperty(component, JSFAttr.SHOW_LINES, _showLines);
                setBooleanProperty(component, JSFAttr.CLIENT_SIDE_TOGGLE, _clientSideToggle);
                setBooleanProperty(component, JSFAttr.SHOW_ROOT_NODE, _showRootNode);
                // ideally, we would add a static var to JSFAttr
                setBooleanProperty(component, EXPAND_ALL_INIT, expandAllInit);
            }

}

In your JSF config file:

  <component>
    <component-type>org.apache.myfaces.HtmlTree3</component-type>
    <component-class>org.apache.myfaces.custom.tree3.HtmlTree</component-class>
  </component>

In your TLD:

    <tag>
        <name>tree3</name>
        <tag-class>org.apache.myfaces.custom.tree3.TreeTag</tag-class>
        <body-content>JSP</body-content>
       
        ... the same att.s you find in the tld extensions file for JSF 1.0.9

        <attribute>
            <description>Expand all nodes the initial time this control is rendered.</description>        
            <name>expandAllInit</name>
            <required>false</required>
            <rtexprvalue>false</rtexprvalue>
        </attribute>
    </tag>  

Reply via email to