1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/rt/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/impl/FXBrwsr.java Thu Mar 14 09:22:28 2013 +0100
1.3 @@ -0,0 +1,169 @@
1.4 +/**
1.5 + * Back 2 Browser Bytecode Translator
1.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
1.7 + *
1.8 + * This program is free software: you can redistribute it and/or modify
1.9 + * it under the terms of the GNU General Public License as published by
1.10 + * the Free Software Foundation, version 2 of the License.
1.11 + *
1.12 + * This program is distributed in the hope that it will be useful,
1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.15 + * GNU General Public License for more details.
1.16 + *
1.17 + * You should have received a copy of the GNU General Public License
1.18 + * along with this program. Look for COPYING file in the top folder.
1.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
1.20 + */
1.21 +package org.apidesign.bck2brwsr.launcher.impl;
1.22 +
1.23 +import java.util.List;
1.24 +import java.util.logging.Level;
1.25 +import java.util.logging.Logger;
1.26 +import javafx.application.Application;
1.27 +import javafx.beans.value.ChangeListener;
1.28 +import javafx.beans.value.ObservableValue;
1.29 +import javafx.concurrent.Worker;
1.30 +import javafx.event.ActionEvent;
1.31 +import javafx.event.EventHandler;
1.32 +import javafx.geometry.HPos;
1.33 +import javafx.geometry.Insets;
1.34 +import javafx.geometry.VPos;
1.35 +import javafx.scene.Node;
1.36 +import javafx.scene.Scene;
1.37 +import javafx.scene.control.Button;
1.38 +import javafx.scene.control.TextField;
1.39 +import javafx.scene.layout.ColumnConstraints;
1.40 +import javafx.scene.layout.GridPane;
1.41 +import javafx.scene.layout.Pane;
1.42 +import javafx.scene.layout.Priority;
1.43 +import javafx.scene.layout.VBox;
1.44 +import javafx.scene.web.WebEngine;
1.45 +import javafx.scene.web.WebEvent;
1.46 +import javafx.scene.web.WebView;
1.47 +import javafx.stage.Stage;
1.48 +import javax.swing.JOptionPane;
1.49 +import netscape.javascript.JSObject;
1.50 +
1.51 +/**
1.52 + * Demonstrates a WebView object accessing a web page.
1.53 + *
1.54 + * @see javafx.scene.web.WebView
1.55 + * @see javafx.scene.web.WebEngine
1.56 + */
1.57 +public class FXBrwsr extends Application {
1.58 + private static final Logger LOG = Logger.getLogger(FXBrwsr.class.getName());
1.59 +
1.60 + @Override
1.61 + public void start(Stage primaryStage) throws Exception {
1.62 + Pane root = new WebViewPane(getParameters().getUnnamed());
1.63 + primaryStage.setScene(new Scene(root, 1024, 768));
1.64 + primaryStage.show();
1.65 + }
1.66 +
1.67 + /**
1.68 + * Create a resizable WebView pane
1.69 + */
1.70 + private class WebViewPane extends Pane {
1.71 + private final JVMBridge bridge = new JVMBridge();
1.72 +
1.73 + public WebViewPane(List<String> params) {
1.74 + VBox.setVgrow(this, Priority.ALWAYS);
1.75 + setMaxWidth(Double.MAX_VALUE);
1.76 + setMaxHeight(Double.MAX_VALUE);
1.77 + WebView view = new WebView();
1.78 + view.setMinSize(500, 400);
1.79 + view.setPrefSize(500, 400);
1.80 + final WebEngine eng = view.getEngine();
1.81 + final TextField locationField = new TextField();
1.82 + LOG.info("params : " + params);
1.83 + if (params.size() > 0) {
1.84 + eng.getLoadWorker().stateProperty().addListener(new ChangeListener<Worker.State>() {
1.85 + @Override
1.86 + public void changed(ObservableValue<? extends Worker.State> ov, Worker.State t, Worker.State t1) {
1.87 + LOG.info("about to init bck2brwsr");
1.88 + initBck2Brwsr(eng);
1.89 + LOG.info("done init of bck2brwsr");
1.90 + }
1.91 + });
1.92 + LOG.info("loading page " + params.get(0));
1.93 + eng.load(params.get(0));
1.94 + LOG.info("done loading page ");
1.95 + locationField.setText(params.get(0));
1.96 + }
1.97 + locationField.setMaxHeight(Double.MAX_VALUE);
1.98 + Button goButton = new Button("Go");
1.99 + goButton.setDefaultButton(true);
1.100 + EventHandler<ActionEvent> goAction = new EventHandler<ActionEvent>() {
1.101 + @Override
1.102 + public void handle(ActionEvent event) {
1.103 + eng.load(locationField.getText().startsWith("http://") ? locationField.getText() : "http://" + locationField.getText());
1.104 + }
1.105 + };
1.106 + goButton.setOnAction(goAction);
1.107 + locationField.setOnAction(goAction);
1.108 + eng.locationProperty().addListener(new ChangeListener<String>() {
1.109 + @Override
1.110 + public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
1.111 + locationField.setText(newValue);
1.112 + }
1.113 + });
1.114 + eng.setOnAlert(new EventHandler<WebEvent<String>>() {
1.115 + @Override
1.116 + public void handle(WebEvent<String> t) {
1.117 + JOptionPane.showMessageDialog(null, t.getData());
1.118 + }
1.119 + });
1.120 + GridPane grid = new GridPane();
1.121 + grid.setVgap(5);
1.122 + grid.setHgap(5);
1.123 + GridPane.setConstraints(locationField, 0, 0, 1, 1, HPos.CENTER, VPos.CENTER, Priority.ALWAYS, Priority.SOMETIMES);
1.124 + GridPane.setConstraints(goButton, 1, 0);
1.125 + GridPane.setConstraints(view, 0, 1, 2, 1, HPos.CENTER, VPos.CENTER, Priority.ALWAYS, Priority.ALWAYS);
1.126 + 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));
1.127 + grid.getChildren().addAll(locationField, goButton, view);
1.128 + getChildren().add(grid);
1.129 + }
1.130 +
1.131 + void initBck2Brwsr(WebEngine webEngine) {
1.132 + JSObject jsobj = (JSObject) webEngine.executeScript("window");
1.133 + LOG.info("window: " + jsobj);
1.134 + System.getProperties().put("webEngine", webEngine);
1.135 + Object prev = jsobj.getMember("jvmBridge");
1.136 + if (prev instanceof JSObject) {
1.137 + jsobj.setMember("jvmBridge", bridge);
1.138 + JSObject jso = (JSObject) prev;
1.139 + Object len = jso.getMember("length");
1.140 + if (len instanceof Integer) {
1.141 + for (int i = 0; i < (Integer) len; i++) {
1.142 + Object slt = jso.getSlot(i);
1.143 + if (slt instanceof String) {
1.144 + try {
1.145 + bridge.loadClass((String) slt);
1.146 + } catch (ClassNotFoundException ex) {
1.147 + LOG.log(Level.SEVERE, null, ex);
1.148 + }
1.149 + }
1.150 + }
1.151 + LOG.info("bck2brwsr: " + jsobj.getMember("bck2brwsr"));
1.152 + }
1.153 + }
1.154 + }
1.155 +
1.156 + @Override
1.157 + protected void layoutChildren() {
1.158 + List<Node> managed = getManagedChildren();
1.159 + double width = getWidth();
1.160 + double height = getHeight();
1.161 + double top = getInsets().getTop();
1.162 + double right = getInsets().getRight();
1.163 + double left = getInsets().getLeft();
1.164 + double bottom = getInsets().getBottom();
1.165 + for (int i = 0; i < managed.size(); i++) {
1.166 + Node child = managed.get(i);
1.167 + layoutInArea(child, left, top, width - left - right, height - top - bottom, 0, Insets.EMPTY, true, true, HPos.CENTER, VPos.CENTER);
1.168 + }
1.169 + }
1.170 + }
1.171 +
1.172 +}