Rather than piggybacking on first alert call, use the fact that the server and FX Web View are in the same VM and notify the view that bck2brwsr.js is about to be served from the server. fx
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 17 Apr 2013 17:04:40 +0200
branchfx
changeset 100404efef2a9c1e
parent 1003 bf8b1d7d76e0
child 1005 512984207634
Rather than piggybacking on first alert call, use the fact that the server and FX Web View are in the same VM and notify the view that bck2brwsr.js is about to be served from the server.
rt/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Bck2BrwsrLauncher.java
rt/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/WebViewLauncher.java
rt/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/impl/FXBrwsr.java
rt/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/impl/JVMBridge.java
     1.1 --- a/rt/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Bck2BrwsrLauncher.java	Wed Apr 17 13:46:06 2013 +0200
     1.2 +++ b/rt/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Bck2BrwsrLauncher.java	Wed Apr 17 17:04:40 2013 +0200
     1.3 @@ -148,7 +148,7 @@
     1.4  
     1.5          final ServerConfiguration conf = s.getServerConfiguration();
     1.6          if (addClasses) {
     1.7 -            conf.addHttpHandler(new VM(this, resources), "/bck2brwsr.js");
     1.8 +            conf.addHttpHandler(new VM(), "/bck2brwsr.js");
     1.9              conf.addHttpHandler(new Classes(resources), "/classes/");
    1.10          }
    1.11          return s;
    1.12 @@ -552,21 +552,14 @@
    1.13          
    1.14      }
    1.15  
    1.16 -    private static class VM extends HttpHandler {
    1.17 -        private final String bck2brwsr;
    1.18 -
    1.19 -        public VM(Bck2BrwsrLauncher l, Res loader) throws IOException {
    1.20 -            StringBuilder sb = new StringBuilder();
    1.21 -            l.generateBck2BrwsrJS(
    1.22 -                  sb, loader);
    1.23 -            this.bck2brwsr = sb.toString();
    1.24 -        }
    1.25 -
    1.26 +    private class VM extends HttpHandler {
    1.27          @Override
    1.28          public void service(Request request, Response response) throws Exception {
    1.29              response.setCharacterEncoding("UTF-8");
    1.30              response.setContentType("text/javascript");
    1.31 -            response.getWriter().write(bck2brwsr);
    1.32 +            StringBuilder sb = new StringBuilder();
    1.33 +            generateBck2BrwsrJS(sb, Bck2BrwsrLauncher.this.resources);
    1.34 +            response.getWriter().write(sb.toString());
    1.35          }
    1.36      }
    1.37  
     2.1 --- a/rt/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/WebViewLauncher.java	Wed Apr 17 13:46:06 2013 +0200
     2.2 +++ b/rt/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/WebViewLauncher.java	Wed Apr 17 17:04:40 2013 +0200
     2.3 @@ -79,11 +79,11 @@
     2.4      @Override
     2.5      void generateBck2BrwsrJS(StringBuilder sb, Bck2Brwsr.Resources loader) throws IOException {
     2.6          sb.append("(function() {\n"
     2.7 -            + "  alert('bck2brwsr');\n"
     2.8              + "  var impl = this.bck2brwsr;\n"
     2.9              + "  this.bck2brwsr = function() { return impl; };\n"
    2.10              + "})(window);\n"
    2.11          );
    2.12 +        JVMBridge.onBck2BrwsrLoad();
    2.13      }
    2.14      
    2.15      
     3.1 --- a/rt/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/impl/FXBrwsr.java	Wed Apr 17 13:46:06 2013 +0200
     3.2 +++ b/rt/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/impl/FXBrwsr.java	Wed Apr 17 17:04:40 2013 +0200
     3.3 @@ -18,9 +18,11 @@
     3.4  package org.apidesign.bck2brwsr.launcher.impl;
     3.5  
     3.6  import java.util.List;
     3.7 +import java.util.TooManyListenersException;
     3.8  import java.util.logging.Level;
     3.9  import java.util.logging.Logger;
    3.10  import javafx.application.Application;
    3.11 +import javafx.application.Platform;
    3.12  import javafx.beans.value.ChangeListener;
    3.13  import javafx.beans.value.ObservableValue;
    3.14  import javafx.event.ActionEvent;
    3.15 @@ -32,7 +34,6 @@
    3.16  import javafx.scene.Node;
    3.17  import javafx.scene.Scene;
    3.18  import javafx.scene.control.Button;
    3.19 -import javafx.scene.control.TextField;
    3.20  import javafx.scene.layout.ColumnConstraints;
    3.21  import javafx.scene.layout.GridPane;
    3.22  import javafx.scene.layout.Pane;
    3.23 @@ -77,6 +78,12 @@
    3.24              view.setPrefSize(500, 400);
    3.25              final WebEngine eng = view.getEngine();
    3.26              LOG.log(Level.FINE, "params : {0}", params);
    3.27 +            try {
    3.28 +                JVMBridge.addBck2BrwsrLoad(new InitBck2Brwsr(eng));
    3.29 +            } catch (TooManyListenersException ex) {
    3.30 +                LOG.log(Level.SEVERE, null, ex);
    3.31 +            }
    3.32 +            
    3.33              if (params.size() > 0) {
    3.34                  LOG.log(Level.FINE, "loading page {0}", params.get(0));
    3.35                  eng.load(params.get(0));
    3.36 @@ -85,12 +92,6 @@
    3.37              eng.setOnAlert(new EventHandler<WebEvent<String>>() {
    3.38                  @Override
    3.39                  public void handle(WebEvent<String> t) {
    3.40 -                    if ("bck2brwsr".equals(t.getData())) { // NOI18N
    3.41 -                        if (initBck2Brwsr(eng)) {
    3.42 -                            // ignore initial message
    3.43 -                            return;
    3.44 -                        }
    3.45 -                    }
    3.46                      final Stage dialogStage = new Stage();
    3.47                      dialogStage.initModality(Modality.WINDOW_MODAL);
    3.48                      dialogStage.setTitle("Warning");
    3.49 @@ -152,6 +153,30 @@
    3.50                  layoutInArea(child, left, top, width - left - right, height - top - bottom, 0, Insets.EMPTY, true, true, HPos.CENTER, VPos.CENTER);
    3.51              }
    3.52          }
    3.53 +
    3.54 +        private class InitBck2Brwsr implements ChangeListener<Void>, Runnable {
    3.55 +            private final WebEngine eng;
    3.56 +
    3.57 +            public InitBck2Brwsr(WebEngine eng) {
    3.58 +                this.eng = eng;
    3.59 +            }
    3.60 +
    3.61 +            @Override
    3.62 +            public synchronized void changed(ObservableValue<? extends Void> ov, Void t, Void t1) {
    3.63 +                Platform.runLater(this);
    3.64 +                try {
    3.65 +                    wait();
    3.66 +                } catch (InterruptedException ex) {
    3.67 +                    LOG.log(Level.SEVERE, null, ex);
    3.68 +                }
    3.69 +            }
    3.70 +
    3.71 +            @Override
    3.72 +            public synchronized void run() {
    3.73 +                initBck2Brwsr(eng);
    3.74 +                notifyAll();
    3.75 +            }
    3.76 +        }
    3.77      }
    3.78      
    3.79  }
     4.1 --- a/rt/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/impl/JVMBridge.java	Wed Apr 17 13:46:06 2013 +0200
     4.2 +++ b/rt/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/impl/JVMBridge.java	Wed Apr 17 17:04:40 2013 +0200
     4.3 @@ -17,17 +17,35 @@
     4.4   */
     4.5  package org.apidesign.bck2brwsr.launcher.impl;
     4.6  
     4.7 +import java.util.TooManyListenersException;
     4.8 +import javafx.beans.value.ChangeListener;
     4.9 +
    4.10  /**
    4.11   *
    4.12   * @author Jaroslav Tulach <jtulach@netbeans.org>
    4.13   */
    4.14  public final class JVMBridge {
    4.15      private static ClassLoader[] ldrs;
    4.16 +    private static ChangeListener<Void> onBck2BrwsrLoad;
    4.17          
    4.18      public static void registerClassLoaders(ClassLoader[] loaders) {
    4.19          ldrs = loaders.clone();
    4.20      }
    4.21      
    4.22 +    public static void addBck2BrwsrLoad(ChangeListener<Void> l) throws TooManyListenersException {
    4.23 +        if (onBck2BrwsrLoad != null) {
    4.24 +            throw new TooManyListenersException();
    4.25 +        }
    4.26 +        onBck2BrwsrLoad = l;
    4.27 +    }
    4.28 +
    4.29 +    public static void onBck2BrwsrLoad() {
    4.30 +        ChangeListener<Void> l = onBck2BrwsrLoad;
    4.31 +        if (l != null) {
    4.32 +            l.changed(null, null, null);
    4.33 +        }
    4.34 +    }
    4.35 +    
    4.36      public Class<?> loadClass(String name) throws ClassNotFoundException {
    4.37          System.err.println("trying to load " + name);
    4.38          ClassNotFoundException ex = null;