rt/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/WebViewLauncher.java
branchfx
changeset 845 859804c78010
parent 844 023cda5b8b0b
child 853 39166e462f8d
     1.1 --- a/rt/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/WebViewLauncher.java	Wed Mar 13 16:20:03 2013 +0100
     1.2 +++ b/rt/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/WebViewLauncher.java	Thu Mar 14 09:22:28 2013 +0100
     1.3 @@ -17,47 +17,32 @@
     1.4   */
     1.5  package org.apidesign.bck2brwsr.launcher;
     1.6  
     1.7 +import org.apidesign.bck2brwsr.launcher.impl.FXBrwsr;
     1.8  import java.io.IOException;
     1.9  import java.lang.reflect.Method;
    1.10  import java.net.URI;
    1.11  import java.net.URL;
    1.12  import java.net.URLClassLoader;
    1.13  
    1.14 -import java.util.List;
    1.15  import java.util.concurrent.Executors;
    1.16 -import javafx.application.Application;
    1.17 +import java.util.logging.Level;
    1.18 +import java.util.logging.Logger;
    1.19  import javafx.application.Platform;
    1.20 -import javafx.beans.value.ChangeListener;
    1.21 -import javafx.beans.value.ObservableValue;
    1.22 -import javafx.event.ActionEvent;
    1.23 -import javafx.event.EventHandler;
    1.24 -import javafx.geometry.HPos;
    1.25 -import javafx.geometry.Insets;
    1.26 -import javafx.geometry.VPos;
    1.27 -import javafx.scene.Node;
    1.28 -import javafx.scene.Scene;
    1.29 -import javafx.scene.control.Button;
    1.30 -import javafx.scene.control.TextField;
    1.31 -import javafx.scene.layout.ColumnConstraints;
    1.32 -import javafx.scene.layout.GridPane;
    1.33 -import javafx.scene.layout.Pane;
    1.34 -import javafx.scene.layout.Priority;
    1.35 -import javafx.scene.layout.VBox;
    1.36 -import javafx.scene.web.WebEngine;
    1.37 -import javafx.scene.web.WebView;
    1.38 -import javafx.stage.Stage;
    1.39 +import org.apidesign.bck2brwsr.launcher.impl.JVMBridge;
    1.40 +import org.apidesign.vm4brwsr.Bck2Brwsr;
    1.41  
    1.42  /**
    1.43   *
    1.44   * @author Jaroslav Tulach <jtulach@netbeans.org>
    1.45   */
    1.46  final class WebViewLauncher extends Bck2BrwsrLauncher {
    1.47 +    private static final Logger LOG = Logger.getLogger(WebViewLauncher.class.getName());
    1.48      static {
    1.49          try {
    1.50              Method m = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
    1.51              m.setAccessible(true);
    1.52              URL l = new URL("file://" + System.getProperty("java.home") + "/lib/jfxrt.jar");
    1.53 -            System.err.println("url : " + l);
    1.54 +            LOG.log(Level.INFO, "url : {0}", l);
    1.55              m.invoke(ClassLoader.getSystemClassLoader(), l);
    1.56          } catch (Exception ex) {
    1.57              throw new LinkageError("Can't add jfxrt.jar on the classpath", ex);
    1.58 @@ -71,108 +56,39 @@
    1.59      @Override
    1.60      protected Object[] showBrwsr(final URI url) throws IOException {
    1.61          try {
    1.62 +            JVMBridge.registerClassLoaders(loaders());
    1.63              Executors.newSingleThreadExecutor().submit(new Runnable() {
    1.64                  @Override
    1.65                  public void run() {
    1.66 -                    WebViewBrowser.launch(WebViewBrowser.class, url.toString());
    1.67 +                    FXBrwsr.launch(FXBrwsr.class, url.toString());
    1.68                  }
    1.69              });
    1.70          } catch (Throwable ex) {
    1.71 -            ex.printStackTrace();
    1.72 +            LOG.log(Level.WARNING, "Can't open WebView", ex);
    1.73          }
    1.74          return null;
    1.75      }
    1.76 -
    1.77 +    
    1.78 +    @Override
    1.79 +    void generateBck2BrwsrJS(StringBuilder sb, Bck2Brwsr.Resources loader) throws IOException {
    1.80 +        sb.append("(function() {\n"
    1.81 +            + "  this.jvmBridge = new Array();\n"
    1.82 +            + "  this.jvmBridge.loadClass = Array.prototype.push;\n"
    1.83 +            + "  var bridge = this.jvmBridge;\n"
    1.84 +            + "  this.bck2brwsr = function() { return {\n"
    1.85 +            + "     loadClass : function(name) {\n"
    1.86 +            + "       return bridge.loadClass(name);\n"
    1.87 +            + "     }"
    1.88 +            + "  }; };\n"
    1.89 +            + "})(window);\n"
    1.90 +        );
    1.91 +    }
    1.92 +    
    1.93 +    
    1.94 +    
    1.95      @Override
    1.96      public void close() throws IOException {
    1.97          Platform.exit();
    1.98      }
    1.99      
   1.100 -    
   1.101 -
   1.102 -    /**
   1.103 -     * Demonstrates a WebView object accessing a web page.
   1.104 -     *
   1.105 -     * @see javafx.scene.web.WebView
   1.106 -     * @see javafx.scene.web.WebEngine
   1.107 -     */
   1.108 -    public static class WebViewBrowser extends Application {
   1.109 -
   1.110 -        @Override
   1.111 -        public void start(Stage primaryStage) throws Exception {
   1.112 -            Pane root = new WebViewPane(getParameters().getUnnamed());
   1.113 -            primaryStage.setScene(new Scene(root, 1024, 768));
   1.114 -            primaryStage.show();
   1.115 -        }
   1.116 -
   1.117 -        /**
   1.118 -         * Create a resizable WebView pane
   1.119 -         */
   1.120 -        public class WebViewPane extends Pane {
   1.121 -
   1.122 -            public WebViewPane(List<String> params) {
   1.123 -                VBox.setVgrow(this, Priority.ALWAYS);
   1.124 -                setMaxWidth(Double.MAX_VALUE);
   1.125 -                setMaxHeight(Double.MAX_VALUE);
   1.126 -
   1.127 -                WebView view = new WebView();
   1.128 -                view.setMinSize(500, 400);
   1.129 -                view.setPrefSize(500, 400);
   1.130 -                final WebEngine eng = view.getEngine();
   1.131 -                final TextField locationField = new TextField();
   1.132 -                System.err.println("params : " + params);
   1.133 -                if (params.size() > 0) {
   1.134 -                    eng.load(params.get(0));
   1.135 -                    locationField.setText(params.get(0));
   1.136 -                }
   1.137 -                locationField.setMaxHeight(Double.MAX_VALUE);
   1.138 -                Button goButton = new Button("Go");
   1.139 -                goButton.setDefaultButton(true);
   1.140 -                EventHandler<ActionEvent> goAction = new EventHandler<ActionEvent>() {
   1.141 -                    @Override
   1.142 -                    public void handle(ActionEvent event) {
   1.143 -                        eng.load(locationField.getText().startsWith("http://") ? locationField.getText()
   1.144 -                            : "http://" + locationField.getText());
   1.145 -                    }
   1.146 -                };
   1.147 -                goButton.setOnAction(goAction);
   1.148 -                locationField.setOnAction(goAction);
   1.149 -                eng.locationProperty().addListener(new ChangeListener<String>() {
   1.150 -                    @Override
   1.151 -                    public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
   1.152 -                        locationField.setText(newValue);
   1.153 -                    }
   1.154 -                });
   1.155 -                GridPane grid = new GridPane();
   1.156 -                grid.setVgap(5);
   1.157 -                grid.setHgap(5);
   1.158 -                GridPane.setConstraints(locationField, 0, 0, 1, 1, HPos.CENTER, VPos.CENTER, Priority.ALWAYS, Priority.SOMETIMES);
   1.159 -                GridPane.setConstraints(goButton, 1, 0);
   1.160 -                GridPane.setConstraints(view, 0, 1, 2, 1, HPos.CENTER, VPos.CENTER, Priority.ALWAYS, Priority.ALWAYS);
   1.161 -                grid.getColumnConstraints().addAll(
   1.162 -                    new ColumnConstraints(100, 100, Double.MAX_VALUE, Priority.ALWAYS, HPos.CENTER, true),
   1.163 -                    new ColumnConstraints(40, 40, 40, Priority.NEVER, HPos.CENTER, true));
   1.164 -                grid.getChildren().addAll(locationField, goButton, view);
   1.165 -                getChildren().add(grid);
   1.166 -            }
   1.167 -
   1.168 -            @Override
   1.169 -            protected void layoutChildren() {
   1.170 -                List<Node> managed = getManagedChildren();
   1.171 -                double width = getWidth();
   1.172 -                double height = getHeight();
   1.173 -                double top = getInsets().getTop();
   1.174 -                double right = getInsets().getRight();
   1.175 -                double left = getInsets().getLeft();
   1.176 -                double bottom = getInsets().getBottom();
   1.177 -                for (int i = 0; i < managed.size(); i++) {
   1.178 -                    Node child = managed.get(i);
   1.179 -                    layoutInArea(child, left, top,
   1.180 -                        width - left - right, height - top - bottom,
   1.181 -                        0, Insets.EMPTY, true, true, HPos.CENTER, VPos.CENTER);
   1.182 -                }
   1.183 -            }
   1.184 -        }
   1.185 -    }
   1.186 -    
   1.187  }