rt/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/WebViewLauncher.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 13 Mar 2013 16:20:03 +0100
branchfx
changeset 844 023cda5b8b0b
child 845 859804c78010
permissions -rw-r--r--
Initial attempt to use JavaFX: When running with -Dvmtest.brwsrs=fx, it runs the tests inside of WebView
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     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.
     8  *
     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.
    13  *
    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.
    17  */
    18 package org.apidesign.bck2brwsr.launcher;
    19 
    20 import java.io.IOException;
    21 import java.lang.reflect.Method;
    22 import java.net.URI;
    23 import java.net.URL;
    24 import java.net.URLClassLoader;
    25 
    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;
    49 
    50 /**
    51  *
    52  * @author Jaroslav Tulach <jtulach@netbeans.org>
    53  */
    54 final class WebViewLauncher extends Bck2BrwsrLauncher {
    55     static {
    56         try {
    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);
    64         }
    65     }
    66     
    67     public WebViewLauncher() {
    68         super(null);
    69     }
    70 
    71     @Override
    72     protected Object[] showBrwsr(final URI url) throws IOException {
    73         try {
    74             Executors.newSingleThreadExecutor().submit(new Runnable() {
    75                 @Override
    76                 public void run() {
    77                     WebViewBrowser.launch(WebViewBrowser.class, url.toString());
    78                 }
    79             });
    80         } catch (Throwable ex) {
    81             ex.printStackTrace();
    82         }
    83         return null;
    84     }
    85 
    86     @Override
    87     public void close() throws IOException {
    88         Platform.exit();
    89     }
    90     
    91     
    92 
    93     /**
    94      * Demonstrates a WebView object accessing a web page.
    95      *
    96      * @see javafx.scene.web.WebView
    97      * @see javafx.scene.web.WebEngine
    98      */
    99     public static class WebViewBrowser extends Application {
   100 
   101         @Override
   102         public void start(Stage primaryStage) throws Exception {
   103             Pane root = new WebViewPane(getParameters().getUnnamed());
   104             primaryStage.setScene(new Scene(root, 1024, 768));
   105             primaryStage.show();
   106         }
   107 
   108         /**
   109          * Create a resizable WebView pane
   110          */
   111         public class WebViewPane extends Pane {
   112 
   113             public WebViewPane(List<String> params) {
   114                 VBox.setVgrow(this, Priority.ALWAYS);
   115                 setMaxWidth(Double.MAX_VALUE);
   116                 setMaxHeight(Double.MAX_VALUE);
   117 
   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));
   127                 }
   128                 locationField.setMaxHeight(Double.MAX_VALUE);
   129                 Button goButton = new Button("Go");
   130                 goButton.setDefaultButton(true);
   131                 EventHandler<ActionEvent> goAction = new EventHandler<ActionEvent>() {
   132                     @Override
   133                     public void handle(ActionEvent event) {
   134                         eng.load(locationField.getText().startsWith("http://") ? locationField.getText()
   135                             : "http://" + locationField.getText());
   136                     }
   137                 };
   138                 goButton.setOnAction(goAction);
   139                 locationField.setOnAction(goAction);
   140                 eng.locationProperty().addListener(new ChangeListener<String>() {
   141                     @Override
   142                     public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
   143                         locationField.setText(newValue);
   144                     }
   145                 });
   146                 GridPane grid = new GridPane();
   147                 grid.setVgap(5);
   148                 grid.setHgap(5);
   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);
   157             }
   158 
   159             @Override
   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);
   173                 }
   174             }
   175         }
   176     }
   177     
   178 }