boot-fx/src/main/java/net/java/html/boot/fx/FXBrowsers.java
author Jaroslav Tulach <jtulach@netbeans.org>
Thu, 17 Dec 2015 22:23:41 +0100
changeset 1037 e390a06dbfac
parent 1006 a0f79e32d526
permissions -rw-r--r--
#257171: FXBrowsers.runInBrowser needs to properly set BrwsrCtx
jaroslav@288
     1
/**
jaroslav@358
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
jaroslav@288
     3
 *
jaroslav@551
     4
 * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
jaroslav@288
     5
 *
jaroslav@358
     6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
jaroslav@358
     7
 * Other names may be trademarks of their respective owners.
jaroslav@288
     8
 *
jaroslav@358
     9
 * The contents of this file are subject to the terms of either the GNU
jaroslav@358
    10
 * General Public License Version 2 only ("GPL") or the Common
jaroslav@358
    11
 * Development and Distribution License("CDDL") (collectively, the
jaroslav@358
    12
 * "License"). You may not use this file except in compliance with the
jaroslav@358
    13
 * License. You can obtain a copy of the License at
jaroslav@358
    14
 * http://www.netbeans.org/cddl-gplv2.html
jaroslav@358
    15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
jaroslav@358
    16
 * specific language governing permissions and limitations under the
jaroslav@358
    17
 * License.  When distributing the software, include this License Header
jaroslav@358
    18
 * Notice in each file and include the License file at
jaroslav@358
    19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
jaroslav@358
    20
 * particular file as subject to the "Classpath" exception as provided
jaroslav@358
    21
 * by Oracle in the GPL Version 2 section of the License file that
jaroslav@358
    22
 * accompanied this code. If applicable, add the following below the
jaroslav@358
    23
 * License Header, with the fields enclosed by brackets [] replaced by
jaroslav@358
    24
 * your own identifying information:
jaroslav@358
    25
 * "Portions Copyrighted [year] [name of copyright owner]"
jaroslav@358
    26
 *
jaroslav@358
    27
 * Contributor(s):
jaroslav@358
    28
 *
jaroslav@358
    29
 * The Original Software is NetBeans. The Initial Developer of the Original
jaroslav@551
    30
 * Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved.
jaroslav@358
    31
 *
jaroslav@358
    32
 * If you wish your version of this file to be governed by only the CDDL
jaroslav@358
    33
 * or only the GPL Version 2, indicate your decision by adding
jaroslav@358
    34
 * "[Contributor] elects to include this software in this distribution
jaroslav@358
    35
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
jaroslav@358
    36
 * single choice of license, a recipient has the option to distribute
jaroslav@358
    37
 * your version of this file under either the CDDL, the GPL Version 2 or
jaroslav@358
    38
 * to extend the choice of license to its licensees as provided above.
jaroslav@358
    39
 * However, if you add GPL Version 2 code and therefore, elected the GPL
jaroslav@358
    40
 * Version 2 license, then the option applies only if the new code is
jaroslav@358
    41
 * made subject to such option by the copyright holder.
jaroslav@288
    42
 */
jaroslav@288
    43
package net.java.html.boot.fx;
jaroslav@288
    44
jaroslav@288
    45
import java.net.URL;
jtulach@656
    46
import javafx.application.Platform;
jaroslav@288
    47
import javafx.beans.value.ChangeListener;
jaroslav@288
    48
import javafx.beans.value.ObservableValue;
jaroslav@288
    49
import javafx.concurrent.Worker;
jaroslav@288
    50
import javafx.scene.web.WebView;
jtulach@656
    51
import net.java.html.BrwsrCtx;
jaroslav@288
    52
import net.java.html.boot.BrowserBuilder;
jaroslav@290
    53
import net.java.html.js.JavaScriptBody;
jaroslav@362
    54
import org.netbeans.html.boot.fx.AbstractFXPresenter;
jtulach@886
    55
import org.netbeans.html.context.spi.Contexts;
jtulach@886
    56
import org.netbeans.html.context.spi.Contexts.Id;
jaroslav@288
    57
jtulach@658
    58
/** Utility methods to use {@link WebView} and {@link JavaScriptBody} code
jtulach@658
    59
 * in existing <em>JavaFX</em> applications.
jtulach@658
    60
 * This class is for those who want to instantiate their own {@link WebView},
jtulach@658
    61
 * configure it manually, embed it into own <em>JavaFX</em>
jtulach@658
    62
 * application and based on other events in the application
jtulach@658
    63
 * {@link #runInBrowser(javafx.scene.web.WebView, java.lang.Runnable) re-execute code} 
jtulach@658
    64
 * inside of such {@link WebView}s.
jtulach@1006
    65
 * <p>
jtulach@658
    66
 * In case such detailed control is not necessary,
jtulach@1006
    67
 * consider using {@link BrowserBuilder}. Btw. when using the {@link BrowserBuilder}
jtulach@1006
    68
 * one can execute presenter in headless mode. Just specify: <code>
jtulach@1006
    69
 * {@link System}.{@link System#setProperty(java.lang.String, java.lang.String) setProperty}("fxpresenter.headless", "true");
jtulach@1006
    70
 * </code>
jtulach@1006
    71
 *
jaroslav@627
    72
 * 
jtulach@655
    73
 * @author Jaroslav Tulach
jaroslav@290
    74
 * @since 0.6
jaroslav@288
    75
 */
jaroslav@288
    76
public final class FXBrowsers {
jaroslav@288
    77
    private FXBrowsers() {
jaroslav@288
    78
    }
jaroslav@288
    79
    
jaroslav@542
    80
    /** Enables the Java/JavaScript bridge (that supports {@link JavaScriptBody} and co.)
jaroslav@290
    81
     * in the provided <code>webView</code>. This method returns 
jaroslav@290
    82
     * immediately. Once the support is active, it calls back specified
jaroslav@290
    83
     * method in <code>onPageLoad</code> class - the class can possibly be
jaroslav@290
    84
     * loaded by a different classloader (to enable replacement of
jaroslav@290
    85
     * methods with {@link JavaScriptBody} annotations with executable
jaroslav@290
    86
     * versions). The method <code>methodName</code> needs to be <code>public</code>
jaroslav@290
    87
     * (in a public class), <code>static</code> and take either no parameters
jaroslav@290
    88
     * or an array of {@link String}s.
jtulach@658
    89
     * <p>
jtulach@658
    90
     * This method sets {@link WebView#getUserData()} and {@link #runInBrowser(javafx.scene.web.WebView, java.lang.Runnable)}
jtulach@658
    91
     * relies on the value. Please don't alter it.
jtulach@886
    92
     * <p>
jtulach@886
    93
     * Since introduction of {@link Id technology identifiers} the 
jtulach@886
    94
     * provided <code>args</code> strings are also passed to the 
jtulach@886
    95
     * {@link BrwsrCtx context} when it is being 
jtulach@886
    96
     * {@link Contexts#newBuilder(java.lang.Object...) created}
jtulach@886
    97
     * and can influence the selection
jtulach@886
    98
     * of available technologies 
jtulach@886
    99
     * (like {@link org.netbeans.html.json.spi.Technology},
jtulach@886
   100
     * {@link org.netbeans.html.json.spi.Transfer} or
jtulach@886
   101
     * {@link org.netbeans.html.json.spi.WSTransfer}).
jaroslav@290
   102
     * 
jaroslav@290
   103
     * @param webView the instance of Web View to tweak
jaroslav@290
   104
     * @param url the URL of the HTML page to load into the view
jaroslav@290
   105
     * @param onPageLoad callback class with method <code>methodName</code>
jaroslav@290
   106
     * @param methodName the method to call when the page is loaded
jaroslav@290
   107
     * @param args arguments to pass to the <code>methodName</code> method
jaroslav@290
   108
     */
jaroslav@288
   109
    public static void load(
jaroslav@288
   110
        final WebView webView, final URL url, 
jaroslav@288
   111
        Class<?> onPageLoad, String methodName,
jaroslav@288
   112
        String... args
jaroslav@288
   113
    ) {
jtulach@886
   114
        Object[] context = new Object[args.length + 1];
jtulach@886
   115
        System.arraycopy(args, 0, context, 1, args.length);
jtulach@1037
   116
        final Load load = new Load(webView, null);
jtulach@1037
   117
        context[0] = load;
jtulach@886
   118
        BrowserBuilder.newBrowser(context).
jaroslav@288
   119
            loadPage(url.toExternalForm()).
jtulach@1037
   120
            loadFinished(load).
jaroslav@288
   121
            loadClass(onPageLoad).
jaroslav@288
   122
            invoke(methodName, args).
jaroslav@288
   123
            showAndWait();
jaroslav@288
   124
    }
jtulach@656
   125
    
jtulach@656
   126
    /** Enables the Java/JavaScript bridge (that supports {@link JavaScriptBody} and co.)
jtulach@656
   127
     * in the provided <code>webView</code>. This method returns 
jtulach@656
   128
     * immediately. Once the support is active, it calls back specified
jtulach@656
   129
     * method in <code>onPageLoad</code>'s run method. 
jtulach@656
   130
     * This is more convenient way to initialize the webview, 
jtulach@656
   131
     * but it requires one to make sure
jtulach@656
   132
     * all {@link JavaScriptBody} methods has been post-processed during
jtulach@656
   133
     * compilation and there will be no need to instantiate new classloader.
jtulach@658
   134
     * <p>
jtulach@658
   135
     * This method sets {@link WebView#getUserData()} and {@link #runInBrowser(javafx.scene.web.WebView, java.lang.Runnable)}
jtulach@658
   136
     * relies on the value. Please don't alter it.
jtulach@656
   137
     * 
jtulach@656
   138
     * @param webView the instance of Web View to tweak
jtulach@656
   139
     * @param url the URL of the HTML page to load into the view
jtulach@656
   140
     * @param onPageLoad callback to call when the page is ready
jtulach@656
   141
     * @since 0.8.1
jtulach@656
   142
     */
jtulach@656
   143
    public static void load(
jtulach@656
   144
        WebView webView, final URL url, Runnable onPageLoad
jtulach@656
   145
    ) {
jtulach@771
   146
        load(webView, url, onPageLoad, null);
jtulach@771
   147
    }
jtulach@771
   148
    
jtulach@771
   149
    /** Enables the Java/JavaScript bridge (that supports {@link JavaScriptBody} and co.)
jtulach@771
   150
     * in the provided <code>webView</code>. This method returns 
jtulach@889
   151
     * immediately. Once the support is active, it calls back {@link Runnable#run() run}
jtulach@889
   152
     * method in <code>onPageLoad</code>. 
jtulach@771
   153
     * This is more convenient way to initialize the webview, 
jtulach@771
   154
     * but it requires one to make sure
jtulach@771
   155
     * all {@link JavaScriptBody} methods has been post-processed during
jtulach@771
   156
     * compilation and there will be no need to instantiate new classloader.
jtulach@771
   157
     * <p>
jtulach@771
   158
     * This method sets {@link WebView#getUserData()} and {@link #runInBrowser(javafx.scene.web.WebView, java.lang.Runnable)}
jtulach@771
   159
     * relies on the value. Please don't alter it.
jtulach@771
   160
     * 
jtulach@771
   161
     * @param webView the instance of Web View to tweak
jtulach@771
   162
     * @param url the URL of the HTML page to load into the view
jtulach@771
   163
     * @param onPageLoad callback to call when the page is ready
jtulach@771
   164
     * @param loader the loader to use when constructing initial {@link BrwsrCtx} or <code>null</code>
jtulach@771
   165
     * @since 0.9
jtulach@771
   166
     */
jtulach@771
   167
    public static void load(
jtulach@771
   168
        WebView webView, final URL url, Runnable onPageLoad, ClassLoader loader
jtulach@771
   169
    ) {
jtulach@889
   170
        load(webView, url, onPageLoad, loader, new Object[0]);
jtulach@889
   171
    }
jtulach@889
   172
    
jtulach@889
   173
    /** Enables the Java/JavaScript bridge (that supports {@link JavaScriptBody} and co.)
jtulach@889
   174
     * in the provided <code>webView</code>. This method returns 
jtulach@889
   175
     * immediately. Once the support is active, it calls back {@link Runnable#run() run}
jtulach@889
   176
     * method in <code>onPageLoad</code>. 
jtulach@889
   177
     * This is more convenient way to initialize the webview, 
jtulach@889
   178
     * but it requires one to make sure
jtulach@889
   179
     * all {@link JavaScriptBody} methods has been post-processed during
jtulach@889
   180
     * compilation and there will be no need to instantiate new classloader.
jtulach@889
   181
     * <p>
jtulach@889
   182
     * This method sets {@link WebView#getUserData()} and {@link #runInBrowser(javafx.scene.web.WebView, java.lang.Runnable)}
jtulach@889
   183
     * relies on the value. Please don't alter it.
jtulach@889
   184
     * 
jtulach@889
   185
     * @param webView the instance of Web View to tweak
jtulach@889
   186
     * @param url the URL of the HTML page to load into the view
jtulach@889
   187
     * @param onPageLoad callback to call when the page is ready
jtulach@889
   188
     * @param loader the loader to use when constructing initial {@link BrwsrCtx} or <code>null</code>
jtulach@889
   189
     * @param context additonal configuration to pass to {@link BrowserBuilder#newBrowser(java.lang.Object...)}
jtulach@889
   190
     *   and {@link Contexts#newBuilder(java.lang.Object...)} factory methods 
jtulach@889
   191
     * @since 1.1
jtulach@889
   192
     */
jtulach@889
   193
    public static void load(
jtulach@889
   194
        WebView webView, final URL url, Runnable onPageLoad, ClassLoader loader,
jtulach@889
   195
        Object... context
jtulach@889
   196
    ) {
jtulach@889
   197
        Object[] newCtx = new Object[context.length + 1];
jtulach@889
   198
        System.arraycopy(context, 0, newCtx, 1, context.length);
jtulach@1037
   199
        final Load load = new Load(webView, onPageLoad);
jtulach@1037
   200
        newCtx[0] = load;
jtulach@889
   201
        BrowserBuilder.newBrowser(newCtx).
jtulach@656
   202
                loadPage(url.toExternalForm()).
jtulach@1037
   203
                loadFinished(load).
jtulach@771
   204
                classloader(loader).
jtulach@656
   205
                showAndWait();
jtulach@656
   206
    }
jtulach@656
   207
    
jtulach@656
   208
    /** Executes a code inside of provided {@link WebView}. This method
jaroslav@752
   209
     * associates the {@link BrwsrCtx execution context} with provided browser,
jtulach@658
   210
     * so the {@link JavaScriptBody} annotations know where to execute
jtulach@658
   211
     * their JavaScript bodies.
jtulach@658
   212
     * The code is going to be executed synchronously
jtulach@656
   213
     * in case {@link Platform#isFxApplicationThread()} returns <code>true</code>.
jtulach@656
   214
     * Otherwise this method returns immediately and the code is executed
jtulach@656
   215
     * later via {@link Platform#runLater(java.lang.Runnable)}.
jtulach@658
   216
     * <p>
jtulach@658
   217
     * This method relies on {@link WebView#getUserData()} being properly
jtulach@658
   218
     * provided by the <code>load</code> methods in this class.
jtulach@656
   219
     * 
jtulach@656
   220
     * @param webView the web view previously prepared by one of the <code>load</code>
jaroslav@752
   221
     *   methods in this class
jtulach@656
   222
     * @param code the code to execute
jtulach@656
   223
     * @throws IllegalArgumentException if the web view was not properly
jtulach@656
   224
     *   initialized
jtulach@656
   225
     * @see BrwsrCtx#execute(java.lang.Runnable) 
jtulach@656
   226
     * @since 0.8.1
jtulach@656
   227
     */
jtulach@656
   228
    public static void runInBrowser(WebView webView, Runnable code) {
jtulach@656
   229
        Object ud = webView.getUserData();
jtulach@656
   230
        if (!(ud instanceof Load)) {
jtulach@656
   231
            throw new IllegalArgumentException();
jtulach@656
   232
        }
jtulach@1037
   233
        ((Load)ud).ctx.execute(code);
jtulach@656
   234
    }
jtulach@656
   235
    
jtulach@1037
   236
    private static class Load extends AbstractFXPresenter implements Runnable {
jtulach@656
   237
        private final WebView webView;
jtulach@1037
   238
        private final Runnable myLoad;
jtulach@1037
   239
        private BrwsrCtx ctx;
jtulach@656
   240
jtulach@1037
   241
        public Load(WebView webView, Runnable onLoad) {
jtulach@1037
   242
            this.webView = webView;
jtulach@1037
   243
            this.myLoad = onLoad;
jtulach@656
   244
            webView.setUserData(this);
jtulach@1037
   245
        }
jtulach@1037
   246
jtulach@1037
   247
        public void run() {
jtulach@1037
   248
            ctx = BrwsrCtx.findDefault(Load.class);
jtulach@1037
   249
            if (myLoad != null) {
jtulach@1037
   250
                myLoad.run();
jtulach@1037
   251
            }
jtulach@656
   252
        }
jtulach@656
   253
        
jtulach@656
   254
        @Override
jtulach@656
   255
        protected void waitFinished() {
jtulach@656
   256
            // don't wait
jtulach@656
   257
        }
jtulach@656
   258
jtulach@656
   259
        @Override
jtulach@656
   260
        protected WebView findView(final URL resource) {
jtulach@656
   261
            final Worker<Void> w = webView.getEngine().getLoadWorker();
jtulach@656
   262
            w.stateProperty().addListener(new ChangeListener<Worker.State>() {
jtulach@656
   263
                private String previous;
jtulach@656
   264
jtulach@656
   265
                @Override
jtulach@656
   266
                public void changed(ObservableValue<? extends Worker.State> ov, Worker.State t, Worker.State newState) {
jtulach@656
   267
                    if (newState.equals(Worker.State.SUCCEEDED)) {
jtulach@656
   268
                        if (checkValid()) {
jtulach@656
   269
                            onPageLoad();
jtulach@656
   270
                        }
jtulach@656
   271
                    }
jtulach@656
   272
                    if (newState.equals(Worker.State.FAILED)) {
jtulach@656
   273
                        checkValid();
jtulach@656
   274
                        throw new IllegalStateException("Failed to load " + resource);
jtulach@656
   275
                    }
jtulach@656
   276
                }
jtulach@656
   277
jtulach@656
   278
                private boolean checkValid() {
jtulach@656
   279
                    final String crnt = webView.getEngine().getLocation();
jtulach@656
   280
                    if (previous != null && !previous.equals(crnt)) {
jtulach@656
   281
                        w.stateProperty().removeListener(this);
jtulach@656
   282
                        return false;
jtulach@656
   283
                    }
jtulach@656
   284
                    previous = crnt;
jtulach@656
   285
                    return true;
jtulach@656
   286
                }
jtulach@656
   287
            });
jtulach@656
   288
jtulach@656
   289
            return webView;
jtulach@656
   290
        }
jtulach@656
   291
    }
jtulach@656
   292
    
jaroslav@288
   293
}