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