1 Reply Latest reply on Sep 23, 2008 10:35 AM by Nick Belaevski

    Problem implementing the richfaces Tree example.

    Paul Nutlis Newbie

      Hello,

      I'm trying to implement a rich:tree sample with FineSystemBean and no tree is rendered.
      I narrowed it down to the problem with FileSystemNode class that always returns CHILDREN_ABSENT due to externalContext.getResourcePaths(this.path) returning null.
      I know it has something to do with ExternalContext and the Path parameter that I pass to FileSystemNode.getNodes(),
      I tried to pass /proj1, /proj1/resources and /WEB-INF/src but nothing worked.

      Below is the code.
      Please help.
      Thanks in advance for any suggestion.
      pn.


      FileSystemNode:
      *****************************************************
      package org.richfaces.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) {
      System.out.println("path: " + path);
      this.path = path;
      int idx = path.lastIndexOf('/');
      if (idx != -1) {
      shortPath = path.substring(idx + 1);
      } else {
      shortPath = path;
      }
      }

      public synchronized FileSystemNode[] getNodes() {
      if (children == null) {
      FacesContext facesContext = FacesContext.getCurrentInstance();
      ExternalContext externalContext = facesContext.getExternalContext();

      System.out.println("User is: " + externalContext.getRemoteUser());
      System.out.println("URL: "+ externalContext.getRequestContextPath());
      System.out.println("Paths: "+ externalContext.getResourcePaths(this.path));

      Set resourcePaths = externalContext.getResourcePaths(this.path);
      if (resourcePaths != null) {
      Object[] nodes = (Object[]) resourcePaths.toArray();
      children = new FileSystemNode[nodes.length];
      System.out.println("from if ...");
      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("from else ...");
      children = CHILDREN_ABSENT;
      }
      }

      return children;
      }

      public String toString() {
      return shortPath;
      }

      }


      FileSystemBean:
      *****************************************************
      package org.richfaces.treemodeladaptor;

      import org.jboss.seam.annotations.Name;
      import org.jboss.seam.annotations.Scope;

      @Name ("fileSystemBean")
      @Scope(org.jboss.seam.ScopeType.SESSION)
      public class FileSystemBean {
      //private static String SRC_PATH = "/WEB-INF/src";
      private static String SRC_PATH = "/proj1";

      private FileSystemNode[] srcRoots;

      public synchronized FileSystemNode[] getSourceRoots() {
      System.out.println("Hello from bean");
      if (srcRoots == null) {
      System.out.println("inside if");
      srcRoots = new FileSystemNode(SRC_PATH).getNodes();
      }

      return srcRoots;
      }
      }
      Console Output:
      *****************************************************
      14:13:13,656 INFO [STDOUT] Hello from bean
      14:13:13,656 INFO [STDOUT] inside if
      14:13:13,656 INFO [STDOUT] path: /proj1
      14:13:13,656 INFO [STDOUT] User is: null
      14:13:13,656 INFO [STDOUT] URL: /proj1
      14:13:13,656 INFO [STDOUT] Paths: null
      14:13:13,656 INFO [STDOUT] from else ...

      the environment is:
      *****************************************************
      eclipse with jboss tools + seam 2.0 + jboss 4.2.3