1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/boot-fx/src/main/java/org/netbeans/html/boot/fx/FXBrwsr.java Mon Dec 16 16:59:43 2013 +0100
1.3 @@ -0,0 +1,193 @@
1.4 +/**
1.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
1.6 + *
1.7 + * Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved.
1.8 + *
1.9 + * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
1.10 + * Other names may be trademarks of their respective owners.
1.11 + *
1.12 + * The contents of this file are subject to the terms of either the GNU
1.13 + * General Public License Version 2 only ("GPL") or the Common
1.14 + * Development and Distribution License("CDDL") (collectively, the
1.15 + * "License"). You may not use this file except in compliance with the
1.16 + * License. You can obtain a copy of the License at
1.17 + * http://www.netbeans.org/cddl-gplv2.html
1.18 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
1.19 + * specific language governing permissions and limitations under the
1.20 + * License. When distributing the software, include this License Header
1.21 + * Notice in each file and include the License file at
1.22 + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
1.23 + * particular file as subject to the "Classpath" exception as provided
1.24 + * by Oracle in the GPL Version 2 section of the License file that
1.25 + * accompanied this code. If applicable, add the following below the
1.26 + * License Header, with the fields enclosed by brackets [] replaced by
1.27 + * your own identifying information:
1.28 + * "Portions Copyrighted [year] [name of copyright owner]"
1.29 + *
1.30 + * Contributor(s):
1.31 + *
1.32 + * The Original Software is NetBeans. The Initial Developer of the Original
1.33 + * Software is Oracle. Portions Copyright 2013-2013 Oracle. All Rights Reserved.
1.34 + *
1.35 + * If you wish your version of this file to be governed by only the CDDL
1.36 + * or only the GPL Version 2, indicate your decision by adding
1.37 + * "[Contributor] elects to include this software in this distribution
1.38 + * under the [CDDL or GPL Version 2] license." If you do not indicate a
1.39 + * single choice of license, a recipient has the option to distribute
1.40 + * your version of this file under either the CDDL, the GPL Version 2 or
1.41 + * to extend the choice of license to its licensees as provided above.
1.42 + * However, if you add GPL Version 2 code and therefore, elected the GPL
1.43 + * Version 2 license, then the option applies only if the new code is
1.44 + * made subject to such option by the copyright holder.
1.45 + */
1.46 +package org.netbeans.html.boot.fx;
1.47 +
1.48 +import java.net.URL;
1.49 +import java.util.concurrent.CountDownLatch;
1.50 +import java.util.concurrent.Executors;
1.51 +import java.util.logging.Level;
1.52 +import java.util.logging.Logger;
1.53 +import javafx.application.Application;
1.54 +import javafx.application.Platform;
1.55 +import javafx.beans.value.ChangeListener;
1.56 +import javafx.beans.value.ObservableValue;
1.57 +import javafx.concurrent.Worker;
1.58 +import javafx.event.ActionEvent;
1.59 +import javafx.event.EventHandler;
1.60 +import javafx.geometry.Insets;
1.61 +import javafx.geometry.Pos;
1.62 +import javafx.scene.Scene;
1.63 +import javafx.scene.control.Button;
1.64 +import javafx.scene.layout.BorderPane;
1.65 +import javafx.scene.layout.VBox;
1.66 +import javafx.scene.text.Text;
1.67 +import javafx.scene.web.WebEvent;
1.68 +import javafx.scene.web.WebView;
1.69 +import javafx.stage.Modality;
1.70 +import javafx.stage.Stage;
1.71 +
1.72 +/** This is an implementation class, use {@link BrowserBuilder} API. Just
1.73 + * include this JAR on classpath and the {@link BrowserBuilder} API will find
1.74 + * this implementation automatically.
1.75 + */
1.76 +public class FXBrwsr extends Application {
1.77 + private static final Logger LOG = Logger.getLogger(FXBrwsr.class.getName());
1.78 + private static FXBrwsr INSTANCE;
1.79 + private static final CountDownLatch FINISHED = new CountDownLatch(1);
1.80 + private BorderPane root;
1.81 +
1.82 + public static synchronized WebView findWebView(final URL url, final FXPresenter onLoad) {
1.83 + if (INSTANCE == null) {
1.84 + Executors.newFixedThreadPool(1).submit(new Runnable() {
1.85 + @Override
1.86 + public void run() {
1.87 + try {
1.88 + FXBrwsr.launch(FXBrwsr.class);
1.89 + } catch (Throwable ex) {
1.90 + ex.printStackTrace();
1.91 + } finally {
1.92 + FINISHED.countDown();
1.93 + }
1.94 + }
1.95 + });
1.96 + }
1.97 + while (INSTANCE == null) {
1.98 + try {
1.99 + FXBrwsr.class.wait();
1.100 + } catch (InterruptedException ex) {
1.101 + // wait more
1.102 + }
1.103 + }
1.104 + if (!Platform.isFxApplicationThread()) {
1.105 + final WebView[] arr = {null};
1.106 + final CountDownLatch waitForResult = new CountDownLatch(1);
1.107 + Platform.runLater(new Runnable() {
1.108 + @Override
1.109 + public void run() {
1.110 + arr[0] = INSTANCE.newView(url, onLoad);
1.111 + waitForResult.countDown();
1.112 + }
1.113 + });
1.114 + for (;;) {
1.115 + try {
1.116 + waitForResult.await();
1.117 + break;
1.118 + } catch (InterruptedException ex) {
1.119 + LOG.log(Level.INFO, null, ex);
1.120 + }
1.121 + }
1.122 + return arr[0];
1.123 + } else {
1.124 + return INSTANCE.newView(url, onLoad);
1.125 + }
1.126 + }
1.127 +
1.128 + @Override
1.129 + public void start(Stage primaryStage) throws Exception {
1.130 + synchronized (FXBrwsr.class) {
1.131 + INSTANCE = this;
1.132 + FXBrwsr.class.notifyAll();
1.133 + }
1.134 + BorderPane r = new BorderPane();
1.135 + Scene scene = new Scene(r, 800, 600);
1.136 + primaryStage.setScene(scene);
1.137 + primaryStage.show();
1.138 + this.root = r;
1.139 + }
1.140 +
1.141 + private WebView newView(final URL url, final FXPresenter onLoad) {
1.142 + final WebView view = new WebView();
1.143 + view.setContextMenuEnabled(false);
1.144 + view.getEngine().setOnAlert(new EventHandler<WebEvent<String>>() {
1.145 + @Override
1.146 + public void handle(WebEvent<String> t) {
1.147 + final Stage dialogStage = new Stage();
1.148 + dialogStage.initModality(Modality.WINDOW_MODAL);
1.149 + dialogStage.setTitle("Warning");
1.150 + final Button button = new Button("Close");
1.151 + final Text text = new Text(t.getData());
1.152 + VBox box = new VBox();
1.153 + box.setAlignment(Pos.CENTER);
1.154 + box.setSpacing(10);
1.155 + box.setPadding(new Insets(10));
1.156 + box.getChildren().addAll(text, button);
1.157 + dialogStage.setScene(new Scene(box));
1.158 + button.setCancelButton(true);
1.159 + button.setOnAction(new EventHandler<ActionEvent>() {
1.160 + @Override
1.161 + public void handle(ActionEvent t) {
1.162 + dialogStage.close();
1.163 + }
1.164 + });
1.165 + dialogStage.centerOnScreen();
1.166 + dialogStage.showAndWait();
1.167 + }
1.168 + });
1.169 + root.setCenter(view);
1.170 + final Worker<Void> w = view.getEngine().getLoadWorker();
1.171 + w.stateProperty().addListener(new ChangeListener<Worker.State>() {
1.172 + @Override
1.173 + public void changed(ObservableValue<? extends Worker.State> ov, Worker.State t, Worker.State newState) {
1.174 + if (newState.equals(Worker.State.SUCCEEDED)) {
1.175 + onLoad.onPageLoad();
1.176 + }
1.177 + if (newState.equals(Worker.State.FAILED)) {
1.178 + throw new IllegalStateException("Failed to load " + url);
1.179 + }
1.180 + }
1.181 + });
1.182 + return view;
1.183 + }
1.184 +
1.185 + static void waitFinished() {
1.186 + for (;;) {
1.187 + try {
1.188 + FINISHED.await();
1.189 + break;
1.190 + } catch (InterruptedException ex) {
1.191 + LOG.log(Level.INFO, null, ex);
1.192 + }
1.193 + }
1.194 + }
1.195 +
1.196 +}