2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
6 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7 * Other names may be trademarks of their respective owners.
9 * The contents of this file are subject to the terms of either the GNU
10 * General Public License Version 2 only ("GPL") or the Common
11 * Development and Distribution License("CDDL") (collectively, the
12 * "License"). You may not use this file except in compliance with the
13 * License. You can obtain a copy of the License at
14 * http://www.netbeans.org/cddl-gplv2.html
15 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16 * specific language governing permissions and limitations under the
17 * License. When distributing the software, include this License Header
18 * Notice in each file and include the License file at
19 * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
20 * particular file as subject to the "Classpath" exception as provided
21 * by Oracle in the GPL Version 2 section of the License file that
22 * accompanied this code. If applicable, add the following below the
23 * License Header, with the fields enclosed by brackets [] replaced by
24 * your own identifying information:
25 * "Portions Copyrighted [year] [name of copyright owner]"
29 * The Original Software is NetBeans. The Initial Developer of the Original
30 * Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved.
32 * If you wish your version of this file to be governed by only the CDDL
33 * or only the GPL Version 2, indicate your decision by adding
34 * "[Contributor] elects to include this software in this distribution
35 * under the [CDDL or GPL Version 2] license." If you do not indicate a
36 * single choice of license, a recipient has the option to distribute
37 * your version of this file under either the CDDL, the GPL Version 2 or
38 * to extend the choice of license to its licensees as provided above.
39 * However, if you add GPL Version 2 code and therefore, elected the GPL
40 * Version 2 license, then the option applies only if the new code is
41 * made subject to such option by the copyright holder.
43 package net.java.html.boot.fx;
46 import javafx.application.Platform;
47 import javafx.beans.value.ChangeListener;
48 import javafx.beans.value.ObservableValue;
49 import javafx.concurrent.Worker;
50 import javafx.scene.web.WebView;
51 import net.java.html.BrwsrCtx;
52 import net.java.html.boot.BrowserBuilder;
53 import net.java.html.js.JavaScriptBody;
54 import org.netbeans.html.boot.fx.AbstractFXPresenter;
56 /** Utility methods to use {@link WebView} and {@link JavaScriptBody} code
57 * in existing <em>JavaFX</em> applications.
58 * This class is for those who want to instantiate their own {@link WebView},
59 * configure it manually, embed it into own <em>JavaFX</em>
60 * application and based on other events in the application
61 * {@link #runInBrowser(javafx.scene.web.WebView, java.lang.Runnable) re-execute code}
62 * inside of such {@link WebView}s.
63 * In case such detailed control is not necessary,
64 * consider using {@link BrowserBuilder}.
66 * @author Jaroslav Tulach
69 public final class FXBrowsers {
70 private FXBrowsers() {
73 /** Enables the Java/JavaScript bridge (that supports {@link JavaScriptBody} and co.)
74 * in the provided <code>webView</code>. This method returns
75 * immediately. Once the support is active, it calls back specified
76 * method in <code>onPageLoad</code> class - the class can possibly be
77 * loaded by a different classloader (to enable replacement of
78 * methods with {@link JavaScriptBody} annotations with executable
79 * versions). The method <code>methodName</code> needs to be <code>public</code>
80 * (in a public class), <code>static</code> and take either no parameters
81 * or an array of {@link String}s.
83 * This method sets {@link WebView#getUserData()} and {@link #runInBrowser(javafx.scene.web.WebView, java.lang.Runnable)}
84 * relies on the value. Please don't alter it.
86 * @param webView the instance of Web View to tweak
87 * @param url the URL of the HTML page to load into the view
88 * @param onPageLoad callback class with method <code>methodName</code>
89 * @param methodName the method to call when the page is loaded
90 * @param args arguments to pass to the <code>methodName</code> method
92 public static void load(
93 final WebView webView, final URL url,
94 Class<?> onPageLoad, String methodName,
97 BrowserBuilder.newBrowser(new Load(webView)).
98 loadPage(url.toExternalForm()).
99 loadClass(onPageLoad).
100 invoke(methodName, args).
104 /** Enables the Java/JavaScript bridge (that supports {@link JavaScriptBody} and co.)
105 * in the provided <code>webView</code>. This method returns
106 * immediately. Once the support is active, it calls back specified
107 * method in <code>onPageLoad</code>'s run method.
108 * This is more convenient way to initialize the webview,
109 * but it requires one to make sure
110 * all {@link JavaScriptBody} methods has been post-processed during
111 * compilation and there will be no need to instantiate new classloader.
113 * This method sets {@link WebView#getUserData()} and {@link #runInBrowser(javafx.scene.web.WebView, java.lang.Runnable)}
114 * relies on the value. Please don't alter it.
116 * @param webView the instance of Web View to tweak
117 * @param url the URL of the HTML page to load into the view
118 * @param onPageLoad callback to call when the page is ready
121 public static void load(
122 WebView webView, final URL url, Runnable onPageLoad
124 load(webView, url, onPageLoad, null);
127 /** Enables the Java/JavaScript bridge (that supports {@link JavaScriptBody} and co.)
128 * in the provided <code>webView</code>. This method returns
129 * immediately. Once the support is active, it calls back specified
130 * method in <code>onPageLoad</code>'s run method.
131 * This is more convenient way to initialize the webview,
132 * but it requires one to make sure
133 * all {@link JavaScriptBody} methods has been post-processed during
134 * compilation and there will be no need to instantiate new classloader.
136 * This method sets {@link WebView#getUserData()} and {@link #runInBrowser(javafx.scene.web.WebView, java.lang.Runnable)}
137 * relies on the value. Please don't alter it.
139 * @param webView the instance of Web View to tweak
140 * @param url the URL of the HTML page to load into the view
141 * @param onPageLoad callback to call when the page is ready
142 * @param loader the loader to use when constructing initial {@link BrwsrCtx} or <code>null</code>
145 public static void load(
146 WebView webView, final URL url, Runnable onPageLoad, ClassLoader loader
148 BrowserBuilder.newBrowser(new Load(webView)).
149 loadPage(url.toExternalForm()).
150 loadFinished(onPageLoad).
155 /** Executes a code inside of provided {@link WebView}. This method
156 * associates the {@link BrwsrCtx execution context} with provided browser,
157 * so the {@link JavaScriptBody} annotations know where to execute
158 * their JavaScript bodies.
159 * The code is going to be executed synchronously
160 * in case {@link Platform#isFxApplicationThread()} returns <code>true</code>.
161 * Otherwise this method returns immediately and the code is executed
162 * later via {@link Platform#runLater(java.lang.Runnable)}.
164 * This method relies on {@link WebView#getUserData()} being properly
165 * provided by the <code>load</code> methods in this class.
167 * @param webView the web view previously prepared by one of the <code>load</code>
168 * methods in this class
169 * @param code the code to execute
170 * @throws IllegalArgumentException if the web view was not properly
172 * @see BrwsrCtx#execute(java.lang.Runnable)
175 public static void runInBrowser(WebView webView, Runnable code) {
176 Object ud = webView.getUserData();
177 if (!(ud instanceof Load)) {
178 throw new IllegalArgumentException();
180 ((Load)ud).execute(code);
183 private static class Load extends AbstractFXPresenter {
184 private final WebView webView;
186 public Load(WebView webView) {
187 webView.setUserData(this);
188 this.webView = webView;
192 protected void waitFinished() {
197 protected WebView findView(final URL resource) {
198 final Worker<Void> w = webView.getEngine().getLoadWorker();
199 w.stateProperty().addListener(new ChangeListener<Worker.State>() {
200 private String previous;
203 public void changed(ObservableValue<? extends Worker.State> ov, Worker.State t, Worker.State newState) {
204 if (newState.equals(Worker.State.SUCCEEDED)) {
209 if (newState.equals(Worker.State.FAILED)) {
211 throw new IllegalStateException("Failed to load " + resource);
215 private boolean checkValid() {
216 final String crnt = webView.getEngine().getLocation();
217 if (previous != null && !previous.equals(crnt)) {
218 w.stateProperty().removeListener(this);