0

Given a folder I would like to construct tree of children down to x levels and render as jstrees html format (i.e ul, li so on) https://www.jstree.com/docs/html/ so that server files can be viewed remotely over webbrowser.

What is the best way to do this ?

Has anyone created a lib tieing java and jstree to do it, seems I cant be the first person to want to do this.

Paul Taylor
  • 13,411
  • 42
  • 184
  • 351

2 Answers2

2

first, I'd like to point out that jsTree also accepts JSON as data input and not only raw html(https://www.jstree.com/docs/json/)

It looks like this:

// Expected format of the node (there are no required fields)
{
  id          : "string" // will be autogenerated if omitted
  text        : "string" // node text
  icon        : "string" // string for custom
  state       : {
    opened    : boolean  // is the node open
    disabled  : boolean  // is the node disabled
    selected  : boolean  // is the node selected
  },
  children    : []  // array of strings or objects
  li_attr     : {}  // attributes for the generated LI node
  a_attr      : {}  // attributes for the generated A node
}

To produce such a JSON format (or the equivalent in HTML form) can be done fairly easely using a recursive function. Here would be the pseudo-code:

class Node {
  String id;
  String text;
  // ...
  List<Node> children;
}

class Filesystem {

   Node browse(File path, int depth) {
       Node node = new Node(file);
       if( depth > 0 ) {
          for(File f : file.listFiles()) {
             Node child = browse(f, depth - 1);
             node.children.add(child);
          }
       }
       return node;
   }
}

It's not a copy-paste solution, but it should fairly straighforward to obtain one.

dagnelies
  • 5,203
  • 5
  • 38
  • 56
  • Thanks this is basically what I have now indeed done, I use FileVisitor and a class similar to Filesystem to create a partial representation of the file system. I create a Data class to represent the data which I turn into Json using Gson and is passed as a json data to a javascript function for jstree . Im now trying to work out how to get ajax working so that I only have to render part of the tree at startup and then use ajax as required when user clicks to open node that we havent retrieved data for yet. But if I get stuck on that will post as different question – Paul Taylor Jan 22 '18 at 16:59
  • Okay I am stuck please see https://stackoverflow.com/questions/48386954/can-i-use-jstree-preloaded-with-json-data-and-use-also-ajax I will award you bounty when time has expired. – Paul Taylor Jan 22 '18 at 17:20
1

Just to clarify your problem:

  • You have a Java application running on a server.
  • The Java application is used to render web pages viewed in a web browser.
  • You want to use jsTree, a Javascript library, to render pretty trees for your client to see.
  • You want jsTree to show the structure of some folders on your server.

jsTree is a javascript library, thus, somewhere in your solution you have to execute Javascript. While it is certainly possible for Java to execute Javascript (Rhino looks promising), it would seem that the browser would be the most natural place.

How does jsTree take input? From the website, it indicates it supports Ajax and JSON. JSON can certainly be embedded in a page by a server app rendering the page, so that seems like a good fit.

So then a solution appears. Your Java server app renders the page. While it is rendering the page, it includes the jsTree library, and it embeds a JSON document containing the structure information of the server-side folders you want the client to see.

Javascript running in the client's browser feeds that embedded json data to jsTree, and you get a pretty diagram.

antiduh
  • 11,853
  • 4
  • 43
  • 66
  • Correct, what Im struggling with it creatig the json representation of server side folders. I want to create representation of servers harddrive down to four levels), looking at using FileVisitor starting with each path returned form FileSystems.getDefault().getRootDirectories() but I cant quite work how to build up the structure. – Paul Taylor Jan 21 '18 at 19:29
  • 1
    @PaulTaylor - I wish you had been more clear in your original question. Your problem is that you need to iterate some folders filling out some json object; the bit about jsTree is ultimately irrelevant. Update your question to specify the parameters of the problem - be specific with your folder enumeration problem, what kind of folders you need to enumerate, limits, etc. Make sure to show what you've already tried, what you've already done so far. – antiduh Jan 21 '18 at 19:38
  • you are right sorry my thoughts were unclear. Okay the specifics of the folder enumeration are detailed in a diferent question - https://stackoverflow.com/questions/48369728/in-java-how-do-i-create-a-structured-tree-using-filevisitor . But with this particular question the reason I ask if there was a lib is 1> because traversing all folders (only) seems quite standard, 2> it cannot be all done at init because too slow so will need callbacks if user actualy browses further down the tree and hence the jstree/java problem gets more complicated. – Paul Taylor Jan 21 '18 at 20:19