2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
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.
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.
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.
18 package org.apidesign.bck2brwsr.launcher.impl;
20 import java.util.List;
21 import java.util.TooManyListenersException;
22 import java.util.logging.Level;
23 import java.util.logging.Logger;
24 import javafx.application.Application;
25 import javafx.application.Platform;
26 import javafx.beans.value.ChangeListener;
27 import javafx.beans.value.ObservableValue;
28 import javafx.event.ActionEvent;
29 import javafx.event.EventHandler;
30 import javafx.geometry.HPos;
31 import javafx.geometry.Insets;
32 import javafx.geometry.Pos;
33 import javafx.geometry.VPos;
34 import javafx.scene.Node;
35 import javafx.scene.Scene;
36 import javafx.scene.control.Button;
37 import javafx.scene.layout.ColumnConstraints;
38 import javafx.scene.layout.GridPane;
39 import javafx.scene.layout.Pane;
40 import javafx.scene.layout.Priority;
41 import javafx.scene.layout.VBox;
42 import javafx.scene.text.Text;
43 import javafx.scene.web.WebEngine;
44 import javafx.scene.web.WebEvent;
45 import javafx.scene.web.WebView;
46 import javafx.stage.Modality;
47 import javafx.stage.Stage;
48 import netscape.javascript.JSObject;
51 * Demonstrates a WebView object accessing a web page.
53 * @see javafx.scene.web.WebView
54 * @see javafx.scene.web.WebEngine
56 public class FXBrwsr extends Application {
57 private static final Logger LOG = Logger.getLogger(FXBrwsr.class.getName());
60 public void start(Stage primaryStage) throws Exception {
61 Pane root = new WebViewPane(getParameters().getUnnamed());
62 primaryStage.setScene(new Scene(root, 1024, 768));
63 LOG.info("Showing the stage");
65 LOG.log(Level.INFO, "State shown: {0}", primaryStage.isShowing());
69 * Create a resizable WebView pane
71 private class WebViewPane extends Pane {
72 private final JVMBridge bridge = new JVMBridge();
74 public WebViewPane(List<String> params) {
75 LOG.log(Level.INFO, "Initializing WebView with {0}", params);
76 VBox.setVgrow(this, Priority.ALWAYS);
77 setMaxWidth(Double.MAX_VALUE);
78 setMaxHeight(Double.MAX_VALUE);
79 WebView view = new WebView();
80 view.setMinSize(500, 400);
81 view.setPrefSize(500, 400);
82 final WebEngine eng = view.getEngine();
84 JVMBridge.addBck2BrwsrLoad(new InitBck2Brwsr(eng));
85 } catch (TooManyListenersException ex) {
86 LOG.log(Level.SEVERE, null, ex);
89 if (params.size() > 0) {
90 LOG.log(Level.INFO, "loading page {0}", params.get(0));
91 eng.load(params.get(0));
92 LOG.fine("back from load");
94 eng.setOnAlert(new EventHandler<WebEvent<String>>() {
96 public void handle(WebEvent<String> t) {
97 final Stage dialogStage = new Stage();
98 dialogStage.initModality(Modality.WINDOW_MODAL);
99 dialogStage.setTitle("Warning");
100 final Button button = new Button("Close");
101 final Text text = new Text(t.getData());
103 VBox box = new VBox();
104 box.setAlignment(Pos.CENTER);
106 box.setPadding(new Insets(10));
107 box.getChildren().addAll(text, button);
109 dialogStage.setScene(new Scene(box));
111 button.setCancelButton(true);
112 button.setOnAction(new EventHandler<ActionEvent>() {
114 public void handle(ActionEvent t) {
119 dialogStage.centerOnScreen();
120 dialogStage.showAndWait();
123 GridPane grid = new GridPane();
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(view);
129 getChildren().add(grid);
132 boolean initBck2Brwsr(WebEngine webEngine) {
133 JSObject jsobj = (JSObject) webEngine.executeScript("window");
134 LOG.log(Level.FINE, "window: {0}", jsobj);
135 Object prev = jsobj.getMember("bck2brwsr");
136 if ("undefined".equals(prev)) {
137 System.getProperties().put("webEngine", webEngine);
138 jsobj.setMember("bck2brwsr", bridge);
145 protected void layoutChildren() {
146 List<Node> managed = getManagedChildren();
147 double width = getWidth();
148 double height = getHeight();
149 double top = getInsets().getTop();
150 double right = getInsets().getRight();
151 double left = getInsets().getLeft();
152 double bottom = getInsets().getBottom();
153 for (int i = 0; i < managed.size(); i++) {
154 Node child = managed.get(i);
155 layoutInArea(child, left, top, width - left - right, height - top - bottom, 0, Insets.EMPTY, true, true, HPos.CENTER, VPos.CENTER);
159 private class InitBck2Brwsr implements ChangeListener<Void>, Runnable {
160 private final WebEngine eng;
162 public InitBck2Brwsr(WebEngine eng) {
167 public synchronized void changed(ObservableValue<? extends Void> ov, Void t, Void t1) {
168 Platform.runLater(this);
171 } catch (InterruptedException ex) {
172 LOG.log(Level.SEVERE, null, ex);
177 public synchronized void run() {