rt/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/WebViewLauncher.java
branchfx
changeset 844 023cda5b8b0b
child 845 859804c78010
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/rt/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/WebViewLauncher.java	Wed Mar 13 16:20:03 2013 +0100
     1.3 @@ -0,0 +1,178 @@
     1.4 +/**
     1.5 + * Back 2 Browser Bytecode Translator
     1.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     1.7 + *
     1.8 + * This program is free software: you can redistribute it and/or modify
     1.9 + * it under the terms of the GNU General Public License as published by
    1.10 + * the Free Software Foundation, version 2 of the License.
    1.11 + *
    1.12 + * This program is distributed in the hope that it will be useful,
    1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.15 + * GNU General Public License for more details.
    1.16 + *
    1.17 + * You should have received a copy of the GNU General Public License
    1.18 + * along with this program. Look for COPYING file in the top folder.
    1.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
    1.20 + */
    1.21 +package org.apidesign.bck2brwsr.launcher;
    1.22 +
    1.23 +import java.io.IOException;
    1.24 +import java.lang.reflect.Method;
    1.25 +import java.net.URI;
    1.26 +import java.net.URL;
    1.27 +import java.net.URLClassLoader;
    1.28 +
    1.29 +import java.util.List;
    1.30 +import java.util.concurrent.Executors;
    1.31 +import javafx.application.Application;
    1.32 +import javafx.application.Platform;
    1.33 +import javafx.beans.value.ChangeListener;
    1.34 +import javafx.beans.value.ObservableValue;
    1.35 +import javafx.event.ActionEvent;
    1.36 +import javafx.event.EventHandler;
    1.37 +import javafx.geometry.HPos;
    1.38 +import javafx.geometry.Insets;
    1.39 +import javafx.geometry.VPos;
    1.40 +import javafx.scene.Node;
    1.41 +import javafx.scene.Scene;
    1.42 +import javafx.scene.control.Button;
    1.43 +import javafx.scene.control.TextField;
    1.44 +import javafx.scene.layout.ColumnConstraints;
    1.45 +import javafx.scene.layout.GridPane;
    1.46 +import javafx.scene.layout.Pane;
    1.47 +import javafx.scene.layout.Priority;
    1.48 +import javafx.scene.layout.VBox;
    1.49 +import javafx.scene.web.WebEngine;
    1.50 +import javafx.scene.web.WebView;
    1.51 +import javafx.stage.Stage;
    1.52 +
    1.53 +/**
    1.54 + *
    1.55 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    1.56 + */
    1.57 +final class WebViewLauncher extends Bck2BrwsrLauncher {
    1.58 +    static {
    1.59 +        try {
    1.60 +            Method m = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
    1.61 +            m.setAccessible(true);
    1.62 +            URL l = new URL("file://" + System.getProperty("java.home") + "/lib/jfxrt.jar");
    1.63 +            System.err.println("url : " + l);
    1.64 +            m.invoke(ClassLoader.getSystemClassLoader(), l);
    1.65 +        } catch (Exception ex) {
    1.66 +            throw new LinkageError("Can't add jfxrt.jar on the classpath", ex);
    1.67 +        }
    1.68 +    }
    1.69 +    
    1.70 +    public WebViewLauncher() {
    1.71 +        super(null);
    1.72 +    }
    1.73 +
    1.74 +    @Override
    1.75 +    protected Object[] showBrwsr(final URI url) throws IOException {
    1.76 +        try {
    1.77 +            Executors.newSingleThreadExecutor().submit(new Runnable() {
    1.78 +                @Override
    1.79 +                public void run() {
    1.80 +                    WebViewBrowser.launch(WebViewBrowser.class, url.toString());
    1.81 +                }
    1.82 +            });
    1.83 +        } catch (Throwable ex) {
    1.84 +            ex.printStackTrace();
    1.85 +        }
    1.86 +        return null;
    1.87 +    }
    1.88 +
    1.89 +    @Override
    1.90 +    public void close() throws IOException {
    1.91 +        Platform.exit();
    1.92 +    }
    1.93 +    
    1.94 +    
    1.95 +
    1.96 +    /**
    1.97 +     * Demonstrates a WebView object accessing a web page.
    1.98 +     *
    1.99 +     * @see javafx.scene.web.WebView
   1.100 +     * @see javafx.scene.web.WebEngine
   1.101 +     */
   1.102 +    public static class WebViewBrowser extends Application {
   1.103 +
   1.104 +        @Override
   1.105 +        public void start(Stage primaryStage) throws Exception {
   1.106 +            Pane root = new WebViewPane(getParameters().getUnnamed());
   1.107 +            primaryStage.setScene(new Scene(root, 1024, 768));
   1.108 +            primaryStage.show();
   1.109 +        }
   1.110 +
   1.111 +        /**
   1.112 +         * Create a resizable WebView pane
   1.113 +         */
   1.114 +        public class WebViewPane extends Pane {
   1.115 +
   1.116 +            public WebViewPane(List<String> params) {
   1.117 +                VBox.setVgrow(this, Priority.ALWAYS);
   1.118 +                setMaxWidth(Double.MAX_VALUE);
   1.119 +                setMaxHeight(Double.MAX_VALUE);
   1.120 +
   1.121 +                WebView view = new WebView();
   1.122 +                view.setMinSize(500, 400);
   1.123 +                view.setPrefSize(500, 400);
   1.124 +                final WebEngine eng = view.getEngine();
   1.125 +                final TextField locationField = new TextField();
   1.126 +                System.err.println("params : " + params);
   1.127 +                if (params.size() > 0) {
   1.128 +                    eng.load(params.get(0));
   1.129 +                    locationField.setText(params.get(0));
   1.130 +                }
   1.131 +                locationField.setMaxHeight(Double.MAX_VALUE);
   1.132 +                Button goButton = new Button("Go");
   1.133 +                goButton.setDefaultButton(true);
   1.134 +                EventHandler<ActionEvent> goAction = new EventHandler<ActionEvent>() {
   1.135 +                    @Override
   1.136 +                    public void handle(ActionEvent event) {
   1.137 +                        eng.load(locationField.getText().startsWith("http://") ? locationField.getText()
   1.138 +                            : "http://" + locationField.getText());
   1.139 +                    }
   1.140 +                };
   1.141 +                goButton.setOnAction(goAction);
   1.142 +                locationField.setOnAction(goAction);
   1.143 +                eng.locationProperty().addListener(new ChangeListener<String>() {
   1.144 +                    @Override
   1.145 +                    public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
   1.146 +                        locationField.setText(newValue);
   1.147 +                    }
   1.148 +                });
   1.149 +                GridPane grid = new GridPane();
   1.150 +                grid.setVgap(5);
   1.151 +                grid.setHgap(5);
   1.152 +                GridPane.setConstraints(locationField, 0, 0, 1, 1, HPos.CENTER, VPos.CENTER, Priority.ALWAYS, Priority.SOMETIMES);
   1.153 +                GridPane.setConstraints(goButton, 1, 0);
   1.154 +                GridPane.setConstraints(view, 0, 1, 2, 1, HPos.CENTER, VPos.CENTER, Priority.ALWAYS, Priority.ALWAYS);
   1.155 +                grid.getColumnConstraints().addAll(
   1.156 +                    new ColumnConstraints(100, 100, Double.MAX_VALUE, Priority.ALWAYS, HPos.CENTER, true),
   1.157 +                    new ColumnConstraints(40, 40, 40, Priority.NEVER, HPos.CENTER, true));
   1.158 +                grid.getChildren().addAll(locationField, goButton, view);
   1.159 +                getChildren().add(grid);
   1.160 +            }
   1.161 +
   1.162 +            @Override
   1.163 +            protected void layoutChildren() {
   1.164 +                List<Node> managed = getManagedChildren();
   1.165 +                double width = getWidth();
   1.166 +                double height = getHeight();
   1.167 +                double top = getInsets().getTop();
   1.168 +                double right = getInsets().getRight();
   1.169 +                double left = getInsets().getLeft();
   1.170 +                double bottom = getInsets().getBottom();
   1.171 +                for (int i = 0; i < managed.size(); i++) {
   1.172 +                    Node child = managed.get(i);
   1.173 +                    layoutInArea(child, left, top,
   1.174 +                        width - left - right, height - top - bottom,
   1.175 +                        0, Insets.EMPTY, true, true, HPos.CENTER, VPos.CENTER);
   1.176 +                }
   1.177 +            }
   1.178 +        }
   1.179 +    }
   1.180 +    
   1.181 +}