Wed, 07 Oct 2015 10:01:40 -0700
Merge
aoqi@0 | 1 | #// Usage: jjs -fx filebrowser.js -- <start_dir> |
aoqi@0 | 2 | |
aoqi@0 | 3 | /* |
aoqi@0 | 4 | * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. |
attila@962 | 5 | * |
aoqi@0 | 6 | * Redistribution and use in source and binary forms, with or without |
aoqi@0 | 7 | * modification, are permitted provided that the following conditions |
aoqi@0 | 8 | * are met: |
attila@962 | 9 | * |
aoqi@0 | 10 | * - Redistributions of source code must retain the above copyright |
aoqi@0 | 11 | * notice, this list of conditions and the following disclaimer. |
attila@962 | 12 | * |
aoqi@0 | 13 | * - Redistributions in binary form must reproduce the above copyright |
aoqi@0 | 14 | * notice, this list of conditions and the following disclaimer in the |
aoqi@0 | 15 | * documentation and/or other materials provided with the distribution. |
attila@962 | 16 | * |
aoqi@0 | 17 | * - Neither the name of Oracle nor the names of its |
aoqi@0 | 18 | * contributors may be used to endorse or promote products derived |
aoqi@0 | 19 | * from this software without specific prior written permission. |
attila@962 | 20 | * |
aoqi@0 | 21 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
aoqi@0 | 22 | * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
aoqi@0 | 23 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
aoqi@0 | 24 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
aoqi@0 | 25 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
aoqi@0 | 26 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
aoqi@0 | 27 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
aoqi@0 | 28 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
aoqi@0 | 29 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
aoqi@0 | 30 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
aoqi@0 | 31 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
aoqi@0 | 32 | */ |
aoqi@0 | 33 | |
aoqi@0 | 34 | // Uses -fx and javafx TreeView to visualize directories |
aoqi@0 | 35 | if (!$OPTIONS._fx) { |
aoqi@0 | 36 | print("Usage: jjs -fx filebrowser.js -- <start_dir>"); |
aoqi@0 | 37 | exit(1); |
aoqi@0 | 38 | } |
aoqi@0 | 39 | |
aoqi@0 | 40 | // Java classes used |
aoqi@0 | 41 | var File = Java.type("java.io.File"); |
aoqi@0 | 42 | var Files = Java.type("java.nio.file.Files"); |
aoqi@0 | 43 | |
aoqi@0 | 44 | // check directory argument, if passed |
aoqi@0 | 45 | var dir = arguments.length > 0? new File(arguments[0]) : new File("."); |
aoqi@0 | 46 | if (! dir.isDirectory()) { |
aoqi@0 | 47 | print(dir + " is not a directory!"); |
aoqi@0 | 48 | exit(2); |
aoqi@0 | 49 | } |
aoqi@0 | 50 | |
aoqi@0 | 51 | // JavaFX classes used |
aoqi@0 | 52 | var FXCollections = Java.type("javafx.collections.FXCollections"); |
aoqi@0 | 53 | var Scene = Java.type("javafx.scene.Scene"); |
aoqi@0 | 54 | var TreeItem = Java.type("javafx.scene.control.TreeItem"); |
aoqi@0 | 55 | var TreeView = Java.type("javafx.scene.control.TreeView"); |
aoqi@0 | 56 | |
aoqi@0 | 57 | // create a subclass of JavaFX TreeItem class |
aoqi@0 | 58 | var LazyTreeItem = Java.extend(TreeItem); |
aoqi@0 | 59 | |
aoqi@0 | 60 | // lazily filling children of a directory LazyTreeItem |
aoqi@0 | 61 | function buildChildren(dir) { |
aoqi@0 | 62 | var children = FXCollections.observableArrayList(); |
aoqi@0 | 63 | var stream = Files.list(dir.toPath()); |
aoqi@0 | 64 | stream.forEach(function(path) { |
aoqi@0 | 65 | var file = path.toFile(); |
aoqi@0 | 66 | var item = file.isDirectory()? |
aoqi@0 | 67 | makeLazyTreeItem(file) : new TreeItem(file.name); |
aoqi@0 | 68 | children.add(item); |
aoqi@0 | 69 | }); |
aoqi@0 | 70 | stream.close(); |
aoqi@0 | 71 | return children; |
aoqi@0 | 72 | } |
aoqi@0 | 73 | |
aoqi@0 | 74 | // create an instance LazyTreeItem with override methods |
aoqi@0 | 75 | function makeLazyTreeItem(dir) { |
aoqi@0 | 76 | var item = new LazyTreeItem(dir.name) { |
aoqi@0 | 77 | expanded: false, |
aoqi@0 | 78 | isLeaf: function() false, |
aoqi@0 | 79 | getChildren: function() { |
aoqi@0 | 80 | if (! this.expanded) { |
aoqi@0 | 81 | // call super class (TreeItem) method |
aoqi@0 | 82 | Java.super(item).getChildren().setAll(buildChildren(dir)); |
aoqi@0 | 83 | this.expanded = true; |
aoqi@0 | 84 | } |
aoqi@0 | 85 | // call super class (TreeItem) method |
aoqi@0 | 86 | return Java.super(item).getChildren(); |
aoqi@0 | 87 | } |
aoqi@0 | 88 | } |
aoqi@0 | 89 | return item; |
aoqi@0 | 90 | } |
aoqi@0 | 91 | |
aoqi@0 | 92 | // JavaFX start method |
aoqi@0 | 93 | function start(stage) { |
aoqi@0 | 94 | stage.title = dir.absolutePath; |
aoqi@0 | 95 | var rootItem = makeLazyTreeItem(dir); |
aoqi@0 | 96 | rootItem.expanded = true; |
aoqi@0 | 97 | var tree = new TreeView(rootItem); |
aoqi@0 | 98 | stage.scene = new Scene(tree, 300, 450); |
aoqi@0 | 99 | stage.show(); |
aoqi@0 | 100 | } |