1 Reply Latest reply on Apr 2, 2009 8:27 AM by nbelaevski

    Not able form Tree node with properties files

    praveenk20

      Hi ,

      I am using Tree node example with properties file. but getting the exception in the code

      java.lang.ClassCastException: org.richfaces.model.TreeNodeImpl

      <<<< Java Code >>>>>
      public class SimpleTreeBean {

      private TreeNode rootNode = null;
      private List selectedNodeChildren = new ArrayList();

      private String nodeTitle;
      private static final String DATA_PATH = "/WEB-INF/classes/simple-tree-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);
      System.out.println("key "+key);
      String value = properties.getProperty(key);
      System.out.println("value "+value);
      if (value != null) {
      TreeNode nodeImpl = new TreeNodeImpl();
      nodeImpl.setData(value);
      node.addChild(new Integer(counter), nodeImpl);
      System.out.println("key, nodeImpl, properties "+key+" "+node.getParent()+" "+properties);
      addNodes(key, nodeImpl, properties);
      //addNodes(key, node, properties);
      counter++;
      } else {
      end = true;
      }
      }
      }

      private void loadTree() {
      FacesContext facesContext = FacesContext.getCurrentInstance();
      ExternalContext externalContext = facesContext.getExternalContext();
      InputStream dataStream = externalContext.getResourceAsStream(DATA_PATH);

      System.out.println(" externalContext.getRequestContextPath() "+externalContext.getRequestContextPath());
      System.out.println(" externalContext.getRequestPathInfo() "+externalContext.getRequestPathInfo());
      try {
      System.out.println("inside try method ");
      Properties properties = new Properties();
      properties.load(dataStream);

      rootNode = new TreeNodeImpl();
      addNodes(null, rootNode, properties);

      } catch (IOException e) {
      System.out.println("EXception occured >>>> "+e.toString());
      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;
      }
      }
      <<<<<<< END >>>>>>

      <<<<<< JSF page START >>>>>

      <ui:composition xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:a4j="http://richfaces.org/a4j"
      xmlns:rich="http://richfaces.org/rich">


      .col, .col2 {
      width:50%;
      vertical-align:top;
      }



      <h:form>
      <h:panelGrid columns="2" width="100%" columnClasses="col1,col2">

      <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" />

      </h:panelGrid>

      </h:form>

      </ui:composition>


      Appreciate help...
      <<<<<< JSF page END >>>>>