3 Replies Latest reply on Jul 22, 2008 10:01 AM by ilya_shaikovsky

    Help with tree

    ravemaster

      Hi,

      in my jsp I've the following code:

      <rich:tree style="width:300px" nodeSelectListener="#{SimpleTreeBean.processSelection}"
       reRender="selectedNode" ajaxSubmitSelection="true" switchType="client"
       value="#{SimpleTreeBean.treeNode}" var="item">
      
       </rich:tree>
       <h:outputText escape="false" value="Selected node: #{SimpleTreeBean.nodeTitle}" id="selectedNode" />
      


      and my SimpleTreeBean.java:

      import java.io.IOException;
      import java.io.InputStream;
      import java.util.Properties;
      import javax.faces.FacesException;
      import javax.faces.context.ExternalContext;
      import javax.faces.context.FacesContext;
      import org.richfaces.component.UITree;
      import org.richfaces.component.UITree;
      import org.richfaces.event.NodeSelectedEvent;
      import org.richfaces.model.TreeNodeImpl;
      import org.richfaces.event.NodeSelectedEvent;
      import org.richfaces.model.TreeNode;
      import org.richfaces.model.TreeNodeImpl;
      
      public class SimpleTreeBean {
      
       private TreeNode<String> rootNode = null;
       private String nodeTitle = "prova";
       private static final String DATA_FILE = "data-tree.properties";
      
      
       public SimpleTreeBean(){
      
       }
      
       private void addNodes(String path, TreeNode<String> node, Properties properties) {
       boolean end = false;
       int counter = 1;
       while (!end) {
       String key = path != null ? path + '.' + counter : String.valueOf(counter);
       String value = properties.getProperty(key);
       if (value != null) {
       TreeNodeImpl<String> nodeImpl = new TreeNodeImpl<String>();
       nodeImpl.setData(value);
       node.addChild(new Integer(counter), nodeImpl);
      
       addNodes(key,nodeImpl,properties);
       counter++;
       }else{
       end=true;
       }
       }
       }
      
       private void loadTree(){
       FacesContext facesContext = FacesContext.getCurrentInstance();
       ExternalContext externalContext = facesContext.getExternalContext();
       ClassLoader cl = this.getClass().getClassLoader();
       InputStream dataStream = cl.getResourceAsStream(DATA_FILE);
       try{
       Properties properties = new Properties();
       properties.load(dataStream);
       rootNode = new TreeNodeImpl<String>();
       rootNode.setData("Prova");
       addNodes(null,rootNode,properties);
       }catch(IOException e){
       throw new FacesException(e.getMessage(),e);
       }finally{
       if(dataStream!=null){
       try{
       dataStream.close();
       }catch(IOException e){
       externalContext.log(e.getMessage(),e);
       }
       }
       }
       }
      
       public TreeNode getTreeNode(){
       if(rootNode==null){
       loadTree();
       }
       return rootNode;
       }
      
       public void processSelection(NodeSelectedEvent event){
       UITree tree =(UITree)event.getComponent();
       nodeTitle = (String)tree.getRowData();
       }
      
       public String getNodeTitle(){
       return nodeTitle;
       }
      
       public void setNodeTitle(String nodeTitle){
       this.nodeTitle = nodeTitle;
       }
      
      }
      


      Now... all works fine, the tree is filled with the values of my data-tree.properties file, but when I click on a node, nothing happens. The method processSelection is not executed. Why? Where is the error?

      Thanx