diff -r 000000000000 -r 859804c78010 rt/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/impl/FXBrwsr.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rt/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/impl/FXBrwsr.java Thu Mar 14 09:22:28 2013 +0100 @@ -0,0 +1,169 @@ +/** + * Back 2 Browser Bytecode Translator + * Copyright (C) 2012 Jaroslav Tulach + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. Look for COPYING file in the top folder. + * If not, see http://opensource.org/licenses/GPL-2.0. + */ +package org.apidesign.bck2brwsr.launcher.impl; + +import java.util.List; +import java.util.logging.Level; +import java.util.logging.Logger; +import javafx.application.Application; +import javafx.beans.value.ChangeListener; +import javafx.beans.value.ObservableValue; +import javafx.concurrent.Worker; +import javafx.event.ActionEvent; +import javafx.event.EventHandler; +import javafx.geometry.HPos; +import javafx.geometry.Insets; +import javafx.geometry.VPos; +import javafx.scene.Node; +import javafx.scene.Scene; +import javafx.scene.control.Button; +import javafx.scene.control.TextField; +import javafx.scene.layout.ColumnConstraints; +import javafx.scene.layout.GridPane; +import javafx.scene.layout.Pane; +import javafx.scene.layout.Priority; +import javafx.scene.layout.VBox; +import javafx.scene.web.WebEngine; +import javafx.scene.web.WebEvent; +import javafx.scene.web.WebView; +import javafx.stage.Stage; +import javax.swing.JOptionPane; +import netscape.javascript.JSObject; + +/** + * Demonstrates a WebView object accessing a web page. + * + * @see javafx.scene.web.WebView + * @see javafx.scene.web.WebEngine + */ +public class FXBrwsr extends Application { + private static final Logger LOG = Logger.getLogger(FXBrwsr.class.getName()); + + @Override + public void start(Stage primaryStage) throws Exception { + Pane root = new WebViewPane(getParameters().getUnnamed()); + primaryStage.setScene(new Scene(root, 1024, 768)); + primaryStage.show(); + } + + /** + * Create a resizable WebView pane + */ + private class WebViewPane extends Pane { + private final JVMBridge bridge = new JVMBridge(); + + public WebViewPane(List params) { + VBox.setVgrow(this, Priority.ALWAYS); + setMaxWidth(Double.MAX_VALUE); + setMaxHeight(Double.MAX_VALUE); + WebView view = new WebView(); + view.setMinSize(500, 400); + view.setPrefSize(500, 400); + final WebEngine eng = view.getEngine(); + final TextField locationField = new TextField(); + LOG.info("params : " + params); + if (params.size() > 0) { + eng.getLoadWorker().stateProperty().addListener(new ChangeListener() { + @Override + public void changed(ObservableValue ov, Worker.State t, Worker.State t1) { + LOG.info("about to init bck2brwsr"); + initBck2Brwsr(eng); + LOG.info("done init of bck2brwsr"); + } + }); + LOG.info("loading page " + params.get(0)); + eng.load(params.get(0)); + LOG.info("done loading page "); + locationField.setText(params.get(0)); + } + locationField.setMaxHeight(Double.MAX_VALUE); + Button goButton = new Button("Go"); + goButton.setDefaultButton(true); + EventHandler goAction = new EventHandler() { + @Override + public void handle(ActionEvent event) { + eng.load(locationField.getText().startsWith("http://") ? locationField.getText() : "http://" + locationField.getText()); + } + }; + goButton.setOnAction(goAction); + locationField.setOnAction(goAction); + eng.locationProperty().addListener(new ChangeListener() { + @Override + public void changed(ObservableValue observable, String oldValue, String newValue) { + locationField.setText(newValue); + } + }); + eng.setOnAlert(new EventHandler>() { + @Override + public void handle(WebEvent t) { + JOptionPane.showMessageDialog(null, t.getData()); + } + }); + GridPane grid = new GridPane(); + grid.setVgap(5); + grid.setHgap(5); + GridPane.setConstraints(locationField, 0, 0, 1, 1, HPos.CENTER, VPos.CENTER, Priority.ALWAYS, Priority.SOMETIMES); + GridPane.setConstraints(goButton, 1, 0); + GridPane.setConstraints(view, 0, 1, 2, 1, HPos.CENTER, VPos.CENTER, Priority.ALWAYS, Priority.ALWAYS); + 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)); + grid.getChildren().addAll(locationField, goButton, view); + getChildren().add(grid); + } + + void initBck2Brwsr(WebEngine webEngine) { + JSObject jsobj = (JSObject) webEngine.executeScript("window"); + LOG.info("window: " + jsobj); + System.getProperties().put("webEngine", webEngine); + Object prev = jsobj.getMember("jvmBridge"); + if (prev instanceof JSObject) { + jsobj.setMember("jvmBridge", bridge); + JSObject jso = (JSObject) prev; + Object len = jso.getMember("length"); + if (len instanceof Integer) { + for (int i = 0; i < (Integer) len; i++) { + Object slt = jso.getSlot(i); + if (slt instanceof String) { + try { + bridge.loadClass((String) slt); + } catch (ClassNotFoundException ex) { + LOG.log(Level.SEVERE, null, ex); + } + } + } + LOG.info("bck2brwsr: " + jsobj.getMember("bck2brwsr")); + } + } + } + + @Override + protected void layoutChildren() { + List managed = getManagedChildren(); + double width = getWidth(); + double height = getHeight(); + double top = getInsets().getTop(); + double right = getInsets().getRight(); + double left = getInsets().getLeft(); + double bottom = getInsets().getBottom(); + for (int i = 0; i < managed.size(); i++) { + Node child = managed.get(i); + layoutInArea(child, left, top, width - left - right, height - top - bottom, 0, Insets.EMPTY, true, true, HPos.CENTER, VPos.CENTER); + } + } + } + +}