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