0 Replies Latest reply on Nov 4, 2011 10:36 AM by Vinny Tiwari

    Rich Tree - Hyperlink to the leaf nodes

    Vinny Tiwari Newbie

      Hi,

       

        I am using rich:tree to create a tree and I want to attach hyperlink to the leaf nodes. Can some one suggesst how to do this?

       

      Thanks,

       

      xhtml -

       

      <rich:tree switchType="client" nodeSelectListener="#{simpleTreeBean.processSelection}" value="#{simpleTreeBean.treeNode}" var="item"/>

       

      Java Code -

       

      public class SimpleTreeBean {

      private TreeNode rootNode = null;
          private List<String> selectedNodeChildren = new ArrayList<String>();   
         
          private String nodeTitle;
          private static final String DATA_PATH = "/Pages/data.properties";
         
         
          private void addNodes(String path, TreeNode 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 nodeImpl = new TreeNodeImpl();
                      nodeImpl.setData(value);
                     
                      System.out.println("Data..."+nodeImpl.getData());
                     
                      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();
              InputStream dataStream = externalContext.getResourceAsStream(DATA_PATH);
              try {
                  Properties properties = new Properties();
                  properties.load(dataStream);
                 
                  rootNode = new TreeNodeImpl();
                  addNodes(null, rootNode, properties);
                  System.out.println("Title..."+rootNode.isLeaf());
                 
              } 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 void processSelection(NodeSelectedEvent event) {
              HtmlTree tree = (HtmlTree) event.getComponent();
              nodeTitle = (String) tree.getRowData();
              selectedNodeChildren.clear();
              TreeNode currentNode = tree.getModelTreeNode(tree.getRowKey());
              if (currentNode.isLeaf()){
                  selectedNodeChildren.add((String)currentNode.getData());        
              }else
              {
                  Iterator<Map.Entry<Object, TreeNode>> it = currentNode.getChildren();
                  while (it!=null &&it.hasNext()) {
                      Map.Entry<Object, TreeNode> entry = it.next();
                      selectedNodeChildren.add(entry.getValue().getData().toString());
                  }
              }
          }
         
          public TreeNode getTreeNode() {
              if (rootNode == null) {
                  loadTree();           
              }
              return rootNode;
          }


         
          public String getNodeTitle() {
              return nodeTitle;
          }

          public void setNodeTitle(String nodeTitle) {
              this.nodeTitle = nodeTitle;
          }
      }