1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/fximpl/FXBrwsr.java Sun Apr 28 17:42:49 2013 +0200
1.3 @@ -0,0 +1,184 @@
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.fximpl;
1.22 +
1.23 +import java.util.List;
1.24 +import java.util.TooManyListenersException;
1.25 +import java.util.logging.Level;
1.26 +import java.util.logging.Logger;
1.27 +import javafx.application.Application;
1.28 +import javafx.application.Platform;
1.29 +import javafx.beans.value.ChangeListener;
1.30 +import javafx.beans.value.ObservableValue;
1.31 +import javafx.event.ActionEvent;
1.32 +import javafx.event.EventHandler;
1.33 +import javafx.geometry.HPos;
1.34 +import javafx.geometry.Insets;
1.35 +import javafx.geometry.Pos;
1.36 +import javafx.geometry.VPos;
1.37 +import javafx.scene.Node;
1.38 +import javafx.scene.Scene;
1.39 +import javafx.scene.control.Button;
1.40 +import javafx.scene.layout.ColumnConstraints;
1.41 +import javafx.scene.layout.GridPane;
1.42 +import javafx.scene.layout.Pane;
1.43 +import javafx.scene.layout.Priority;
1.44 +import javafx.scene.layout.VBox;
1.45 +import javafx.scene.text.Text;
1.46 +import javafx.scene.web.WebEngine;
1.47 +import javafx.scene.web.WebEvent;
1.48 +import javafx.scene.web.WebView;
1.49 +import javafx.stage.Modality;
1.50 +import javafx.stage.Stage;
1.51 +import netscape.javascript.JSObject;
1.52 +
1.53 +/**
1.54 + * Demonstrates a WebView object accessing a web page.
1.55 + *
1.56 + * @see javafx.scene.web.WebView
1.57 + * @see javafx.scene.web.WebEngine
1.58 + */
1.59 +public class FXBrwsr extends Application {
1.60 + private static final Logger LOG = Logger.getLogger(FXBrwsr.class.getName());
1.61 +
1.62 + @Override
1.63 + public void start(Stage primaryStage) throws Exception {
1.64 + Pane root = new WebViewPane(getParameters().getUnnamed());
1.65 + primaryStage.setScene(new Scene(root, 1024, 768));
1.66 + LOG.info("Showing the stage");
1.67 + primaryStage.show();
1.68 + LOG.log(Level.INFO, "State shown: {0}", primaryStage.isShowing());
1.69 + }
1.70 +
1.71 + /**
1.72 + * Create a resizable WebView pane
1.73 + */
1.74 + private class WebViewPane extends Pane {
1.75 + private final JVMBridge bridge = new JVMBridge();
1.76 +
1.77 + public WebViewPane(List<String> params) {
1.78 + LOG.log(Level.INFO, "Initializing WebView with {0}", params);
1.79 + VBox.setVgrow(this, Priority.ALWAYS);
1.80 + setMaxWidth(Double.MAX_VALUE);
1.81 + setMaxHeight(Double.MAX_VALUE);
1.82 + WebView view = new WebView();
1.83 + view.setMinSize(500, 400);
1.84 + view.setPrefSize(500, 400);
1.85 + final WebEngine eng = view.getEngine();
1.86 + try {
1.87 + JVMBridge.addBck2BrwsrLoad(new InitBck2Brwsr(eng));
1.88 + } catch (TooManyListenersException ex) {
1.89 + LOG.log(Level.SEVERE, null, ex);
1.90 + }
1.91 +
1.92 + if (params.size() > 0) {
1.93 + LOG.log(Level.INFO, "loading page {0}", params.get(0));
1.94 + eng.load(params.get(0));
1.95 + LOG.fine("back from load");
1.96 + }
1.97 + eng.setOnAlert(new EventHandler<WebEvent<String>>() {
1.98 + @Override
1.99 + public void handle(WebEvent<String> t) {
1.100 + final Stage dialogStage = new Stage();
1.101 + dialogStage.initModality(Modality.WINDOW_MODAL);
1.102 + dialogStage.setTitle("Warning");
1.103 + final Button button = new Button("Close");
1.104 + final Text text = new Text(t.getData());
1.105 +
1.106 + VBox box = new VBox();
1.107 + box.setAlignment(Pos.CENTER);
1.108 + box.setSpacing(10);
1.109 + box.setPadding(new Insets(10));
1.110 + box.getChildren().addAll(text, button);
1.111 +
1.112 + dialogStage.setScene(new Scene(box));
1.113 +
1.114 + button.setCancelButton(true);
1.115 + button.setOnAction(new EventHandler<ActionEvent>() {
1.116 + @Override
1.117 + public void handle(ActionEvent t) {
1.118 + dialogStage.close();
1.119 + }
1.120 + });
1.121 +
1.122 + dialogStage.centerOnScreen();
1.123 + dialogStage.showAndWait();
1.124 + }
1.125 + });
1.126 + GridPane grid = new GridPane();
1.127 + grid.setVgap(5);
1.128 + grid.setHgap(5);
1.129 + GridPane.setConstraints(view, 0, 1, 2, 1, HPos.CENTER, VPos.CENTER, Priority.ALWAYS, Priority.ALWAYS);
1.130 + 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.131 + grid.getChildren().addAll(view);
1.132 + getChildren().add(grid);
1.133 + }
1.134 +
1.135 + boolean initBck2Brwsr(WebEngine webEngine) {
1.136 + JSObject jsobj = (JSObject) webEngine.executeScript("window");
1.137 + LOG.log(Level.FINE, "window: {0}", jsobj);
1.138 + Object prev = jsobj.getMember("bck2brwsr");
1.139 + if ("undefined".equals(prev)) {
1.140 + System.getProperties().put("webEngine", webEngine);
1.141 + jsobj.setMember("bck2brwsr", bridge);
1.142 + return true;
1.143 + }
1.144 + return false;
1.145 + }
1.146 +
1.147 + @Override
1.148 + protected void layoutChildren() {
1.149 + List<Node> managed = getManagedChildren();
1.150 + double width = getWidth();
1.151 + double height = getHeight();
1.152 + double top = getInsets().getTop();
1.153 + double right = getInsets().getRight();
1.154 + double left = getInsets().getLeft();
1.155 + double bottom = getInsets().getBottom();
1.156 + for (int i = 0; i < managed.size(); i++) {
1.157 + Node child = managed.get(i);
1.158 + layoutInArea(child, left, top, width - left - right, height - top - bottom, 0, Insets.EMPTY, true, true, HPos.CENTER, VPos.CENTER);
1.159 + }
1.160 + }
1.161 +
1.162 + private class InitBck2Brwsr implements ChangeListener<Void>, Runnable {
1.163 + private final WebEngine eng;
1.164 +
1.165 + public InitBck2Brwsr(WebEngine eng) {
1.166 + this.eng = eng;
1.167 + }
1.168 +
1.169 + @Override
1.170 + public synchronized void changed(ObservableValue<? extends Void> ov, Void t, Void t1) {
1.171 + Platform.runLater(this);
1.172 + try {
1.173 + wait();
1.174 + } catch (InterruptedException ex) {
1.175 + LOG.log(Level.SEVERE, null, ex);
1.176 + }
1.177 + }
1.178 +
1.179 + @Override
1.180 + public synchronized void run() {
1.181 + initBck2Brwsr(eng);
1.182 + notifyAll();
1.183 + }
1.184 + }
1.185 + }
1.186 +
1.187 +}