Initial attempt to use JavaFX: When running with -Dvmtest.brwsrs=fx, it runs the tests inside of WebView
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;
20 import java.io.IOException;
21 import java.lang.reflect.Method;
24 import java.net.URLClassLoader;
26 import java.util.List;
27 import java.util.concurrent.Executors;
28 import javafx.application.Application;
29 import javafx.application.Platform;
30 import javafx.beans.value.ChangeListener;
31 import javafx.beans.value.ObservableValue;
32 import javafx.event.ActionEvent;
33 import javafx.event.EventHandler;
34 import javafx.geometry.HPos;
35 import javafx.geometry.Insets;
36 import javafx.geometry.VPos;
37 import javafx.scene.Node;
38 import javafx.scene.Scene;
39 import javafx.scene.control.Button;
40 import javafx.scene.control.TextField;
41 import javafx.scene.layout.ColumnConstraints;
42 import javafx.scene.layout.GridPane;
43 import javafx.scene.layout.Pane;
44 import javafx.scene.layout.Priority;
45 import javafx.scene.layout.VBox;
46 import javafx.scene.web.WebEngine;
47 import javafx.scene.web.WebView;
48 import javafx.stage.Stage;
52 * @author Jaroslav Tulach <jtulach@netbeans.org>
54 final class WebViewLauncher extends Bck2BrwsrLauncher {
57 Method m = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
58 m.setAccessible(true);
59 URL l = new URL("file://" + System.getProperty("java.home") + "/lib/jfxrt.jar");
60 System.err.println("url : " + l);
61 m.invoke(ClassLoader.getSystemClassLoader(), l);
62 } catch (Exception ex) {
63 throw new LinkageError("Can't add jfxrt.jar on the classpath", ex);
67 public WebViewLauncher() {
72 protected Object[] showBrwsr(final URI url) throws IOException {
74 Executors.newSingleThreadExecutor().submit(new Runnable() {
77 WebViewBrowser.launch(WebViewBrowser.class, url.toString());
80 } catch (Throwable ex) {
87 public void close() throws IOException {
94 * Demonstrates a WebView object accessing a web page.
96 * @see javafx.scene.web.WebView
97 * @see javafx.scene.web.WebEngine
99 public static class WebViewBrowser extends Application {
102 public void start(Stage primaryStage) throws Exception {
103 Pane root = new WebViewPane(getParameters().getUnnamed());
104 primaryStage.setScene(new Scene(root, 1024, 768));
109 * Create a resizable WebView pane
111 public class WebViewPane extends Pane {
113 public WebViewPane(List<String> params) {
114 VBox.setVgrow(this, Priority.ALWAYS);
115 setMaxWidth(Double.MAX_VALUE);
116 setMaxHeight(Double.MAX_VALUE);
118 WebView view = new WebView();
119 view.setMinSize(500, 400);
120 view.setPrefSize(500, 400);
121 final WebEngine eng = view.getEngine();
122 final TextField locationField = new TextField();
123 System.err.println("params : " + params);
124 if (params.size() > 0) {
125 eng.load(params.get(0));
126 locationField.setText(params.get(0));
128 locationField.setMaxHeight(Double.MAX_VALUE);
129 Button goButton = new Button("Go");
130 goButton.setDefaultButton(true);
131 EventHandler<ActionEvent> goAction = new EventHandler<ActionEvent>() {
133 public void handle(ActionEvent event) {
134 eng.load(locationField.getText().startsWith("http://") ? locationField.getText()
135 : "http://" + locationField.getText());
138 goButton.setOnAction(goAction);
139 locationField.setOnAction(goAction);
140 eng.locationProperty().addListener(new ChangeListener<String>() {
142 public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
143 locationField.setText(newValue);
146 GridPane grid = new GridPane();
149 GridPane.setConstraints(locationField, 0, 0, 1, 1, HPos.CENTER, VPos.CENTER, Priority.ALWAYS, Priority.SOMETIMES);
150 GridPane.setConstraints(goButton, 1, 0);
151 GridPane.setConstraints(view, 0, 1, 2, 1, HPos.CENTER, VPos.CENTER, Priority.ALWAYS, Priority.ALWAYS);
152 grid.getColumnConstraints().addAll(
153 new ColumnConstraints(100, 100, Double.MAX_VALUE, Priority.ALWAYS, HPos.CENTER, true),
154 new ColumnConstraints(40, 40, 40, Priority.NEVER, HPos.CENTER, true));
155 grid.getChildren().addAll(locationField, goButton, view);
156 getChildren().add(grid);
160 protected void layoutChildren() {
161 List<Node> managed = getManagedChildren();
162 double width = getWidth();
163 double height = getHeight();
164 double top = getInsets().getTop();
165 double right = getInsets().getRight();
166 double left = getInsets().getLeft();
167 double bottom = getInsets().getBottom();
168 for (int i = 0; i < managed.size(); i++) {
169 Node child = managed.get(i);
170 layoutInArea(child, left, top,
171 width - left - right, height - top - bottom,
172 0, Insets.EMPTY, true, true, HPos.CENTER, VPos.CENTER);