rt/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/impl/FXBrwsr.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 20 Mar 2013 06:56:44 +0100
branchfx
changeset 853 39166e462f8d
parent 845 859804c78010
child 854 acffc26790d7
permissions -rw-r--r--
There is a way for the JavaScript to tell the Java wrapper it is running: alert. Initialize bck2brwsr callback in alert handler.
     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.impl;
    19 
    20 import java.util.List;
    21 import java.util.logging.Level;
    22 import java.util.logging.Logger;
    23 import javafx.application.Application;
    24 import javafx.beans.value.ChangeListener;
    25 import javafx.beans.value.ObservableValue;
    26 import javafx.concurrent.Worker;
    27 import javafx.event.ActionEvent;
    28 import javafx.event.EventHandler;
    29 import javafx.geometry.HPos;
    30 import javafx.geometry.Insets;
    31 import javafx.geometry.VPos;
    32 import javafx.scene.Node;
    33 import javafx.scene.Scene;
    34 import javafx.scene.control.Button;
    35 import javafx.scene.control.TextField;
    36 import javafx.scene.layout.ColumnConstraints;
    37 import javafx.scene.layout.GridPane;
    38 import javafx.scene.layout.Pane;
    39 import javafx.scene.layout.Priority;
    40 import javafx.scene.layout.VBox;
    41 import javafx.scene.web.WebEngine;
    42 import javafx.scene.web.WebEvent;
    43 import javafx.scene.web.WebView;
    44 import javafx.stage.Stage;
    45 import javax.swing.JOptionPane;
    46 import netscape.javascript.JSObject;
    47 
    48 /**
    49  * Demonstrates a WebView object accessing a web page.
    50  *
    51  * @see javafx.scene.web.WebView
    52  * @see javafx.scene.web.WebEngine
    53  */
    54 public class FXBrwsr extends Application {
    55     private static final Logger LOG = Logger.getLogger(FXBrwsr.class.getName());
    56 
    57     @Override
    58     public void start(Stage primaryStage) throws Exception {
    59         Pane root = new WebViewPane(getParameters().getUnnamed());
    60         primaryStage.setScene(new Scene(root, 1024, 768));
    61         primaryStage.show();
    62     }
    63     
    64     /**
    65      * Create a resizable WebView pane
    66      */
    67     private class WebViewPane extends Pane {
    68         private final JVMBridge bridge = new JVMBridge();
    69 
    70         public WebViewPane(List<String> params) {
    71             VBox.setVgrow(this, Priority.ALWAYS);
    72             setMaxWidth(Double.MAX_VALUE);
    73             setMaxHeight(Double.MAX_VALUE);
    74             WebView view = new WebView();
    75             view.setMinSize(500, 400);
    76             view.setPrefSize(500, 400);
    77             final WebEngine eng = view.getEngine();
    78             final TextField locationField = new TextField();
    79             LOG.info("params : " + params);
    80             if (params.size() > 0) {
    81                 eng.getLoadWorker().stateProperty().addListener(new ChangeListener<Worker.State>() {
    82                     @Override
    83                     public void changed(ObservableValue<? extends Worker.State> ov, Worker.State t, Worker.State t1) {
    84                         LOG.info("about to init bck2brwsr");
    85                         initBck2Brwsr(eng);
    86                         LOG.info("done init of bck2brwsr");
    87                     }
    88                 });
    89                 LOG.info("loading page " + params.get(0));
    90                 eng.load(params.get(0));
    91                 LOG.info("done loading page ");
    92                 locationField.setText(params.get(0));
    93             }
    94             locationField.setMaxHeight(Double.MAX_VALUE);
    95             Button goButton = new Button("Go");
    96             goButton.setDefaultButton(true);
    97             EventHandler<ActionEvent> goAction = new EventHandler<ActionEvent>() {
    98                 @Override
    99                 public void handle(ActionEvent event) {
   100                     eng.load(locationField.getText().startsWith("http://") ? locationField.getText() : "http://" + locationField.getText());
   101                 }
   102             };
   103             goButton.setOnAction(goAction);
   104             locationField.setOnAction(goAction);
   105             eng.locationProperty().addListener(new ChangeListener<String>() {
   106                 @Override
   107                 public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
   108                     locationField.setText(newValue);
   109                 }
   110             });
   111             eng.setOnAlert(new EventHandler<WebEvent<String>>() {
   112                 @Override
   113                 public void handle(WebEvent<String> t) {
   114                     if ("bck2brwsr".equals(t.getData())) {
   115                         initBck2Brwsr(eng);
   116                         return;
   117                     }
   118                     JOptionPane.showMessageDialog(null, t.getData());
   119                 }
   120             });
   121             GridPane grid = new GridPane();
   122             grid.setVgap(5);
   123             grid.setHgap(5);
   124             GridPane.setConstraints(locationField, 0, 0, 1, 1, HPos.CENTER, VPos.CENTER, Priority.ALWAYS, Priority.SOMETIMES);
   125             GridPane.setConstraints(goButton, 1, 0);
   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(locationField, goButton, view);
   129             getChildren().add(grid);
   130         }
   131 
   132         void initBck2Brwsr(WebEngine webEngine) {
   133             JSObject jsobj = (JSObject) webEngine.executeScript("window");
   134             LOG.info("window: " + jsobj);
   135             Object prev = jsobj.getMember("bck2brwsr");
   136             if ("undefined".equals(prev)) {
   137                 System.getProperties().put("webEngine", webEngine);
   138                 jsobj.setMember("bck2brwsr", bridge);
   139             }
   140         }
   141 
   142         @Override
   143         protected void layoutChildren() {
   144             List<Node> managed = getManagedChildren();
   145             double width = getWidth();
   146             double height = getHeight();
   147             double top = getInsets().getTop();
   148             double right = getInsets().getRight();
   149             double left = getInsets().getLeft();
   150             double bottom = getInsets().getBottom();
   151             for (int i = 0; i < managed.size(); i++) {
   152                 Node child = managed.get(i);
   153                 layoutInArea(child, left, top, width - left - right, height - top - bottom, 0, Insets.EMPTY, true, true, HPos.CENTER, VPos.CENTER);
   154             }
   155         }
   156     }
   157     
   158 }