If the netbeans.inspect.port is on and working smootly enable reloading of the HTML page via popup menu
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 24 Aug 2013 16:56:40 +0200
changeset 2537c8bbc652f04
parent 252 cc71321770b8
child 254 ec06d7725d5b
If the netbeans.inspect.port is on and working smootly enable reloading of the HTML page via popup menu
boot-fx/src/main/java/org/apidesign/html/boot/fx/FXInspect.java
boot-fx/src/main/java/org/apidesign/html/boot/fx/FXPresenter.java
     1.1 --- a/boot-fx/src/main/java/org/apidesign/html/boot/fx/FXInspect.java	Sat Aug 24 13:02:30 2013 +0200
     1.2 +++ b/boot-fx/src/main/java/org/apidesign/html/boot/fx/FXInspect.java	Sat Aug 24 16:56:40 2013 +0200
     1.3 @@ -54,17 +54,19 @@
     1.4          initializeDebugger(output);
     1.5      }
     1.6      
     1.7 -    static void initialize(WebEngine engine) {
     1.8 +    static boolean initialize(WebEngine engine) {
     1.9          final int inspectPort = Integer.getInteger("netbeans.inspect.port", -1); // NOI18N
    1.10          if (inspectPort != -1) {
    1.11              try {
    1.12                  FXInspect inspector = new FXInspect(engine, inspectPort);
    1.13                  Thread t = new Thread(inspector, "FX<->NetBeans Inspector");
    1.14                  t.start();
    1.15 +                return true;
    1.16              } catch (IOException ex) {
    1.17                  LOG.log(Level.INFO, "Cannot connect to NetBeans IDE to port " + inspectPort, ex); // NOI18N
    1.18              }
    1.19          }
    1.20 +        return false;
    1.21      }
    1.22      
    1.23      private void initializeDebugger(final ObjectOutputStream output) {
     2.1 --- a/boot-fx/src/main/java/org/apidesign/html/boot/fx/FXPresenter.java	Sat Aug 24 13:02:30 2013 +0200
     2.2 +++ b/boot-fx/src/main/java/org/apidesign/html/boot/fx/FXPresenter.java	Sat Aug 24 16:56:40 2013 +0200
     2.3 @@ -85,6 +85,8 @@
     2.4          }
     2.5      }
     2.6      
     2.7 +    private List<String> scripts;
     2.8 +    private Runnable onLoad;
     2.9      private WebEngine engine;
    2.10      
    2.11      @Override
    2.12 @@ -117,20 +119,40 @@
    2.13              }
    2.14              sb.append(l).append('\n');
    2.15          }
    2.16 -        engine.executeScript(sb.toString());
    2.17 +        final String script = sb.toString();
    2.18 +        if (scripts != null) {
    2.19 +            scripts.add(script);
    2.20 +        }
    2.21 +        engine.executeScript(script);
    2.22 +    }
    2.23 +    
    2.24 +    final void onPageLoad() {
    2.25 +        if (scripts != null) {
    2.26 +            for (String s : scripts) {
    2.27 +                engine.executeScript(s);
    2.28 +            }
    2.29 +        }
    2.30 +        onLoad.run();
    2.31      }
    2.32  
    2.33      @Override
    2.34 -    public void displayPage(final URL resource, Runnable onLoad) {
    2.35 -        engine = FXBrwsr.findEngine(resource, onLoad);
    2.36 +    public void displayPage(final URL resource, final Runnable onLoad) {
    2.37 +        this.onLoad = onLoad;
    2.38 +        final WebView view = FXBrwsr.findWebView(resource, this); 
    2.39 +        this.engine = view.getEngine();
    2.40          try {
    2.41 -            FXInspect.initialize(engine);
    2.42 +            if (FXInspect.initialize(engine)) {
    2.43 +                scripts = new ArrayList<String>();
    2.44 +            }
    2.45          } catch (Throwable ex) {
    2.46              ex.printStackTrace();
    2.47          }
    2.48          Platform.runLater(new Runnable() {
    2.49              @Override
    2.50              public void run() {
    2.51 +                if (scripts != null) {
    2.52 +                    view.setContextMenuEnabled(true);
    2.53 +                }
    2.54                  engine.load(resource.toExternalForm());
    2.55              }
    2.56          });
    2.57 @@ -174,7 +196,7 @@
    2.58  
    2.59          private static final Logger LOG = Logger.getLogger(FXBrwsr.class.getName());
    2.60          
    2.61 -        public synchronized static WebEngine findEngine(final URL url, final Runnable onLoad) {
    2.62 +        public synchronized static WebView findWebView(final URL url, final FXPresenter onLoad) {
    2.63              if (INSTANCE == null) {
    2.64                  Executors.newFixedThreadPool(1).submit(new Runnable() {
    2.65                      @Override
    2.66 @@ -197,12 +219,12 @@
    2.67                  }
    2.68              }
    2.69              if (!Platform.isFxApplicationThread()) {
    2.70 -                final WebEngine[] arr = { null };
    2.71 +                final WebView[] arr = { null };
    2.72                  final CountDownLatch waitForResult = new CountDownLatch(1);
    2.73                  Platform.runLater(new Runnable() {
    2.74                      @Override
    2.75                      public void run() {
    2.76 -                        arr[0] = INSTANCE.newEngine(url, onLoad);
    2.77 +                        arr[0] = INSTANCE.newView(url, onLoad);
    2.78                          waitForResult.countDown();
    2.79                      }
    2.80                  });
    2.81 @@ -216,7 +238,7 @@
    2.82                  }
    2.83                  return arr[0];
    2.84              } else {
    2.85 -                return INSTANCE.newEngine(url, onLoad);
    2.86 +                return INSTANCE.newView(url, onLoad);
    2.87              }
    2.88          }
    2.89  
    2.90 @@ -233,10 +255,38 @@
    2.91              this.root = r;
    2.92          }
    2.93  
    2.94 -        private WebEngine newEngine(final URL url, final Runnable onLoad) {
    2.95 +        private WebView newView(final URL url, final FXPresenter onLoad) {
    2.96              final WebView view = new WebView();
    2.97 -            final String nbUserDir = this.getParameters().getNamed().get("userdir"); // NOI18N
    2.98 -            WebController wc = new WebController(view, nbUserDir, getParameters().getUnnamed());
    2.99 +            view.setContextMenuEnabled(false);
   2.100 +            view.getEngine().setOnAlert(new EventHandler<WebEvent<String>>() {
   2.101 +                @Override
   2.102 +                public void handle(WebEvent<String> t) {
   2.103 +                    final Stage dialogStage = new Stage();
   2.104 +                    dialogStage.initModality(Modality.WINDOW_MODAL);
   2.105 +                    dialogStage.setTitle("Warning");
   2.106 +                    final Button button = new Button("Close");
   2.107 +                    final Text text = new Text(t.getData());
   2.108 +
   2.109 +                    VBox box = new VBox();
   2.110 +                    box.setAlignment(Pos.CENTER);
   2.111 +                    box.setSpacing(10);
   2.112 +                    box.setPadding(new Insets(10));
   2.113 +                    box.getChildren().addAll(text, button);
   2.114 +
   2.115 +                    dialogStage.setScene(new Scene(box));
   2.116 +
   2.117 +                    button.setCancelButton(true);
   2.118 +                    button.setOnAction(new EventHandler<ActionEvent>() {
   2.119 +                        @Override
   2.120 +                        public void handle(ActionEvent t) {
   2.121 +                            dialogStage.close();
   2.122 +                        }
   2.123 +                    });
   2.124 +
   2.125 +                    dialogStage.centerOnScreen();
   2.126 +                    dialogStage.showAndWait();
   2.127 +                }
   2.128 +            });
   2.129              root.setCenter(view);
   2.130  
   2.131              final Worker<Void> w = view.getEngine().getLoadWorker();
   2.132 @@ -244,77 +294,16 @@
   2.133                  @Override
   2.134                  public void changed(ObservableValue<? extends Worker.State> ov, Worker.State t, Worker.State newState) {
   2.135                      if (newState.equals(Worker.State.SUCCEEDED)) {
   2.136 -                        w.stateProperty().removeListener(this);
   2.137 -                        onLoad.run();
   2.138 +                        onLoad.onPageLoad();
   2.139                      }
   2.140                      if (newState.equals(Worker.State.FAILED)) {
   2.141                          throw new IllegalStateException("Failed to load " + url);
   2.142                      }
   2.143                  }
   2.144              });
   2.145 -            return view.getEngine();
   2.146 +            return view;
   2.147          }
   2.148  
   2.149 -        /**
   2.150 -         * Create a resizable WebView pane
   2.151 -         */
   2.152 -        private static class WebController {
   2.153 -            private final String ud;
   2.154 -
   2.155 -            public WebController(WebView view, String ud, List<String> params) {
   2.156 -                final WebEngine eng = view.getEngine();
   2.157 -//                this.bridge = new JVMBridge(view.getEngine());
   2.158 -                this.ud = ud;
   2.159 -                LOG.log(Level.INFO, "Initializing WebView with {0}", params);
   2.160 -
   2.161 -                if (params.size() > 0) {
   2.162 -                    LOG.log(Level.INFO, "loading page {0}", params.get(0));
   2.163 -                    eng.load(params.get(0));
   2.164 -                    LOG.fine("back from load");
   2.165 -                }
   2.166 -                eng.setOnAlert(new EventHandler<WebEvent<String>>() {
   2.167 -                    @Override
   2.168 -                    public void handle(WebEvent<String> t) {
   2.169 -                        final Stage dialogStage = new Stage();
   2.170 -                        dialogStage.initModality(Modality.WINDOW_MODAL);
   2.171 -                        dialogStage.setTitle("Warning");
   2.172 -                        final Button button = new Button("Close");
   2.173 -                        final Text text = new Text(t.getData());
   2.174 -
   2.175 -                        VBox box = new VBox();
   2.176 -                        box.setAlignment(Pos.CENTER);
   2.177 -                        box.setSpacing(10);
   2.178 -                        box.setPadding(new Insets(10));
   2.179 -                        box.getChildren().addAll(text, button);
   2.180 -
   2.181 -                        dialogStage.setScene(new Scene(box));
   2.182 -
   2.183 -                        button.setCancelButton(true);
   2.184 -                        button.setOnAction(new EventHandler<ActionEvent>() {
   2.185 -                            @Override
   2.186 -                            public void handle(ActionEvent t) {
   2.187 -                                dialogStage.close();
   2.188 -                            }
   2.189 -                        });
   2.190 -
   2.191 -                        dialogStage.centerOnScreen();
   2.192 -                        dialogStage.showAndWait();
   2.193 -                    }
   2.194 -                });
   2.195 -                /*
   2.196 -                WebDebug wd = null;
   2.197 -                try {
   2.198 -                    if (ud != null) {
   2.199 -                        wd = WebDebug.create(eng.impl_getDebugger(), ud);
   2.200 -                    }
   2.201 -                } catch (Exception ex) {
   2.202 -                    LOG.log(Level.WARNING, null, ex);
   2.203 -                }
   2.204 -                this.dbg = wd;
   2.205 -                   */
   2.206 -            }
   2.207 -
   2.208 -        }
   2.209          private static void waitFinished() {
   2.210              for (;;) {
   2.211                  try {