launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/fximpl/FXBrwsr.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 30 Oct 2013 11:24:41 +0100
changeset 1391 4cd2c7e22646
parent 1180 80affbdece28
child 1415 9718335431b7
permissions -rw-r--r--
JavaFX from JDK8 does not have this impl method anymore
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, version 2 of the License.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program. Look for COPYING file in the top folder.
    16  * If not, see http://opensource.org/licenses/GPL-2.0.
    17  */
    18 package org.apidesign.bck2brwsr.launcher.fximpl;
    19 
    20 import java.util.List;
    21 import java.util.TooManyListenersException;
    22 import java.util.logging.Level;
    23 import java.util.logging.Logger;
    24 import javafx.application.Application;
    25 import javafx.application.Platform;
    26 import javafx.beans.value.ChangeListener;
    27 import javafx.beans.value.ObservableValue;
    28 import javafx.event.ActionEvent;
    29 import javafx.event.EventHandler;
    30 import javafx.geometry.Insets;
    31 import javafx.geometry.Pos;
    32 import javafx.scene.Scene;
    33 import javafx.scene.control.Button;
    34 import javafx.scene.control.ToolBar;
    35 import javafx.scene.layout.BorderPane;
    36 import javafx.scene.layout.HBox;
    37 import javafx.scene.layout.VBox;
    38 import javafx.scene.text.Text;
    39 import javafx.scene.web.WebEngine;
    40 import javafx.scene.web.WebEvent;
    41 import javafx.scene.web.WebView;
    42 import javafx.stage.Modality;
    43 import javafx.stage.Stage;
    44 import netscape.javascript.JSObject;
    45 
    46 /**
    47  * Demonstrates a WebView object accessing a web page.
    48  *
    49  * @see javafx.scene.web.WebView
    50  * @see javafx.scene.web.WebEngine
    51  */
    52 public class FXBrwsr extends Application {
    53     private static final Logger LOG = Logger.getLogger(FXBrwsr.class.getName());
    54     
    55     @Override
    56     public void start(Stage primaryStage) throws Exception {
    57         WebView view = new WebView();
    58         WebController wc = new WebController(view, getParameters().getUnnamed());
    59         
    60         FXInspect.initialize(view.getEngine());
    61 
    62         final VBox vbox = new VBox();
    63         vbox.setAlignment( Pos.CENTER );
    64         vbox.setStyle( "-fx-background-color: #808080;");
    65 
    66 
    67         HBox hbox = new HBox();
    68         hbox.setStyle( "-fx-background-color: #808080;");
    69         hbox.setAlignment(Pos.CENTER);
    70         hbox.getChildren().add(vbox);
    71         vbox.getChildren().add(view);
    72 
    73         BorderPane root = new BorderPane();
    74         final boolean showToolbar = "true".equals(this.getParameters().getNamed().get("toolbar")); // NOI18N
    75         final boolean useFirebug = "true".equals(this.getParameters().getNamed().get("firebug")); // NOI18N
    76         if (showToolbar) {
    77             final ToolBar toolbar = new BrowserToolbar(view, vbox, useFirebug);
    78             root.setTop( toolbar );
    79         }
    80         root.setCenter(hbox);
    81 
    82         Scene scene = new Scene(root, 800, 600);
    83 
    84         primaryStage.setTitle( "Device Emulator" );
    85         primaryStage.setScene( scene );
    86         primaryStage.show();
    87     }
    88     
    89     /**
    90      * Create a resizable WebView pane
    91      */
    92     private static class WebController {
    93         private final JVMBridge bridge;
    94 
    95         public WebController(WebView view, List<String> params) {
    96             this.bridge = new JVMBridge(view.getEngine());
    97             LOG.log(Level.INFO, "Initializing WebView with {0}", params);
    98             final WebEngine eng = view.getEngine();
    99             try {
   100                 JVMBridge.addBck2BrwsrLoad(new InitBck2Brwsr(eng));
   101             } catch (TooManyListenersException ex) {
   102                 LOG.log(Level.SEVERE, null, ex);
   103             }
   104             
   105             if (params.size() > 0) {
   106                 LOG.log(Level.INFO, "loading page {0}", params.get(0));
   107                 eng.load(params.get(0));
   108                 LOG.fine("back from load");
   109             }
   110             eng.setOnAlert(new EventHandler<WebEvent<String>>() {
   111                 @Override
   112                 public void handle(WebEvent<String> t) {
   113                     final Stage dialogStage = new Stage();
   114                     dialogStage.initModality(Modality.WINDOW_MODAL);
   115                     dialogStage.setTitle("Warning");
   116                     final Button button = new Button("Close");
   117                     final Text text = new Text(t.getData());
   118                     
   119                     VBox box = new VBox();
   120                     box.setAlignment(Pos.CENTER);
   121                     box.setSpacing(10);
   122                     box.setPadding(new Insets(10));
   123                     box.getChildren().addAll(text, button);
   124                     
   125                     dialogStage.setScene(new Scene(box));
   126                     
   127                     button.setCancelButton(true);
   128                     button.setOnAction(new EventHandler<ActionEvent>() {
   129                         @Override
   130                         public void handle(ActionEvent t) {
   131                             dialogStage.close();
   132                         }
   133                     });
   134                     
   135                     dialogStage.centerOnScreen();
   136                     dialogStage.showAndWait();
   137                 }
   138             });
   139         }
   140 
   141         boolean initBck2Brwsr(WebEngine webEngine) {
   142             JSObject jsobj = (JSObject) webEngine.executeScript("window");
   143             LOG.log(Level.FINE, "window: {0}", jsobj);
   144             Object prev = jsobj.getMember("bck2brwsr");
   145             if ("undefined".equals(prev)) {
   146                 jsobj.setMember("bck2brwsr", bridge);
   147                 return true;
   148             }
   149             return false;
   150         }
   151 
   152         private class InitBck2Brwsr implements ChangeListener<Void>, Runnable {
   153             private final WebEngine eng;
   154 
   155             public InitBck2Brwsr(WebEngine eng) {
   156                 this.eng = eng;
   157             }
   158 
   159             @Override
   160             public synchronized void changed(ObservableValue<? extends Void> ov, Void t, Void t1) {
   161                 Platform.runLater(this);
   162                 try {
   163                     wait();
   164                 } catch (InterruptedException ex) {
   165                     LOG.log(Level.SEVERE, null, ex);
   166                 }
   167             }
   168 
   169             @Override
   170             public synchronized void run() {
   171                 initBck2Brwsr(eng);
   172                 notifyAll();
   173             }
   174         }
   175     }
   176     
   177 }