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.fximpl;
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.control.ToolBar;
38 import javafx.scene.layout.BorderPane;
39 import javafx.scene.layout.ColumnConstraints;
40 import javafx.scene.layout.GridPane;
41 import javafx.scene.layout.HBox;
42 import javafx.scene.layout.Pane;
43 import javafx.scene.layout.Priority;
44 import javafx.scene.layout.VBox;
45 import javafx.scene.text.Text;
46 import javafx.scene.web.WebEngine;
47 import javafx.scene.web.WebEvent;
48 import javafx.scene.web.WebView;
49 import javafx.stage.Modality;
50 import javafx.stage.Stage;
51 import netscape.javascript.JSObject;
54 * Demonstrates a WebView object accessing a web page.
56 * @see javafx.scene.web.WebView
57 * @see javafx.scene.web.WebEngine
59 public class FXBrwsr extends Application {
60 private static final Logger LOG = Logger.getLogger(FXBrwsr.class.getName());
63 public void start(Stage primaryStage) throws Exception {
64 WebView view = new WebView();
65 WebController wc = new WebController(view, getParameters().getUnnamed());
67 final VBox vbox = new VBox();
68 vbox.setAlignment( Pos.CENTER );
69 vbox.setStyle( "-fx-background-color: #808080;");
72 HBox hbox = new HBox();
73 hbox.setStyle( "-fx-background-color: #808080;");
74 hbox.setAlignment(Pos.CENTER);
75 hbox.getChildren().add(vbox);
76 vbox.getChildren().add(view);
78 BorderPane root = new BorderPane();
79 if ("true".equals(this.getParameters().getNamed().get("toolbar"))) { // NOI18N
80 final ToolBar toolbar = BrowserToolbar.create(view, vbox);
81 root.setTop( toolbar );
85 Scene scene = new Scene(root, 800, 600);
87 primaryStage.setTitle( "Device Emulator" );
88 primaryStage.setScene( scene );
93 * Create a resizable WebView pane
95 private static class WebController {
96 private final JVMBridge bridge = new JVMBridge();
98 public WebController(WebView view, List<String> params) {
99 LOG.log(Level.INFO, "Initializing WebView with {0}", params);
100 final WebEngine eng = view.getEngine();
102 JVMBridge.addBck2BrwsrLoad(new InitBck2Brwsr(eng));
103 } catch (TooManyListenersException ex) {
104 LOG.log(Level.SEVERE, null, ex);
107 if (params.size() > 0) {
108 LOG.log(Level.INFO, "loading page {0}", params.get(0));
109 eng.load(params.get(0));
110 LOG.fine("back from load");
112 eng.setOnAlert(new EventHandler<WebEvent<String>>() {
114 public void handle(WebEvent<String> t) {
115 final Stage dialogStage = new Stage();
116 dialogStage.initModality(Modality.WINDOW_MODAL);
117 dialogStage.setTitle("Warning");
118 final Button button = new Button("Close");
119 final Text text = new Text(t.getData());
121 VBox box = new VBox();
122 box.setAlignment(Pos.CENTER);
124 box.setPadding(new Insets(10));
125 box.getChildren().addAll(text, button);
127 dialogStage.setScene(new Scene(box));
129 button.setCancelButton(true);
130 button.setOnAction(new EventHandler<ActionEvent>() {
132 public void handle(ActionEvent t) {
137 dialogStage.centerOnScreen();
138 dialogStage.showAndWait();
143 boolean initBck2Brwsr(WebEngine webEngine) {
144 JSObject jsobj = (JSObject) webEngine.executeScript("window");
145 LOG.log(Level.FINE, "window: {0}", jsobj);
146 Object prev = jsobj.getMember("bck2brwsr");
147 if ("undefined".equals(prev)) {
148 System.getProperties().put("webEngine", webEngine);
149 jsobj.setMember("bck2brwsr", bridge);
155 private class InitBck2Brwsr implements ChangeListener<Void>, Runnable {
156 private final WebEngine eng;
158 public InitBck2Brwsr(WebEngine eng) {
163 public synchronized void changed(ObservableValue<? extends Void> ov, Void t, Void t1) {
164 Platform.runLater(this);
167 } catch (InterruptedException ex) {
168 LOG.log(Level.SEVERE, null, ex);
173 public synchronized void run() {