1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/samples/filebrowser.js Wed Apr 27 01:36:41 2016 +0800 1.3 @@ -0,0 +1,100 @@ 1.4 +#// Usage: jjs -fx filebrowser.js -- <start_dir> 1.5 + 1.6 +/* 1.7 + * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. 1.8 + * 1.9 + * Redistribution and use in source and binary forms, with or without 1.10 + * modification, are permitted provided that the following conditions 1.11 + * are met: 1.12 + * 1.13 + * - Redistributions of source code must retain the above copyright 1.14 + * notice, this list of conditions and the following disclaimer. 1.15 + * 1.16 + * - Redistributions in binary form must reproduce the above copyright 1.17 + * notice, this list of conditions and the following disclaimer in the 1.18 + * documentation and/or other materials provided with the distribution. 1.19 + * 1.20 + * - Neither the name of Oracle nor the names of its 1.21 + * contributors may be used to endorse or promote products derived 1.22 + * from this software without specific prior written permission. 1.23 + * 1.24 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 1.25 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 1.26 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 1.27 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 1.28 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 1.29 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 1.30 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 1.31 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 1.32 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 1.33 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 1.34 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.35 + */ 1.36 + 1.37 +// Uses -fx and javafx TreeView to visualize directories 1.38 +if (!$OPTIONS._fx) { 1.39 + print("Usage: jjs -fx filebrowser.js -- <start_dir>"); 1.40 + exit(1); 1.41 +} 1.42 + 1.43 +// Java classes used 1.44 +var File = Java.type("java.io.File"); 1.45 +var Files = Java.type("java.nio.file.Files"); 1.46 + 1.47 +// check directory argument, if passed 1.48 +var dir = arguments.length > 0? new File(arguments[0]) : new File("."); 1.49 +if (! dir.isDirectory()) { 1.50 + print(dir + " is not a directory!"); 1.51 + exit(2); 1.52 +} 1.53 + 1.54 +// JavaFX classes used 1.55 +var FXCollections = Java.type("javafx.collections.FXCollections"); 1.56 +var Scene = Java.type("javafx.scene.Scene"); 1.57 +var TreeItem = Java.type("javafx.scene.control.TreeItem"); 1.58 +var TreeView = Java.type("javafx.scene.control.TreeView"); 1.59 + 1.60 +// create a subclass of JavaFX TreeItem class 1.61 +var LazyTreeItem = Java.extend(TreeItem); 1.62 + 1.63 +// lazily filling children of a directory LazyTreeItem 1.64 +function buildChildren(dir) { 1.65 + var children = FXCollections.observableArrayList(); 1.66 + var stream = Files.list(dir.toPath()); 1.67 + stream.forEach(function(path) { 1.68 + var file = path.toFile(); 1.69 + var item = file.isDirectory()? 1.70 + makeLazyTreeItem(file) : new TreeItem(file.name); 1.71 + children.add(item); 1.72 + }); 1.73 + stream.close(); 1.74 + return children; 1.75 +} 1.76 + 1.77 +// create an instance LazyTreeItem with override methods 1.78 +function makeLazyTreeItem(dir) { 1.79 + var item = new LazyTreeItem(dir.name) { 1.80 + expanded: false, 1.81 + isLeaf: function() false, 1.82 + getChildren: function() { 1.83 + if (! this.expanded) { 1.84 + // call super class (TreeItem) method 1.85 + Java.super(item).getChildren().setAll(buildChildren(dir)); 1.86 + this.expanded = true; 1.87 + } 1.88 + // call super class (TreeItem) method 1.89 + return Java.super(item).getChildren(); 1.90 + } 1.91 + } 1.92 + return item; 1.93 +} 1.94 + 1.95 +// JavaFX start method 1.96 +function start(stage) { 1.97 + stage.title = dir.absolutePath; 1.98 + var rootItem = makeLazyTreeItem(dir); 1.99 + rootItem.expanded = true; 1.100 + var tree = new TreeView(rootItem); 1.101 + stage.scene = new Scene(tree, 300, 450); 1.102 + stage.show(); 1.103 +}