samples/browser_dom.js

Wed, 18 Oct 2017 09:15:26 -0700

author
asaha
date
Wed, 18 Oct 2017 09:15:26 -0700
changeset 2196
c21cf096431b
parent 1371
a8c536d1d3e0
permissions
-rw-r--r--

Added tag jdk8u161-b03 for changeset e33ecc88dd63

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

mercurial