jaroslav@844: /** jaroslav@844: * Back 2 Browser Bytecode Translator jaroslav@844: * Copyright (C) 2012 Jaroslav Tulach jaroslav@844: * jaroslav@844: * This program is free software: you can redistribute it and/or modify jaroslav@844: * it under the terms of the GNU General Public License as published by jaroslav@844: * the Free Software Foundation, version 2 of the License. jaroslav@844: * jaroslav@844: * This program is distributed in the hope that it will be useful, jaroslav@844: * but WITHOUT ANY WARRANTY; without even the implied warranty of jaroslav@844: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the jaroslav@844: * GNU General Public License for more details. jaroslav@844: * jaroslav@844: * You should have received a copy of the GNU General Public License jaroslav@844: * along with this program. Look for COPYING file in the top folder. jaroslav@844: * If not, see http://opensource.org/licenses/GPL-2.0. jaroslav@844: */ jaroslav@844: package org.apidesign.bck2brwsr.launcher; jaroslav@844: jaroslav@844: import java.io.IOException; jaroslav@844: import java.lang.reflect.Method; jaroslav@844: import java.net.URI; jaroslav@844: import java.net.URL; jaroslav@844: import java.net.URLClassLoader; jaroslav@844: jaroslav@844: import java.util.List; jaroslav@844: import java.util.concurrent.Executors; jaroslav@844: import javafx.application.Application; jaroslav@844: import javafx.application.Platform; jaroslav@844: import javafx.beans.value.ChangeListener; jaroslav@844: import javafx.beans.value.ObservableValue; jaroslav@844: import javafx.event.ActionEvent; jaroslav@844: import javafx.event.EventHandler; jaroslav@844: import javafx.geometry.HPos; jaroslav@844: import javafx.geometry.Insets; jaroslav@844: import javafx.geometry.VPos; jaroslav@844: import javafx.scene.Node; jaroslav@844: import javafx.scene.Scene; jaroslav@844: import javafx.scene.control.Button; jaroslav@844: import javafx.scene.control.TextField; jaroslav@844: import javafx.scene.layout.ColumnConstraints; jaroslav@844: import javafx.scene.layout.GridPane; jaroslav@844: import javafx.scene.layout.Pane; jaroslav@844: import javafx.scene.layout.Priority; jaroslav@844: import javafx.scene.layout.VBox; jaroslav@844: import javafx.scene.web.WebEngine; jaroslav@844: import javafx.scene.web.WebView; jaroslav@844: import javafx.stage.Stage; jaroslav@844: jaroslav@844: /** jaroslav@844: * jaroslav@844: * @author Jaroslav Tulach jaroslav@844: */ jaroslav@844: final class WebViewLauncher extends Bck2BrwsrLauncher { jaroslav@844: static { jaroslav@844: try { jaroslav@844: Method m = URLClassLoader.class.getDeclaredMethod("addURL", URL.class); jaroslav@844: m.setAccessible(true); jaroslav@844: URL l = new URL("file://" + System.getProperty("java.home") + "/lib/jfxrt.jar"); jaroslav@844: System.err.println("url : " + l); jaroslav@844: m.invoke(ClassLoader.getSystemClassLoader(), l); jaroslav@844: } catch (Exception ex) { jaroslav@844: throw new LinkageError("Can't add jfxrt.jar on the classpath", ex); jaroslav@844: } jaroslav@844: } jaroslav@844: jaroslav@844: public WebViewLauncher() { jaroslav@844: super(null); jaroslav@844: } jaroslav@844: jaroslav@844: @Override jaroslav@844: protected Object[] showBrwsr(final URI url) throws IOException { jaroslav@844: try { jaroslav@844: Executors.newSingleThreadExecutor().submit(new Runnable() { jaroslav@844: @Override jaroslav@844: public void run() { jaroslav@844: WebViewBrowser.launch(WebViewBrowser.class, url.toString()); jaroslav@844: } jaroslav@844: }); jaroslav@844: } catch (Throwable ex) { jaroslav@844: ex.printStackTrace(); jaroslav@844: } jaroslav@844: return null; jaroslav@844: } jaroslav@844: jaroslav@844: @Override jaroslav@844: public void close() throws IOException { jaroslav@844: Platform.exit(); jaroslav@844: } jaroslav@844: jaroslav@844: jaroslav@844: jaroslav@844: /** jaroslav@844: * Demonstrates a WebView object accessing a web page. jaroslav@844: * jaroslav@844: * @see javafx.scene.web.WebView jaroslav@844: * @see javafx.scene.web.WebEngine jaroslav@844: */ jaroslav@844: public static class WebViewBrowser extends Application { jaroslav@844: jaroslav@844: @Override jaroslav@844: public void start(Stage primaryStage) throws Exception { jaroslav@844: Pane root = new WebViewPane(getParameters().getUnnamed()); jaroslav@844: primaryStage.setScene(new Scene(root, 1024, 768)); jaroslav@844: primaryStage.show(); jaroslav@844: } jaroslav@844: jaroslav@844: /** jaroslav@844: * Create a resizable WebView pane jaroslav@844: */ jaroslav@844: public class WebViewPane extends Pane { jaroslav@844: jaroslav@844: public WebViewPane(List params) { jaroslav@844: VBox.setVgrow(this, Priority.ALWAYS); jaroslav@844: setMaxWidth(Double.MAX_VALUE); jaroslav@844: setMaxHeight(Double.MAX_VALUE); jaroslav@844: jaroslav@844: WebView view = new WebView(); jaroslav@844: view.setMinSize(500, 400); jaroslav@844: view.setPrefSize(500, 400); jaroslav@844: final WebEngine eng = view.getEngine(); jaroslav@844: final TextField locationField = new TextField(); jaroslav@844: System.err.println("params : " + params); jaroslav@844: if (params.size() > 0) { jaroslav@844: eng.load(params.get(0)); jaroslav@844: locationField.setText(params.get(0)); jaroslav@844: } jaroslav@844: locationField.setMaxHeight(Double.MAX_VALUE); jaroslav@844: Button goButton = new Button("Go"); jaroslav@844: goButton.setDefaultButton(true); jaroslav@844: EventHandler goAction = new EventHandler() { jaroslav@844: @Override jaroslav@844: public void handle(ActionEvent event) { jaroslav@844: eng.load(locationField.getText().startsWith("http://") ? locationField.getText() jaroslav@844: : "http://" + locationField.getText()); jaroslav@844: } jaroslav@844: }; jaroslav@844: goButton.setOnAction(goAction); jaroslav@844: locationField.setOnAction(goAction); jaroslav@844: eng.locationProperty().addListener(new ChangeListener() { jaroslav@844: @Override jaroslav@844: public void changed(ObservableValue observable, String oldValue, String newValue) { jaroslav@844: locationField.setText(newValue); jaroslav@844: } jaroslav@844: }); jaroslav@844: GridPane grid = new GridPane(); jaroslav@844: grid.setVgap(5); jaroslav@844: grid.setHgap(5); jaroslav@844: GridPane.setConstraints(locationField, 0, 0, 1, 1, HPos.CENTER, VPos.CENTER, Priority.ALWAYS, Priority.SOMETIMES); jaroslav@844: GridPane.setConstraints(goButton, 1, 0); jaroslav@844: GridPane.setConstraints(view, 0, 1, 2, 1, HPos.CENTER, VPos.CENTER, Priority.ALWAYS, Priority.ALWAYS); jaroslav@844: grid.getColumnConstraints().addAll( jaroslav@844: new ColumnConstraints(100, 100, Double.MAX_VALUE, Priority.ALWAYS, HPos.CENTER, true), jaroslav@844: new ColumnConstraints(40, 40, 40, Priority.NEVER, HPos.CENTER, true)); jaroslav@844: grid.getChildren().addAll(locationField, goButton, view); jaroslav@844: getChildren().add(grid); jaroslav@844: } jaroslav@844: jaroslav@844: @Override jaroslav@844: protected void layoutChildren() { jaroslav@844: List managed = getManagedChildren(); jaroslav@844: double width = getWidth(); jaroslav@844: double height = getHeight(); jaroslav@844: double top = getInsets().getTop(); jaroslav@844: double right = getInsets().getRight(); jaroslav@844: double left = getInsets().getLeft(); jaroslav@844: double bottom = getInsets().getBottom(); jaroslav@844: for (int i = 0; i < managed.size(); i++) { jaroslav@844: Node child = managed.get(i); jaroslav@844: layoutInArea(child, left, top, jaroslav@844: width - left - right, height - top - bottom, jaroslav@844: 0, Insets.EMPTY, true, true, HPos.CENTER, VPos.CENTER); jaroslav@844: } jaroslav@844: } jaroslav@844: } jaroslav@844: } jaroslav@844: jaroslav@844: }