samples/filebrowser.js

Wed, 27 Apr 2016 01:36:41 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:36:41 +0800
changeset 0
b1a7da25b547
child 962
ac62e33a99b0
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/nashorn/
changeset: 1034:4b9cc65dd24d
tag: jdk8u25-b17

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

mercurial