rt/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/impl/FXBrwsr.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 14 Mar 2013 09:22:28 +0100
branchfx
changeset 845 859804c78010
child 853 39166e462f8d
permissions -rw-r--r--
Hacky way (relies on singleton) to get FX working. Can attach @On(event=CLICK) and show alert in FX WebView now
     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                     JOptionPane.showMessageDialog(null, t.getData());
   115                 }
   116             });
   117             GridPane grid = new GridPane();
   118             grid.setVgap(5);
   119             grid.setHgap(5);
   120             GridPane.setConstraints(locationField, 0, 0, 1, 1, HPos.CENTER, VPos.CENTER, Priority.ALWAYS, Priority.SOMETIMES);
   121             GridPane.setConstraints(goButton, 1, 0);
   122             GridPane.setConstraints(view, 0, 1, 2, 1, HPos.CENTER, VPos.CENTER, Priority.ALWAYS, Priority.ALWAYS);
   123             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));
   124             grid.getChildren().addAll(locationField, goButton, view);
   125             getChildren().add(grid);
   126         }
   127 
   128         void initBck2Brwsr(WebEngine webEngine) {
   129             JSObject jsobj = (JSObject) webEngine.executeScript("window");
   130             LOG.info("window: " + jsobj);
   131             System.getProperties().put("webEngine", webEngine);
   132             Object prev = jsobj.getMember("jvmBridge");
   133             if (prev instanceof JSObject) {
   134                 jsobj.setMember("jvmBridge", bridge);
   135                 JSObject jso = (JSObject) prev;
   136                 Object len = jso.getMember("length");
   137                 if (len instanceof Integer) {
   138                     for (int i = 0; i < (Integer) len; i++) {
   139                         Object slt = jso.getSlot(i);
   140                         if (slt instanceof String) {
   141                             try {
   142                                 bridge.loadClass((String) slt);
   143                             } catch (ClassNotFoundException ex) {
   144                                 LOG.log(Level.SEVERE, null, ex);
   145                             }
   146                         }
   147                     }
   148                     LOG.info("bck2brwsr: " + jsobj.getMember("bck2brwsr"));
   149                 }
   150             }
   151         }
   152 
   153         @Override
   154         protected void layoutChildren() {
   155             List<Node> managed = getManagedChildren();
   156             double width = getWidth();
   157             double height = getHeight();
   158             double top = getInsets().getTop();
   159             double right = getInsets().getRight();
   160             double left = getInsets().getLeft();
   161             double bottom = getInsets().getBottom();
   162             for (int i = 0; i < managed.size(); i++) {
   163                 Node child = managed.get(i);
   164                 layoutInArea(child, left, top, width - left - right, height - top - bottom, 0, Insets.EMPTY, true, true, HPos.CENTER, VPos.CENTER);
   165             }
   166         }
   167     }
   168     
   169 }