boot-fx/src/main/java/net/java/html/boot/fx/FXBrowsers.java
author Jaroslav Tulach <jaroslav.tulach@netbeans.org>
Fri, 07 Feb 2014 07:44:34 +0100
changeset 551 7ca2253fa86d
parent 542 ae488d072a16
child 627 e44ad6c000d1
child 636 d31666353dc4
permissions -rw-r--r--
Updating copyright headers to mention current year
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;
jaroslav@288
    46
import javafx.beans.value.ChangeListener;
jaroslav@288
    47
import javafx.beans.value.ObservableValue;
jaroslav@288
    48
import javafx.concurrent.Worker;
jaroslav@288
    49
import javafx.scene.web.WebView;
jaroslav@288
    50
import net.java.html.boot.BrowserBuilder;
jaroslav@290
    51
import net.java.html.js.JavaScriptBody;
jaroslav@362
    52
import org.netbeans.html.boot.fx.AbstractFXPresenter;
jaroslav@288
    53
jaroslav@290
    54
/** Utility methods for working with <em>JavaFX</em> <code>WebView</code>s.
jaroslav@288
    55
 *
jaroslav@288
    56
 * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@290
    57
 * @since 0.6
jaroslav@288
    58
 */
jaroslav@288
    59
public final class FXBrowsers {
jaroslav@288
    60
    private FXBrowsers() {
jaroslav@288
    61
    }
jaroslav@288
    62
    
jaroslav@542
    63
    /** Enables the Java/JavaScript bridge (that supports {@link JavaScriptBody} and co.)
jaroslav@290
    64
     * in the provided <code>webView</code>. This method returns 
jaroslav@290
    65
     * immediately. Once the support is active, it calls back specified
jaroslav@290
    66
     * method in <code>onPageLoad</code> class - the class can possibly be
jaroslav@290
    67
     * loaded by a different classloader (to enable replacement of
jaroslav@290
    68
     * methods with {@link JavaScriptBody} annotations with executable
jaroslav@290
    69
     * versions). The method <code>methodName</code> needs to be <code>public</code>
jaroslav@290
    70
     * (in a public class), <code>static</code> and take either no parameters
jaroslav@290
    71
     * or an array of {@link String}s.
jaroslav@290
    72
     * 
jaroslav@290
    73
     * @param webView the instance of Web View to tweak
jaroslav@290
    74
     * @param url the URL of the HTML page to load into the view
jaroslav@290
    75
     * @param onPageLoad callback class with method <code>methodName</code>
jaroslav@290
    76
     * @param methodName the method to call when the page is loaded
jaroslav@290
    77
     * @param args arguments to pass to the <code>methodName</code> method
jaroslav@290
    78
     */
jaroslav@288
    79
    public static void load(
jaroslav@288
    80
        final WebView webView, final URL url, 
jaroslav@288
    81
        Class<?> onPageLoad, String methodName,
jaroslav@288
    82
        String... args
jaroslav@288
    83
    ) {
jaroslav@288
    84
        class Load extends AbstractFXPresenter {
jaroslav@288
    85
            @Override
jaroslav@288
    86
            protected void waitFinished() {
jaroslav@288
    87
                // don't wait
jaroslav@288
    88
            }
jaroslav@288
    89
jaroslav@288
    90
            @Override
jaroslav@288
    91
            protected WebView findView(URL resource) {
jaroslav@288
    92
                final Worker<Void> w = webView.getEngine().getLoadWorker();
jaroslav@288
    93
                w.stateProperty().addListener(new ChangeListener<Worker.State>() {
jaroslav@288
    94
                    @Override
jaroslav@288
    95
                    public void changed(ObservableValue<? extends Worker.State> ov, Worker.State t, Worker.State newState) {
jaroslav@288
    96
                        if (newState.equals(Worker.State.SUCCEEDED)) {
jaroslav@288
    97
                            onPageLoad();
jaroslav@288
    98
                        }
jaroslav@288
    99
                        if (newState.equals(Worker.State.FAILED)) {
jaroslav@288
   100
                            throw new IllegalStateException("Failed to load " + url);
jaroslav@288
   101
                        }
jaroslav@288
   102
                    }
jaroslav@288
   103
                });
jaroslav@288
   104
                
jaroslav@288
   105
                return webView;
jaroslav@288
   106
            }
jaroslav@288
   107
        }
jaroslav@288
   108
        BrowserBuilder.newBrowser(new Load()).
jaroslav@288
   109
            loadPage(url.toExternalForm()).
jaroslav@288
   110
            loadClass(onPageLoad).
jaroslav@288
   111
            invoke(methodName, args).
jaroslav@288
   112
            showAndWait();
jaroslav@288
   113
    }
jaroslav@288
   114
}