nashorn/samples/browser_dom.js
author mlarsson
Fri, 20 Nov 2015 12:42:21 +0100
changeset 34252 59d76c40998a
parent 31924 243ebe5fe525
permissions -rw-r--r--
8143229: Replace the develop level with develop macros in Unified Logging Reviewed-by: brutisso, coleenp, dholmes, rprotacio
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
28123
b13d67aea450 8067420: BrowserJSObjectLinker should give priority to beans linker for property get/set
sundar
parents: 27973
diff changeset
     1
#// Usage: jjs -fx browser_dom.js
27973
ecc16e813691 8067136: BrowserJSObjectLinker does not handle call on JSObjects
sundar
parents:
diff changeset
     2
ecc16e813691 8067136: BrowserJSObjectLinker does not handle call on JSObjects
sundar
parents:
diff changeset
     3
/*
ecc16e813691 8067136: BrowserJSObjectLinker does not handle call on JSObjects
sundar
parents:
diff changeset
     4
 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
ecc16e813691 8067136: BrowserJSObjectLinker does not handle call on JSObjects
sundar
parents:
diff changeset
     5
 *
ecc16e813691 8067136: BrowserJSObjectLinker does not handle call on JSObjects
sundar
parents:
diff changeset
     6
 * Redistribution and use in source and binary forms, with or without
ecc16e813691 8067136: BrowserJSObjectLinker does not handle call on JSObjects
sundar
parents:
diff changeset
     7
 * modification, are permitted provided that the following conditions
ecc16e813691 8067136: BrowserJSObjectLinker does not handle call on JSObjects
sundar
parents:
diff changeset
     8
 * are met:
ecc16e813691 8067136: BrowserJSObjectLinker does not handle call on JSObjects
sundar
parents:
diff changeset
     9
 *
ecc16e813691 8067136: BrowserJSObjectLinker does not handle call on JSObjects
sundar
parents:
diff changeset
    10
 *   - Redistributions of source code must retain the above copyright
ecc16e813691 8067136: BrowserJSObjectLinker does not handle call on JSObjects
sundar
parents:
diff changeset
    11
 *     notice, this list of conditions and the following disclaimer.
ecc16e813691 8067136: BrowserJSObjectLinker does not handle call on JSObjects
sundar
parents:
diff changeset
    12
 *
ecc16e813691 8067136: BrowserJSObjectLinker does not handle call on JSObjects
sundar
parents:
diff changeset
    13
 *   - Redistributions in binary form must reproduce the above copyright
ecc16e813691 8067136: BrowserJSObjectLinker does not handle call on JSObjects
sundar
parents:
diff changeset
    14
 *     notice, this list of conditions and the following disclaimer in the
ecc16e813691 8067136: BrowserJSObjectLinker does not handle call on JSObjects
sundar
parents:
diff changeset
    15
 *     documentation and/or other materials provided with the distribution.
ecc16e813691 8067136: BrowserJSObjectLinker does not handle call on JSObjects
sundar
parents:
diff changeset
    16
 *
ecc16e813691 8067136: BrowserJSObjectLinker does not handle call on JSObjects
sundar
parents:
diff changeset
    17
 *   - Neither the name of Oracle nor the names of its
ecc16e813691 8067136: BrowserJSObjectLinker does not handle call on JSObjects
sundar
parents:
diff changeset
    18
 *     contributors may be used to endorse or promote products derived
ecc16e813691 8067136: BrowserJSObjectLinker does not handle call on JSObjects
sundar
parents:
diff changeset
    19
 *     from this software without specific prior written permission.
ecc16e813691 8067136: BrowserJSObjectLinker does not handle call on JSObjects
sundar
parents:
diff changeset
    20
 *
ecc16e813691 8067136: BrowserJSObjectLinker does not handle call on JSObjects
sundar
parents:
diff changeset
    21
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
ecc16e813691 8067136: BrowserJSObjectLinker does not handle call on JSObjects
sundar
parents:
diff changeset
    22
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
ecc16e813691 8067136: BrowserJSObjectLinker does not handle call on JSObjects
sundar
parents:
diff changeset
    23
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
ecc16e813691 8067136: BrowserJSObjectLinker does not handle call on JSObjects
sundar
parents:
diff changeset
    24
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
ecc16e813691 8067136: BrowserJSObjectLinker does not handle call on JSObjects
sundar
parents:
diff changeset
    25
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
ecc16e813691 8067136: BrowserJSObjectLinker does not handle call on JSObjects
sundar
parents:
diff changeset
    26
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
ecc16e813691 8067136: BrowserJSObjectLinker does not handle call on JSObjects
sundar
parents:
diff changeset
    27
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
ecc16e813691 8067136: BrowserJSObjectLinker does not handle call on JSObjects
sundar
parents:
diff changeset
    28
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
ecc16e813691 8067136: BrowserJSObjectLinker does not handle call on JSObjects
sundar
parents:
diff changeset
    29
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
ecc16e813691 8067136: BrowserJSObjectLinker does not handle call on JSObjects
sundar
parents:
diff changeset
    30
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
ecc16e813691 8067136: BrowserJSObjectLinker does not handle call on JSObjects
sundar
parents:
diff changeset
    31
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
ecc16e813691 8067136: BrowserJSObjectLinker does not handle call on JSObjects
sundar
parents:
diff changeset
    32
 */
ecc16e813691 8067136: BrowserJSObjectLinker does not handle call on JSObjects
sundar
parents:
diff changeset
    33
ecc16e813691 8067136: BrowserJSObjectLinker does not handle call on JSObjects
sundar
parents:
diff changeset
    34
if (!$OPTIONS._fx) {
28123
b13d67aea450 8067420: BrowserJSObjectLinker should give priority to beans linker for property get/set
sundar
parents: 27973
diff changeset
    35
    print("Usage: jjs -fx browser_dom.js");
27973
ecc16e813691 8067136: BrowserJSObjectLinker does not handle call on JSObjects
sundar
parents:
diff changeset
    36
    exit(1);
ecc16e813691 8067136: BrowserJSObjectLinker does not handle call on JSObjects
sundar
parents:
diff changeset
    37
}
ecc16e813691 8067136: BrowserJSObjectLinker does not handle call on JSObjects
sundar
parents:
diff changeset
    38
ecc16e813691 8067136: BrowserJSObjectLinker does not handle call on JSObjects
sundar
parents:
diff changeset
    39
// JavaFX classes used
ecc16e813691 8067136: BrowserJSObjectLinker does not handle call on JSObjects
sundar
parents:
diff changeset
    40
var ChangeListener = Java.type("javafx.beans.value.ChangeListener");
ecc16e813691 8067136: BrowserJSObjectLinker does not handle call on JSObjects
sundar
parents:
diff changeset
    41
var Scene     = Java.type("javafx.scene.Scene");
ecc16e813691 8067136: BrowserJSObjectLinker does not handle call on JSObjects
sundar
parents:
diff changeset
    42
var WebView   = Java.type("javafx.scene.web.WebView");
ecc16e813691 8067136: BrowserJSObjectLinker does not handle call on JSObjects
sundar
parents:
diff changeset
    43
ecc16e813691 8067136: BrowserJSObjectLinker does not handle call on JSObjects
sundar
parents:
diff changeset
    44
// JavaFX start method
ecc16e813691 8067136: BrowserJSObjectLinker does not handle call on JSObjects
sundar
parents:
diff changeset
    45
function start(stage) {
31924
243ebe5fe525 8132305: fix incorrect title assignment in Nashorn JavaFX samples
mhaupt
parents: 28126
diff changeset
    46
    stage.title = "Web View";
27973
ecc16e813691 8067136: BrowserJSObjectLinker does not handle call on JSObjects
sundar
parents:
diff changeset
    47
    var wv = new WebView();
ecc16e813691 8067136: BrowserJSObjectLinker does not handle call on JSObjects
sundar
parents:
diff changeset
    48
    wv.engine.loadContent(<<EOF
ecc16e813691 8067136: BrowserJSObjectLinker does not handle call on JSObjects
sundar
parents:
diff changeset
    49
<html>
ecc16e813691 8067136: BrowserJSObjectLinker does not handle call on JSObjects
sundar
parents:
diff changeset
    50
<head>
ecc16e813691 8067136: BrowserJSObjectLinker does not handle call on JSObjects
sundar
parents:
diff changeset
    51
<title>
ecc16e813691 8067136: BrowserJSObjectLinker does not handle call on JSObjects
sundar
parents:
diff changeset
    52
This is the title
ecc16e813691 8067136: BrowserJSObjectLinker does not handle call on JSObjects
sundar
parents:
diff changeset
    53
</title>
ecc16e813691 8067136: BrowserJSObjectLinker does not handle call on JSObjects
sundar
parents:
diff changeset
    54
<script>
ecc16e813691 8067136: BrowserJSObjectLinker does not handle call on JSObjects
sundar
parents:
diff changeset
    55
// click count for OK button
ecc16e813691 8067136: BrowserJSObjectLinker does not handle call on JSObjects
sundar
parents:
diff changeset
    56
var okCount = 0;
ecc16e813691 8067136: BrowserJSObjectLinker does not handle call on JSObjects
sundar
parents:
diff changeset
    57
</script>
ecc16e813691 8067136: BrowserJSObjectLinker does not handle call on JSObjects
sundar
parents:
diff changeset
    58
</head>
ecc16e813691 8067136: BrowserJSObjectLinker does not handle call on JSObjects
sundar
parents:
diff changeset
    59
<body>
ecc16e813691 8067136: BrowserJSObjectLinker does not handle call on JSObjects
sundar
parents:
diff changeset
    60
Button from the input html<br>
ecc16e813691 8067136: BrowserJSObjectLinker does not handle call on JSObjects
sundar
parents:
diff changeset
    61
<button type="button" onclick="okCount++">OK</button><br>
ecc16e813691 8067136: BrowserJSObjectLinker does not handle call on JSObjects
sundar
parents:
diff changeset
    62
</body>
ecc16e813691 8067136: BrowserJSObjectLinker does not handle call on JSObjects
sundar
parents:
diff changeset
    63
</html>
ecc16e813691 8067136: BrowserJSObjectLinker does not handle call on JSObjects
sundar
parents:
diff changeset
    64
EOF, "text/html");
ecc16e813691 8067136: BrowserJSObjectLinker does not handle call on JSObjects
sundar
parents:
diff changeset
    65
ecc16e813691 8067136: BrowserJSObjectLinker does not handle call on JSObjects
sundar
parents:
diff changeset
    66
    // attach onload handler
ecc16e813691 8067136: BrowserJSObjectLinker does not handle call on JSObjects
sundar
parents:
diff changeset
    67
    wv.engine.loadWorker.stateProperty().addListener(
ecc16e813691 8067136: BrowserJSObjectLinker does not handle call on JSObjects
sundar
parents:
diff changeset
    68
        new ChangeListener() {
ecc16e813691 8067136: BrowserJSObjectLinker does not handle call on JSObjects
sundar
parents:
diff changeset
    69
            changed: function() {
ecc16e813691 8067136: BrowserJSObjectLinker does not handle call on JSObjects
sundar
parents:
diff changeset
    70
               // DOM document element
ecc16e813691 8067136: BrowserJSObjectLinker does not handle call on JSObjects
sundar
parents:
diff changeset
    71
               var document = wv.engine.document;
ecc16e813691 8067136: BrowserJSObjectLinker does not handle call on JSObjects
sundar
parents:
diff changeset
    72
               // DOM manipulation
ecc16e813691 8067136: BrowserJSObjectLinker does not handle call on JSObjects
sundar
parents:
diff changeset
    73
               var btn = document.createElement("button");
ecc16e813691 8067136: BrowserJSObjectLinker does not handle call on JSObjects
sundar
parents:
diff changeset
    74
               var n = 0;
ecc16e813691 8067136: BrowserJSObjectLinker does not handle call on JSObjects
sundar
parents:
diff changeset
    75
               // attach a button handler - nashorn function!
28123
b13d67aea450 8067420: BrowserJSObjectLinker should give priority to beans linker for property get/set
sundar
parents: 27973
diff changeset
    76
               btn.onclick = function() {
27973
ecc16e813691 8067136: BrowserJSObjectLinker does not handle call on JSObjects
sundar
parents:
diff changeset
    77
                   n++; print("You clicked " + n + " time(s)");
ecc16e813691 8067136: BrowserJSObjectLinker does not handle call on JSObjects
sundar
parents:
diff changeset
    78
                   print("you clicked OK " + wv.engine.executeScript("okCount"));
28123
b13d67aea450 8067420: BrowserJSObjectLinker should give priority to beans linker for property get/set
sundar
parents: 27973
diff changeset
    79
               };
27973
ecc16e813691 8067136: BrowserJSObjectLinker does not handle call on JSObjects
sundar
parents:
diff changeset
    80
               // attach text to button
ecc16e813691 8067136: BrowserJSObjectLinker does not handle call on JSObjects
sundar
parents:
diff changeset
    81
               var t = document.createTextNode("Click Me!"); 
ecc16e813691 8067136: BrowserJSObjectLinker does not handle call on JSObjects
sundar
parents:
diff changeset
    82
               btn.appendChild(t);
ecc16e813691 8067136: BrowserJSObjectLinker does not handle call on JSObjects
sundar
parents:
diff changeset
    83
               // attach button to the document
ecc16e813691 8067136: BrowserJSObjectLinker does not handle call on JSObjects
sundar
parents:
diff changeset
    84
               document.body.appendChild(btn); 
ecc16e813691 8067136: BrowserJSObjectLinker does not handle call on JSObjects
sundar
parents:
diff changeset
    85
           }
ecc16e813691 8067136: BrowserJSObjectLinker does not handle call on JSObjects
sundar
parents:
diff changeset
    86
        }
ecc16e813691 8067136: BrowserJSObjectLinker does not handle call on JSObjects
sundar
parents:
diff changeset
    87
    );
ecc16e813691 8067136: BrowserJSObjectLinker does not handle call on JSObjects
sundar
parents:
diff changeset
    88
    stage.scene = new Scene(wv, 750, 500);
ecc16e813691 8067136: BrowserJSObjectLinker does not handle call on JSObjects
sundar
parents:
diff changeset
    89
    stage.show();
ecc16e813691 8067136: BrowserJSObjectLinker does not handle call on JSObjects
sundar
parents:
diff changeset
    90
}