Using FX dialog. Intercepting just the first alert('bck2brwsr') call.
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.logging.Level;
22 import java.util.logging.Logger;
23 import javafx.application.Application;
24 import javafx.beans.value.ChangeListener;
25 import javafx.beans.value.ObservableValue;
26 import javafx.event.ActionEvent;
27 import javafx.event.EventHandler;
28 import javafx.geometry.HPos;
29 import javafx.geometry.Insets;
30 import javafx.geometry.Pos;
31 import javafx.geometry.VPos;
32 import javafx.scene.Node;
33 import javafx.scene.Scene;
34 import javafx.scene.control.Button;
35 import javafx.scene.control.TextField;
36 import javafx.scene.layout.ColumnConstraints;
37 import javafx.scene.layout.GridPane;
38 import javafx.scene.layout.Pane;
39 import javafx.scene.layout.Priority;
40 import javafx.scene.layout.VBox;
41 import javafx.scene.text.Text;
42 import javafx.scene.web.WebEngine;
43 import javafx.scene.web.WebEvent;
44 import javafx.scene.web.WebView;
45 import javafx.stage.Modality;
46 import javafx.stage.Stage;
47 import netscape.javascript.JSObject;
50 * Demonstrates a WebView object accessing a web page.
52 * @see javafx.scene.web.WebView
53 * @see javafx.scene.web.WebEngine
55 public class FXBrwsr extends Application {
56 private static final Logger LOG = Logger.getLogger(FXBrwsr.class.getName());
59 public void start(Stage primaryStage) throws Exception {
60 Pane root = new WebViewPane(getParameters().getUnnamed());
61 primaryStage.setScene(new Scene(root, 1024, 768));
66 * Create a resizable WebView pane
68 private class WebViewPane extends Pane {
69 private final JVMBridge bridge = new JVMBridge();
71 public WebViewPane(List<String> params) {
72 VBox.setVgrow(this, Priority.ALWAYS);
73 setMaxWidth(Double.MAX_VALUE);
74 setMaxHeight(Double.MAX_VALUE);
75 WebView view = new WebView();
76 view.setMinSize(500, 400);
77 view.setPrefSize(500, 400);
78 final WebEngine eng = view.getEngine();
79 final TextField locationField = new TextField();
80 LOG.log(Level.FINE, "params : {0}", params);
81 if (params.size() > 0) {
82 LOG.log(Level.FINE, "loading page {0}", params.get(0));
83 eng.load(params.get(0));
84 LOG.fine("done loading page ");
85 locationField.setText(params.get(0));
87 locationField.setMaxHeight(Double.MAX_VALUE);
88 Button goButton = new Button("Go");
89 goButton.setDefaultButton(true);
90 EventHandler<ActionEvent> goAction = new EventHandler<ActionEvent>() {
92 public void handle(ActionEvent event) {
93 eng.load(locationField.getText().startsWith("http://") ? locationField.getText() : "http://" + locationField.getText());
96 goButton.setOnAction(goAction);
97 locationField.setOnAction(goAction);
98 eng.locationProperty().addListener(new ChangeListener<String>() {
100 public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
101 locationField.setText(newValue);
104 eng.setOnAlert(new EventHandler<WebEvent<String>>() {
106 public void handle(WebEvent<String> t) {
107 if ("bck2brwsr".equals(t.getData())) { // NOI18N
108 if (initBck2Brwsr(eng)) {
109 // ignore initial message
113 final Stage dialogStage = new Stage();
114 dialogStage.initModality(Modality.WINDOW_MODAL);
115 dialogStage.setTitle("Warning");
116 final Button button = new Button("Close");
117 final Text text = new Text(t.getData());
119 VBox box = new VBox();
120 box.setAlignment(Pos.CENTER);
122 box.setPadding(new Insets(10));
123 box.getChildren().addAll(text, button);
125 dialogStage.setScene(new Scene(box));
127 button.setCancelButton(true);
128 button.setOnAction(new EventHandler<ActionEvent>() {
130 public void handle(ActionEvent t) {
135 dialogStage.centerOnScreen();
136 dialogStage.showAndWait();
139 GridPane grid = new GridPane();
142 GridPane.setConstraints(locationField, 0, 0, 1, 1, HPos.CENTER, VPos.CENTER, Priority.ALWAYS, Priority.SOMETIMES);
143 GridPane.setConstraints(goButton, 1, 0);
144 GridPane.setConstraints(view, 0, 1, 2, 1, HPos.CENTER, VPos.CENTER, Priority.ALWAYS, Priority.ALWAYS);
145 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));
146 grid.getChildren().addAll(locationField, goButton, view);
147 getChildren().add(grid);
150 boolean initBck2Brwsr(WebEngine webEngine) {
151 JSObject jsobj = (JSObject) webEngine.executeScript("window");
152 LOG.log(Level.FINE, "window: {0}", jsobj);
153 Object prev = jsobj.getMember("bck2brwsr");
154 if ("undefined".equals(prev)) {
155 System.getProperties().put("webEngine", webEngine);
156 jsobj.setMember("bck2brwsr", bridge);
163 protected void layoutChildren() {
164 List<Node> managed = getManagedChildren();
165 double width = getWidth();
166 double height = getHeight();
167 double top = getInsets().getTop();
168 double right = getInsets().getRight();
169 double left = getInsets().getLeft();
170 double bottom = getInsets().getBottom();
171 for (int i = 0; i < managed.size(); i++) {
172 Node child = managed.get(i);
173 layoutInArea(child, left, top, width - left - right, height - top - bottom, 0, Insets.EMPTY, true, true, HPos.CENTER, VPos.CENTER);