launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/fximpl/FXBrwsr.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 28 Apr 2013 17:42:49 +0200
branchmodel
changeset 1041 f18b7262fe91
child 1166 16555ef29e9e
permissions -rw-r--r--
FX launcher can start tests
     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.HPos;
    31 import javafx.geometry.Insets;
    32 import javafx.geometry.Pos;
    33 import javafx.geometry.VPos;
    34 import javafx.scene.Node;
    35 import javafx.scene.Scene;
    36 import javafx.scene.control.Button;
    37 import javafx.scene.layout.ColumnConstraints;
    38 import javafx.scene.layout.GridPane;
    39 import javafx.scene.layout.Pane;
    40 import javafx.scene.layout.Priority;
    41 import javafx.scene.layout.VBox;
    42 import javafx.scene.text.Text;
    43 import javafx.scene.web.WebEngine;
    44 import javafx.scene.web.WebEvent;
    45 import javafx.scene.web.WebView;
    46 import javafx.stage.Modality;
    47 import javafx.stage.Stage;
    48 import netscape.javascript.JSObject;
    49 
    50 /**
    51  * Demonstrates a WebView object accessing a web page.
    52  *
    53  * @see javafx.scene.web.WebView
    54  * @see javafx.scene.web.WebEngine
    55  */
    56 public class FXBrwsr extends Application {
    57     private static final Logger LOG = Logger.getLogger(FXBrwsr.class.getName());
    58 
    59     @Override
    60     public void start(Stage primaryStage) throws Exception {
    61         Pane root = new WebViewPane(getParameters().getUnnamed());
    62         primaryStage.setScene(new Scene(root, 1024, 768));
    63         LOG.info("Showing the stage");
    64         primaryStage.show();
    65         LOG.log(Level.INFO, "State shown: {0}", primaryStage.isShowing());
    66     }
    67     
    68     /**
    69      * Create a resizable WebView pane
    70      */
    71     private class WebViewPane extends Pane {
    72         private final JVMBridge bridge = new JVMBridge();
    73 
    74         public WebViewPane(List<String> params) {
    75             LOG.log(Level.INFO, "Initializing WebView with {0}", params);
    76             VBox.setVgrow(this, Priority.ALWAYS);
    77             setMaxWidth(Double.MAX_VALUE);
    78             setMaxHeight(Double.MAX_VALUE);
    79             WebView view = new WebView();
    80             view.setMinSize(500, 400);
    81             view.setPrefSize(500, 400);
    82             final WebEngine eng = view.getEngine();
    83             try {
    84                 JVMBridge.addBck2BrwsrLoad(new InitBck2Brwsr(eng));
    85             } catch (TooManyListenersException ex) {
    86                 LOG.log(Level.SEVERE, null, ex);
    87             }
    88             
    89             if (params.size() > 0) {
    90                 LOG.log(Level.INFO, "loading page {0}", params.get(0));
    91                 eng.load(params.get(0));
    92                 LOG.fine("back from load");
    93             }
    94             eng.setOnAlert(new EventHandler<WebEvent<String>>() {
    95                 @Override
    96                 public void handle(WebEvent<String> t) {
    97                     final Stage dialogStage = new Stage();
    98                     dialogStage.initModality(Modality.WINDOW_MODAL);
    99                     dialogStage.setTitle("Warning");
   100                     final Button button = new Button("Close");
   101                     final Text text = new Text(t.getData());
   102                     
   103                     VBox box = new VBox();
   104                     box.setAlignment(Pos.CENTER);
   105                     box.setSpacing(10);
   106                     box.setPadding(new Insets(10));
   107                     box.getChildren().addAll(text, button);
   108                     
   109                     dialogStage.setScene(new Scene(box));
   110                     
   111                     button.setCancelButton(true);
   112                     button.setOnAction(new EventHandler<ActionEvent>() {
   113                         @Override
   114                         public void handle(ActionEvent t) {
   115                             dialogStage.close();
   116                         }
   117                     });
   118                     
   119                     dialogStage.centerOnScreen();
   120                     dialogStage.showAndWait();
   121                 }
   122             });
   123             GridPane grid = new GridPane();
   124             grid.setVgap(5);
   125             grid.setHgap(5);
   126             GridPane.setConstraints(view, 0, 1, 2, 1, HPos.CENTER, VPos.CENTER, Priority.ALWAYS, Priority.ALWAYS);
   127             grid.getColumnConstraints().addAll(new ColumnConstraints(100, 100, Double.MAX_VALUE, Priority.ALWAYS, HPos.CENTER, true), new ColumnConstraints(40, 40, 40, Priority.NEVER, HPos.CENTER, true));
   128             grid.getChildren().addAll(view);
   129             getChildren().add(grid);
   130         }
   131 
   132         boolean initBck2Brwsr(WebEngine webEngine) {
   133             JSObject jsobj = (JSObject) webEngine.executeScript("window");
   134             LOG.log(Level.FINE, "window: {0}", jsobj);
   135             Object prev = jsobj.getMember("bck2brwsr");
   136             if ("undefined".equals(prev)) {
   137                 System.getProperties().put("webEngine", webEngine);
   138                 jsobj.setMember("bck2brwsr", bridge);
   139                 return true;
   140             }
   141             return false;
   142         }
   143 
   144         @Override
   145         protected void layoutChildren() {
   146             List<Node> managed = getManagedChildren();
   147             double width = getWidth();
   148             double height = getHeight();
   149             double top = getInsets().getTop();
   150             double right = getInsets().getRight();
   151             double left = getInsets().getLeft();
   152             double bottom = getInsets().getBottom();
   153             for (int i = 0; i < managed.size(); i++) {
   154                 Node child = managed.get(i);
   155                 layoutInArea(child, left, top, width - left - right, height - top - bottom, 0, Insets.EMPTY, true, true, HPos.CENTER, VPos.CENTER);
   156             }
   157         }
   158 
   159         private class InitBck2Brwsr implements ChangeListener<Void>, Runnable {
   160             private final WebEngine eng;
   161 
   162             public InitBck2Brwsr(WebEngine eng) {
   163                 this.eng = eng;
   164             }
   165 
   166             @Override
   167             public synchronized void changed(ObservableValue<? extends Void> ov, Void t, Void t1) {
   168                 Platform.runLater(this);
   169                 try {
   170                     wait();
   171                 } catch (InterruptedException ex) {
   172                     LOG.log(Level.SEVERE, null, ex);
   173                 }
   174             }
   175 
   176             @Override
   177             public synchronized void run() {
   178                 initBck2Brwsr(eng);
   179                 notifyAll();
   180             }
   181         }
   182     }
   183     
   184 }