0 Replies Latest reply on Apr 7, 2008 3:10 PM by Thomas Kessler

    Building a tree node structure outside of application direct

    Thomas Kessler Newbie

      A while ago I was trying to build a tree node hierarchy of a directory on my web server. The code on this page: http://livedemo.exadel.com/richfaces-demo/richfaces/treeNodesAdaptor.jsf?c=recursiveTreeNodesAdaptor shows how to build a tree node structure of a directory with in the application's own folder, but not a folder located outside of the application. I was able to achieve this functionality with very little change in code. I'm sure this trivial information to most, but for some it might help.

       private static String SRC_PATH = "/download";
       private FileSystemNode[] srcRoots;
      
       public synchronized FileSystemNode[] getSourceRoots() {
       if (srcRoots == null) {
       srcRoots = new FileSystemNode(SRC_PATH).getNodes();
       }
       return srcRoots;
       }
      


       public class FileSystemNode {
      
       private static FileSystemNode[] CHILDREN_ABSENT = new FileSystemNode[0];
       private FileSystemNode[] children;
       private String path;
       private String shortPath;
      
       public FileSystemNode() {
       }
      
       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() {
      
       if (children == null) {
      
       File file = new File(this.path);
      
       if (file != null) {
      
       Object[] nodes = (Object[]) file.list();
      
       if (nodes != null) {
      
       Arrays.sort(nodes);
      
       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(this.path + "/" + nodePath);
       }
       } else {
       children = CHILDREN_ABSENT;
       }
       }
       }
      
       return children;
       }
      
       public String toString() {
       return shortPath;
       }
      
       public String getShortPath() {
       return shortPath;
       }
      }