1 Reply Latest reply on Oct 8, 2007 4:15 AM by Kerdudou Ronan

    recursiveTreeNodesAdaptor - demo code does not show nodes be

    Anjali Pandit Newbie

      I am a novice JSF developer and have experimenting with using the richFaces component recursiveTreeNodesAdaptor to build a tree. The first thing I tried was trying to make the demo code work on my system. The tree come up in the UI but the code recurses only two levels deep in the tree and does not go beyond that. Please help me in identifying where am I going wrong.


      The file structure that i have created under /WEB-INF/src is following :
      +Folder1
      ++Folder1.1
      +++Folder1.1.1
      +Folder2
      ++Folder2.1
      +++Folder2.1.1
      +++Folder2.1.2
      ++Folder2.2
      +++Folder2.2.1
      +++Folder2.2.2
      +Folder3
      ++Folder3.1
      ++Folder3.2
      ++Folder3.3

      What I get displayed in UI is following structure :

      +Folder1
      ++Folder1.1
      +Folder2
      ++Folder2.1
      ++Folder2.2
      +Folder3
      ++Folder3.1
      ++Folder3.2
      ++Folder3.3

      Here is demo code with my debug messages included and at the bottom is the debug log :

      FileSystemBean.java
      ==============
      package treemodeladaptor;

      public class FileSystemBean {
      private static String SRC_PATH = "/WEB-INF/src";

      private FileSystemNode[] srcRoots;

      public synchronized FileSystemNode[] getSourceRoots() {
      System.out.println("getSourceRoots + ");
      if (srcRoots == null) {
      System.out.println("getSourceRoots - srcRoots is null ");
      srcRoots = new FileSystemNode(SRC_PATH).getNodes();
      }

      return srcRoots;
      }
      }


      FileSystemNode.java
      =============

      package treemodeladaptor;

      import java.util.Set;

      import javax.faces.context.ExternalContext;
      import javax.faces.context.FacesContext;

      public class FileSystemNode {
      private String path;

      private static FileSystemNode[] CHILDREN_ABSENT = new FileSystemNode[0];

      private FileSystemNode[] children;

      private String shortPath;

      public FileSystemNode(String path) {
      this.path = path;
      int idx = path.lastIndexOf('/');
      if (idx != -1) {
      shortPath = path.substring(idx + 1);
      } else {
      shortPath = path;
      }
      }

      public synchronized FileSystemNode[] getNodes() {
      //System.out.println("==getNodes + : ");
      System.out.println("==getNodes + path : " + path);
      if (children == null) {
      System.out.println("====getNodes - children array is null");
      FacesContext facesContext = FacesContext.getCurrentInstance();
      ExternalContext externalContext = facesContext.getExternalContext();
      Set resourcePaths = externalContext.getResourcePaths(this.path);
      if (resourcePaths != null)
      {
      Object[] nodes = (Object[]) resourcePaths.toArray();
      System.out.println("======getNodes - Child Node Count : " + nodes.length);
      children = new FileSystemNode[nodes.length];

      for (int i = 0; i < nodes.length; i++) {
      String nodePath = nodes.toString();
      if (nodePath.endsWith("/")) {
      nodePath = nodePath.substring(0, nodePath.length() - 1);
      }
      children
      = new FileSystemNode(nodePath);
      }
      } else {
      System.out.println("======getNodes - No Child Nodes ");
      children = CHILDREN_ABSENT;
      }
      }

      return children;
      }

      public String toString() {
      return shortPath;
      }

      }

      PostbackPhaseListener.java
      ==================

      package treemodeladaptor;

      import java.util.Map;
      import javax.faces.context.ExternalContext;
      import javax.faces.context.FacesContext;
      import javax.faces.event.PhaseEvent;
      import javax.faces.event.PhaseId;
      import javax.faces.event.PhaseListener;

      public class PostbackPhaseListener implements PhaseListener {

      public static final String POSTBACK_ATTRIBUTE_NAME = PostbackPhaseListener.class.getName();

      public void afterPhase(PhaseEvent event) {
      }

      public void beforePhase(PhaseEvent event) {
      System.out.println("beforePhase + ");
      FacesContext facesContext = event.getFacesContext();
      Map requestMap = facesContext.getExternalContext().getRequestMap();
      requestMap.put(POSTBACK_ATTRIBUTE_NAME, Boolean.TRUE);
      }

      public PhaseId getPhaseId() {
      return PhaseId.APPLY_REQUEST_VALUES;
      }

      public static boolean isPostback() {
      System.err.println("isPostback + ");
      FacesContext facesContext = FacesContext.getCurrentInstance();
      if (facesContext != null) {
      ExternalContext externalContext = facesContext.getExternalContext();
      if (externalContext != null) {
      return Boolean.TRUE.equals(
      externalContext.getRequestMap().get(POSTBACK_ATTRIBUTE_NAME));
      }
      }

      return false;
      }

      }


      TreeDemoStateAdvisor.java
      ==================
      package treemodeladaptor;

      import org.richfaces.component.UITree;
      import org.richfaces.component.state.TreeStateAdvisor;
      import org.richfaces.model.TreeRowKey;

      public class TreeDemoStateAdvisor implements TreeStateAdvisor {

      public Boolean adviseNodeOpened(UITree tree) {
      System.err.println("adviseNodeOpened + ");
      if (!PostbackPhaseListener.isPostback()) {
      Object key = tree.getRowKey();
      TreeRowKey treeRowKey = (TreeRowKey) key;
      System.err.println("adviseNodeOpened 1 - tree depth : " + treeRowKey.depth() );
      if (treeRowKey == null || treeRowKey.depth() <= 2) {
      return Boolean.TRUE;
      }
      }

      return null;
      }

      public Boolean adviseNodeSelected(UITree tree) {
      return null;
      }

      }


      Here is the debug log :
      "
      getSourceRoots +
      getSourceRoots - srcRoots is null
      ==getNodes + path : /WEB-INF/src
      ====getNodes - children array is null
      ======getNodes - Child Node Count : 3
      getSourceRoots +
      adviseNodeOpened +
      isPostback +
      adviseNodeOpened 1 - tree depth : 1
      ==getNodes + path : /WEB-INF/src/Folder3
      ====getNodes - children array is null
      ======getNodes - Child Node Count : 3
      ==getNodes + path : /WEB-INF/src/Folder3
      ==getNodes + path : /WEB-INF/src/Folder3
      ==getNodes + path : /WEB-INF/src/Folder3
      ==getNodes + path : /WEB-INF/src/Folder3
      ==getNodes + path : /WEB-INF/src/Folder3
      ==getNodes + path : /WEB-INF/src/Folder3
      adviseNodeOpened +
      isPostback +
      adviseNodeOpened 1 - tree depth : 2
      ==getNodes + path : /WEB-INF/src/Folder3
      adviseNodeOpened +
      isPostback +
      adviseNodeOpened 1 - tree depth : 2
      ==getNodes + path : /WEB-INF/src/Folder3
      adviseNodeOpened +
      isPostback +
      adviseNodeOpened 1 - tree depth : 2
      getSourceRoots +
      adviseNodeOpened +
      isPostback +
      adviseNodeOpened 1 - tree depth : 1
      ==getNodes + path : /WEB-INF/src/Folder1
      ====getNodes - children array is null
      ======getNodes - Child Node Count : 2
      ==getNodes + path : /WEB-INF/src/Folder1
      ==getNodes + path : /WEB-INF/src/Folder1
      ==getNodes + path : /WEB-INF/src/Folder1
      ==getNodes + path : /WEB-INF/src/Folder1
      ==getNodes + path : /WEB-INF/src/Folder1
      ==getNodes + path : /WEB-INF/src/Folder1
      adviseNodeOpened +
      isPostback +
      adviseNodeOpened 1 - tree depth : 2
      ==getNodes + path : /WEB-INF/src/Folder1
      adviseNodeOpened +
      isPostback +
      adviseNodeOpened 1 - tree depth : 2
      getSourceRoots +
      adviseNodeOpened +
      isPostback +
      adviseNodeOpened 1 - tree depth : 1
      ==getNodes + path : /WEB-INF/src/Folder2
      ====getNodes - children array is null
      ======getNodes - Child Node Count : 2
      ==getNodes + path : /WEB-INF/src/Folder2
      ==getNodes + path : /WEB-INF/src/Folder2
      ==getNodes + path : /WEB-INF/src/Folder2
      ==getNodes + path : /WEB-INF/src/Folder2
      ==getNodes + path : /WEB-INF/src/Folder2
      ==getNodes + path : /WEB-INF/src/Folder2
      adviseNodeOpened +
      isPostback +
      adviseNodeOpened 1 - tree depth : 2
      ==getNodes + path : /WEB-INF/src/Folder2
      adviseNodeOpened +
      isPostback +
      adviseNodeOpened 1 - tree depth : 2
      "

        • 1. Re: recursiveTreeNodesAdaptor - demo code does not show node
          Kerdudou Ronan Apprentice

          as the demo is quite complicated, you should begin with something simpler...

          just make a recursive bean structure (in java) and forget the advisor (and the PostbackPhaseListener)...

          just map your recursive object with the recursiveTreeNodesAdaptor and see how it works very well...

          <rich:tree>
          <rich:recursiveTreeNodesAdaptor roots="#{handler.firstNode}" var="node" nodes="#{node.childs}" >
          <rich:treeNode>
          <h:outputText value="#{node.label}" />
          </rich:treeNode>
          </rich:recursiveTreeNodesAdaptor>
          </rich:tree>


          in your handler : getFirstNode() returns a BeanNode that is the root of your tree.
          a BeanNode has a property 'childs' that is a list of BeanNode. and for exemple a property 'label' that contains a label for the node.