aoqi@0: #// Usage: jjs -fx filebrowser.js -- aoqi@0: aoqi@0: /* aoqi@0: * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. attila@962: * aoqi@0: * Redistribution and use in source and binary forms, with or without aoqi@0: * modification, are permitted provided that the following conditions aoqi@0: * are met: attila@962: * aoqi@0: * - Redistributions of source code must retain the above copyright aoqi@0: * notice, this list of conditions and the following disclaimer. attila@962: * aoqi@0: * - Redistributions in binary form must reproduce the above copyright aoqi@0: * notice, this list of conditions and the following disclaimer in the aoqi@0: * documentation and/or other materials provided with the distribution. attila@962: * aoqi@0: * - Neither the name of Oracle nor the names of its aoqi@0: * contributors may be used to endorse or promote products derived aoqi@0: * from this software without specific prior written permission. attila@962: * aoqi@0: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS aoqi@0: * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, aoqi@0: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR aoqi@0: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR aoqi@0: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, aoqi@0: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, aoqi@0: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR aoqi@0: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF aoqi@0: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING aoqi@0: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS aoqi@0: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. aoqi@0: */ aoqi@0: aoqi@0: // Uses -fx and javafx TreeView to visualize directories aoqi@0: if (!$OPTIONS._fx) { aoqi@0: print("Usage: jjs -fx filebrowser.js -- "); aoqi@0: exit(1); aoqi@0: } aoqi@0: aoqi@0: // Java classes used aoqi@0: var File = Java.type("java.io.File"); aoqi@0: var Files = Java.type("java.nio.file.Files"); aoqi@0: aoqi@0: // check directory argument, if passed aoqi@0: var dir = arguments.length > 0? new File(arguments[0]) : new File("."); aoqi@0: if (! dir.isDirectory()) { aoqi@0: print(dir + " is not a directory!"); aoqi@0: exit(2); aoqi@0: } aoqi@0: aoqi@0: // JavaFX classes used aoqi@0: var FXCollections = Java.type("javafx.collections.FXCollections"); aoqi@0: var Scene = Java.type("javafx.scene.Scene"); aoqi@0: var TreeItem = Java.type("javafx.scene.control.TreeItem"); aoqi@0: var TreeView = Java.type("javafx.scene.control.TreeView"); aoqi@0: aoqi@0: // create a subclass of JavaFX TreeItem class aoqi@0: var LazyTreeItem = Java.extend(TreeItem); aoqi@0: aoqi@0: // lazily filling children of a directory LazyTreeItem aoqi@0: function buildChildren(dir) { aoqi@0: var children = FXCollections.observableArrayList(); aoqi@0: var stream = Files.list(dir.toPath()); aoqi@0: stream.forEach(function(path) { aoqi@0: var file = path.toFile(); aoqi@0: var item = file.isDirectory()? aoqi@0: makeLazyTreeItem(file) : new TreeItem(file.name); aoqi@0: children.add(item); aoqi@0: }); aoqi@0: stream.close(); aoqi@0: return children; aoqi@0: } aoqi@0: aoqi@0: // create an instance LazyTreeItem with override methods aoqi@0: function makeLazyTreeItem(dir) { aoqi@0: var item = new LazyTreeItem(dir.name) { aoqi@0: expanded: false, aoqi@0: isLeaf: function() false, aoqi@0: getChildren: function() { aoqi@0: if (! this.expanded) { aoqi@0: // call super class (TreeItem) method aoqi@0: Java.super(item).getChildren().setAll(buildChildren(dir)); aoqi@0: this.expanded = true; aoqi@0: } aoqi@0: // call super class (TreeItem) method aoqi@0: return Java.super(item).getChildren(); aoqi@0: } aoqi@0: } aoqi@0: return item; aoqi@0: } aoqi@0: aoqi@0: // JavaFX start method aoqi@0: function start(stage) { aoqi@0: stage.title = dir.absolutePath; aoqi@0: var rootItem = makeLazyTreeItem(dir); aoqi@0: rootItem.expanded = true; aoqi@0: var tree = new TreeView(rootItem); aoqi@0: stage.scene = new Scene(tree, 300, 450); aoqi@0: stage.show(); aoqi@0: }