boot-fx/src/main/java/net/java/html/boot/fx/FXBrowsers.java
author Jaroslav Tulach <jtulach@netbeans.org>
Thu, 04 Dec 2014 09:21:55 +0100
changeset 886 88d62267a0b5
parent 771 ee3614350fc8
child 889 548982033730
permissions -rw-r--r--
#248918: Introducing technology identifiers
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@658
    65
 * In case such detailed control is not necessary,
jaroslav@627
    66
 * consider using {@link BrowserBuilder}.
jaroslav@627
    67
 * 
jtulach@655
    68
 * @author Jaroslav Tulach
jaroslav@290
    69
 * @since 0.6
jaroslav@288
    70
 */
jaroslav@288
    71
public final class FXBrowsers {
jaroslav@288
    72
    private FXBrowsers() {
jaroslav@288
    73
    }
jaroslav@288
    74
    
jaroslav@542
    75
    /** Enables the Java/JavaScript bridge (that supports {@link JavaScriptBody} and co.)
jaroslav@290
    76
     * in the provided <code>webView</code>. This method returns 
jaroslav@290
    77
     * immediately. Once the support is active, it calls back specified
jaroslav@290
    78
     * method in <code>onPageLoad</code> class - the class can possibly be
jaroslav@290
    79
     * loaded by a different classloader (to enable replacement of
jaroslav@290
    80
     * methods with {@link JavaScriptBody} annotations with executable
jaroslav@290
    81
     * versions). The method <code>methodName</code> needs to be <code>public</code>
jaroslav@290
    82
     * (in a public class), <code>static</code> and take either no parameters
jaroslav@290
    83
     * or an array of {@link String}s.
jtulach@658
    84
     * <p>
jtulach@658
    85
     * This method sets {@link WebView#getUserData()} and {@link #runInBrowser(javafx.scene.web.WebView, java.lang.Runnable)}
jtulach@658
    86
     * relies on the value. Please don't alter it.
jtulach@886
    87
     * <p>
jtulach@886
    88
     * Since introduction of {@link Id technology identifiers} the 
jtulach@886
    89
     * provided <code>args</code> strings are also passed to the 
jtulach@886
    90
     * {@link BrwsrCtx context} when it is being 
jtulach@886
    91
     * {@link Contexts#newBuilder(java.lang.Object...) created}
jtulach@886
    92
     * and can influence the selection
jtulach@886
    93
     * of available technologies 
jtulach@886
    94
     * (like {@link org.netbeans.html.json.spi.Technology},
jtulach@886
    95
     * {@link org.netbeans.html.json.spi.Transfer} or
jtulach@886
    96
     * {@link org.netbeans.html.json.spi.WSTransfer}).
jaroslav@290
    97
     * 
jaroslav@290
    98
     * @param webView the instance of Web View to tweak
jaroslav@290
    99
     * @param url the URL of the HTML page to load into the view
jaroslav@290
   100
     * @param onPageLoad callback class with method <code>methodName</code>
jaroslav@290
   101
     * @param methodName the method to call when the page is loaded
jaroslav@290
   102
     * @param args arguments to pass to the <code>methodName</code> method
jaroslav@290
   103
     */
jaroslav@288
   104
    public static void load(
jaroslav@288
   105
        final WebView webView, final URL url, 
jaroslav@288
   106
        Class<?> onPageLoad, String methodName,
jaroslav@288
   107
        String... args
jaroslav@288
   108
    ) {
jtulach@886
   109
        Object[] context = new Object[args.length + 1];
jtulach@886
   110
        System.arraycopy(args, 0, context, 1, args.length);
jtulach@886
   111
        context[0] = new Load(webView);
jtulach@886
   112
        BrowserBuilder.newBrowser(context).
jaroslav@288
   113
            loadPage(url.toExternalForm()).
jaroslav@288
   114
            loadClass(onPageLoad).
jaroslav@288
   115
            invoke(methodName, args).
jaroslav@288
   116
            showAndWait();
jaroslav@288
   117
    }
jtulach@656
   118
    
jtulach@656
   119
    /** Enables the Java/JavaScript bridge (that supports {@link JavaScriptBody} and co.)
jtulach@656
   120
     * in the provided <code>webView</code>. This method returns 
jtulach@656
   121
     * immediately. Once the support is active, it calls back specified
jtulach@656
   122
     * method in <code>onPageLoad</code>'s run method. 
jtulach@656
   123
     * This is more convenient way to initialize the webview, 
jtulach@656
   124
     * but it requires one to make sure
jtulach@656
   125
     * all {@link JavaScriptBody} methods has been post-processed during
jtulach@656
   126
     * compilation and there will be no need to instantiate new classloader.
jtulach@658
   127
     * <p>
jtulach@658
   128
     * This method sets {@link WebView#getUserData()} and {@link #runInBrowser(javafx.scene.web.WebView, java.lang.Runnable)}
jtulach@658
   129
     * relies on the value. Please don't alter it.
jtulach@656
   130
     * 
jtulach@656
   131
     * @param webView the instance of Web View to tweak
jtulach@656
   132
     * @param url the URL of the HTML page to load into the view
jtulach@656
   133
     * @param onPageLoad callback to call when the page is ready
jtulach@656
   134
     * @since 0.8.1
jtulach@656
   135
     */
jtulach@656
   136
    public static void load(
jtulach@656
   137
        WebView webView, final URL url, Runnable onPageLoad
jtulach@656
   138
    ) {
jtulach@771
   139
        load(webView, url, onPageLoad, null);
jtulach@771
   140
    }
jtulach@771
   141
    
jtulach@771
   142
    /** Enables the Java/JavaScript bridge (that supports {@link JavaScriptBody} and co.)
jtulach@771
   143
     * in the provided <code>webView</code>. This method returns 
jtulach@771
   144
     * immediately. Once the support is active, it calls back specified
jtulach@771
   145
     * method in <code>onPageLoad</code>'s run method. 
jtulach@771
   146
     * This is more convenient way to initialize the webview, 
jtulach@771
   147
     * but it requires one to make sure
jtulach@771
   148
     * all {@link JavaScriptBody} methods has been post-processed during
jtulach@771
   149
     * compilation and there will be no need to instantiate new classloader.
jtulach@771
   150
     * <p>
jtulach@771
   151
     * This method sets {@link WebView#getUserData()} and {@link #runInBrowser(javafx.scene.web.WebView, java.lang.Runnable)}
jtulach@771
   152
     * relies on the value. Please don't alter it.
jtulach@771
   153
     * 
jtulach@771
   154
     * @param webView the instance of Web View to tweak
jtulach@771
   155
     * @param url the URL of the HTML page to load into the view
jtulach@771
   156
     * @param onPageLoad callback to call when the page is ready
jtulach@771
   157
     * @param loader the loader to use when constructing initial {@link BrwsrCtx} or <code>null</code>
jtulach@771
   158
     * @since 0.9
jtulach@771
   159
     */
jtulach@771
   160
    public static void load(
jtulach@771
   161
        WebView webView, final URL url, Runnable onPageLoad, ClassLoader loader
jtulach@771
   162
    ) {
jtulach@656
   163
        BrowserBuilder.newBrowser(new Load(webView)).
jtulach@656
   164
                loadPage(url.toExternalForm()).
jtulach@656
   165
                loadFinished(onPageLoad).
jtulach@771
   166
                classloader(loader).
jtulach@656
   167
                showAndWait();
jtulach@656
   168
    }
jtulach@656
   169
    
jtulach@656
   170
    /** Executes a code inside of provided {@link WebView}. This method
jaroslav@752
   171
     * associates the {@link BrwsrCtx execution context} with provided browser,
jtulach@658
   172
     * so the {@link JavaScriptBody} annotations know where to execute
jtulach@658
   173
     * their JavaScript bodies.
jtulach@658
   174
     * The code is going to be executed synchronously
jtulach@656
   175
     * in case {@link Platform#isFxApplicationThread()} returns <code>true</code>.
jtulach@656
   176
     * Otherwise this method returns immediately and the code is executed
jtulach@656
   177
     * later via {@link Platform#runLater(java.lang.Runnable)}.
jtulach@658
   178
     * <p>
jtulach@658
   179
     * This method relies on {@link WebView#getUserData()} being properly
jtulach@658
   180
     * provided by the <code>load</code> methods in this class.
jtulach@656
   181
     * 
jtulach@656
   182
     * @param webView the web view previously prepared by one of the <code>load</code>
jaroslav@752
   183
     *   methods in this class
jtulach@656
   184
     * @param code the code to execute
jtulach@656
   185
     * @throws IllegalArgumentException if the web view was not properly
jtulach@656
   186
     *   initialized
jtulach@656
   187
     * @see BrwsrCtx#execute(java.lang.Runnable) 
jtulach@656
   188
     * @since 0.8.1
jtulach@656
   189
     */
jtulach@656
   190
    public static void runInBrowser(WebView webView, Runnable code) {
jtulach@656
   191
        Object ud = webView.getUserData();
jtulach@656
   192
        if (!(ud instanceof Load)) {
jtulach@656
   193
            throw new IllegalArgumentException();
jtulach@656
   194
        }
jtulach@656
   195
        ((Load)ud).execute(code);
jtulach@656
   196
    }
jtulach@656
   197
    
jtulach@656
   198
    private static class Load extends AbstractFXPresenter {
jtulach@656
   199
        private final WebView webView;
jtulach@656
   200
jtulach@656
   201
        public Load(WebView webView) {
jtulach@656
   202
            webView.setUserData(this);
jtulach@656
   203
            this.webView = webView;
jtulach@656
   204
        }
jtulach@656
   205
        
jtulach@656
   206
        @Override
jtulach@656
   207
        protected void waitFinished() {
jtulach@656
   208
            // don't wait
jtulach@656
   209
        }
jtulach@656
   210
jtulach@656
   211
        @Override
jtulach@656
   212
        protected WebView findView(final URL resource) {
jtulach@656
   213
            final Worker<Void> w = webView.getEngine().getLoadWorker();
jtulach@656
   214
            w.stateProperty().addListener(new ChangeListener<Worker.State>() {
jtulach@656
   215
                private String previous;
jtulach@656
   216
jtulach@656
   217
                @Override
jtulach@656
   218
                public void changed(ObservableValue<? extends Worker.State> ov, Worker.State t, Worker.State newState) {
jtulach@656
   219
                    if (newState.equals(Worker.State.SUCCEEDED)) {
jtulach@656
   220
                        if (checkValid()) {
jtulach@656
   221
                            onPageLoad();
jtulach@656
   222
                        }
jtulach@656
   223
                    }
jtulach@656
   224
                    if (newState.equals(Worker.State.FAILED)) {
jtulach@656
   225
                        checkValid();
jtulach@656
   226
                        throw new IllegalStateException("Failed to load " + resource);
jtulach@656
   227
                    }
jtulach@656
   228
                }
jtulach@656
   229
jtulach@656
   230
                private boolean checkValid() {
jtulach@656
   231
                    final String crnt = webView.getEngine().getLocation();
jtulach@656
   232
                    if (previous != null && !previous.equals(crnt)) {
jtulach@656
   233
                        w.stateProperty().removeListener(this);
jtulach@656
   234
                        return false;
jtulach@656
   235
                    }
jtulach@656
   236
                    previous = crnt;
jtulach@656
   237
                    return true;
jtulach@656
   238
                }
jtulach@656
   239
            });
jtulach@656
   240
jtulach@656
   241
            return webView;
jtulach@656
   242
        }
jtulach@656
   243
    }
jtulach@656
   244
    
jaroslav@288
   245
}