#252862: Remember state of FXWebView from previous application run
authorJaroslav Tulach <jtulach@netbeans.org>
Sun, 19 Jul 2015 14:04:03 +0200
changeset 953cd7d2aca34be
parent 952 f5d1e573de92
child 954 95755c3c836b
#252862: Remember state of FXWebView from previous application run
boot-fx/src/main/java/org/netbeans/html/boot/fx/AbstractFXPresenter.java
boot-fx/src/main/java/org/netbeans/html/boot/fx/FXBrwsr.java
boot-fx/src/test/java/org/netbeans/html/boot/fx/FXBrwsrTest.java
boot-fx/src/test/java/org/sample/app/pkg/SampleApp.java
     1.1 --- a/boot-fx/src/main/java/org/netbeans/html/boot/fx/AbstractFXPresenter.java	Sun Jul 19 13:09:06 2015 +0200
     1.2 +++ b/boot-fx/src/main/java/org/netbeans/html/boot/fx/AbstractFXPresenter.java	Sun Jul 19 14:04:03 2015 +0200
     1.3 @@ -88,7 +88,7 @@
     1.4              throw new IllegalStateException(ex);
     1.5          }
     1.6      }
     1.7 -    
     1.8 +
     1.9      @Override
    1.10      public Fn defineFn(String code, String... names) {
    1.11          return defineJSFn(code, names, null);
    1.12 @@ -98,9 +98,9 @@
    1.13      public Fn defineFn(String code, String[] names, boolean[] keepAlive) {
    1.14          return defineJSFn(code, names, keepAlive);
    1.15      }
    1.16 -    
    1.17 -    
    1.18 -    
    1.19 +
    1.20 +
    1.21 +
    1.22      final JSFn defineJSFn(String code, String[] names, boolean[] keepAlive) {
    1.23          StringBuilder sb = new StringBuilder();
    1.24          sb.append("(function() {\n");
    1.25 @@ -115,8 +115,8 @@
    1.26          sb.append("};\n");
    1.27          sb.append("})();\n");
    1.28          if (LOG.isLoggable(Level.FINE)) {
    1.29 -            LOG.log(Level.FINE, 
    1.30 -                "defining function #{0}:\n{1}\n", 
    1.31 +            LOG.log(Level.FINE,
    1.32 +                "defining function #{0}:\n{1}\n",
    1.33                  new Object[] { ++cnt, code }
    1.34              );
    1.35          }
    1.36 @@ -152,24 +152,9 @@
    1.37          }
    1.38      }
    1.39  
    1.40 -    private static String findCalleeClassName() {
    1.41 -        for (StackTraceElement e : new Exception().getStackTrace()) {
    1.42 -            String cn = e.getClassName();
    1.43 -            if (cn.startsWith("org.netbeans.html")) {
    1.44 -                continue;
    1.45 -            }
    1.46 -            if (cn.startsWith("net.java.html")) {
    1.47 -                continue;
    1.48 -            }
    1.49 -            return cn;
    1.50 -        }
    1.51 -        return "org.netbeans.html";
    1.52 -    }
    1.53 -
    1.54      @Override
    1.55      public void displayPage(final URL resource, final Runnable onLoad) {
    1.56          this.onLoad = onLoad;
    1.57 -        System.err.println("className: " + findCalleeClassName());
    1.58          final WebView view = findView(resource);
    1.59          this.engine = view.getEngine();
    1.60          boolean inspectOn = false;
    1.61 @@ -211,7 +196,7 @@
    1.62      protected abstract void waitFinished();
    1.63  
    1.64      protected abstract WebView findView(final URL resource);
    1.65 -    
    1.66 +
    1.67      final JSObject convertArrays(Object[] arr) {
    1.68          for (int i = 0; i < arr.length; i++) {
    1.69              if (arr[i] instanceof Object[]) {
    1.70 @@ -280,7 +265,7 @@
    1.71          }
    1.72          return checkArray(jsArray);
    1.73      }
    1.74 -    
    1.75 +
    1.76      @Override
    1.77      public Object toJavaScript(Object toReturn) {
    1.78          if (toReturn instanceof Object[]) {
    1.79 @@ -289,7 +274,7 @@
    1.80              return toReturn;
    1.81          }
    1.82      }
    1.83 -    
    1.84 +
    1.85      @Override public void execute(final Runnable r) {
    1.86          if (Platform.isFxApplicationThread()) {
    1.87              Closeable c = Fn.activate(this);
    1.88 @@ -301,7 +286,7 @@
    1.89                  } catch (IOException ex) {
    1.90                      // ignore
    1.91                  }
    1.92 -            }                
    1.93 +            }
    1.94          } else {
    1.95              class Wrap implements Runnable {
    1.96                  @Override
    1.97 @@ -315,7 +300,7 @@
    1.98                          } catch (IOException ex) {
    1.99                              // ignore
   1.100                          }
   1.101 -                    }                
   1.102 +                    }
   1.103                  }
   1.104              }
   1.105              Platform.runLater(new Wrap());
   1.106 @@ -340,7 +325,7 @@
   1.107          public Object invoke(Object thiz, Object... args) throws Exception {
   1.108              return invokeImpl(thiz, true, args);
   1.109          }
   1.110 -        
   1.111 +
   1.112          final Object invokeImpl(Object thiz, boolean arrayChecks, Object... args) throws Exception {
   1.113              try {
   1.114                  if (LOG.isLoggable(Level.FINE)) {
   1.115 @@ -357,7 +342,7 @@
   1.116                              Object[] arr = (Object[]) args[i];
   1.117                              conv = ((AbstractFXPresenter)presenter()).convertArrays(arr);
   1.118                          }
   1.119 -                        if (conv != null && keepAlive != null && 
   1.120 +                        if (conv != null && keepAlive != null &&
   1.121                              !keepAlive[i] && !isJSReady(conv) &&
   1.122                              !conv.getClass().getSimpleName().equals("$JsCallbacks$") // NOI18N
   1.123                          ) {
   1.124 @@ -386,7 +371,7 @@
   1.125              }
   1.126          }
   1.127      }
   1.128 -    
   1.129 +
   1.130      private static boolean isJSReady(Object obj) {
   1.131          if (obj == null) {
   1.132              return true;
   1.133 @@ -405,7 +390,7 @@
   1.134          }
   1.135          return false;
   1.136      }
   1.137 -    
   1.138 +
   1.139      private static final class Weak extends WeakReference<Object> {
   1.140          public Weak(Object referent) {
   1.141              super(referent);
     2.1 --- a/boot-fx/src/main/java/org/netbeans/html/boot/fx/FXBrwsr.java	Sun Jul 19 13:09:06 2015 +0200
     2.2 +++ b/boot-fx/src/main/java/org/netbeans/html/boot/fx/FXBrwsr.java	Sun Jul 19 14:04:03 2015 +0200
     2.3 @@ -48,6 +48,7 @@
     2.4  import java.util.concurrent.Executors;
     2.5  import java.util.logging.Level;
     2.6  import java.util.logging.Logger;
     2.7 +import java.util.prefs.Preferences;
     2.8  import javafx.application.Application;
     2.9  import javafx.application.Platform;
    2.10  import javafx.beans.value.ChangeListener;
    2.11 @@ -71,6 +72,8 @@
    2.12  import javafx.stage.Modality;
    2.13  import javafx.stage.Screen;
    2.14  import javafx.stage.Stage;
    2.15 +import javafx.stage.Window;
    2.16 +import javafx.stage.WindowEvent;
    2.17  import javafx.util.Callback;
    2.18  
    2.19  /** This is an implementation class, to implement browser builder API. Just
    2.20 @@ -85,11 +88,12 @@
    2.21  
    2.22      public static synchronized WebView findWebView(final URL url, final FXPresenter onLoad) {
    2.23          if (INSTANCE == null) {
    2.24 +            final String callee = findCalleeClassName();
    2.25              Executors.newFixedThreadPool(1).submit(new Runnable() {
    2.26                  @Override
    2.27                  public void run() {
    2.28                      try {
    2.29 -                        FXBrwsr.launch(FXBrwsr.class);
    2.30 +                        FXBrwsr.launch(FXBrwsr.class, callee);
    2.31                      } catch (Throwable ex) {
    2.32                          ex.printStackTrace();
    2.33                      } finally {
    2.34 @@ -141,8 +145,8 @@
    2.35      @Override
    2.36      public void start(Stage primaryStage) throws Exception {
    2.37          BorderPane r = new BorderPane();
    2.38 -        Rectangle2D rect = findInitialSize();
    2.39 -        Scene scene = new Scene(r, rect.getWidth(), rect.getHeight());
    2.40 +        Object[] arr = findInitialSize(this.getParameters().getRaw().get(0));
    2.41 +        Scene scene = new Scene(r, (Double)arr[2], (Double)arr[3]);
    2.42          primaryStage.setScene(scene);
    2.43          this.root = r;
    2.44          this.stage = primaryStage;
    2.45 @@ -150,19 +154,64 @@
    2.46              INSTANCE = this;
    2.47              FXBrwsr.class.notifyAll();
    2.48          }
    2.49 -        primaryStage.setX(rect.getMinX());
    2.50 -        primaryStage.setY(rect.getMinY());
    2.51 +        primaryStage.setX((Double)arr[0]);
    2.52 +        primaryStage.setY((Double)arr[1]);
    2.53 +        if (arr[4] != null) {
    2.54 +            scene.getWindow().setOnCloseRequest((EventHandler<WindowEvent>) arr[4]);
    2.55 +        }
    2.56          primaryStage.show();
    2.57      }
    2.58  
    2.59 -    static Rectangle2D findInitialSize() {
    2.60 +    static String findCalleeClassName() {
    2.61 +        StackTraceElement[] frames = new Exception().getStackTrace();
    2.62 +        for (StackTraceElement e : frames) {
    2.63 +            String cn = e.getClassName();
    2.64 +            if (cn.startsWith("org.netbeans.html.")) { // NOI18N
    2.65 +                continue;
    2.66 +            }
    2.67 +            if (cn.startsWith("net.java.html.")) { // NOI18N
    2.68 +                continue;
    2.69 +            }
    2.70 +            if (cn.startsWith("java.")) { // NOI18N
    2.71 +                continue;
    2.72 +            }
    2.73 +            if (cn.startsWith("javafx.")) { // NOI18N
    2.74 +                continue;
    2.75 +            }
    2.76 +            if (cn.startsWith("com.sun.")) { // NOI18N
    2.77 +                continue;
    2.78 +            }
    2.79 +            return cn;
    2.80 +        }
    2.81 +        return "org.netbeans.html"; // NOI18N
    2.82 +    }
    2.83 +
    2.84 +    private static Object[] findInitialSize(String callee) {
    2.85 +        final Preferences prefs = Preferences.userRoot().node(callee.replace('.', '/'));
    2.86          Rectangle2D screen = Screen.getPrimary().getBounds();
    2.87 -        return new Rectangle2D(
    2.88 -            screen.getWidth() * 0.05,
    2.89 -            screen.getHeight() * 0.05,
    2.90 -            screen.getWidth() * 0.9,
    2.91 -            screen.getHeight() * 0.9
    2.92 -        );
    2.93 +        double x = prefs.getDouble("x", screen.getWidth() * 0.05); // NOI18N
    2.94 +        double y = prefs.getDouble("y", screen.getHeight() * 0.05); // NOI18N
    2.95 +        double width = prefs.getDouble("width", screen.getWidth() * 0.9); // NOI18N
    2.96 +        double height = prefs.getDouble("height", screen.getHeight() * 0.9); // NOI18N
    2.97 +
    2.98 +        Object[] arr = {
    2.99 +            x, y, width, height, null
   2.100 +        };
   2.101 +
   2.102 +        if (!callee.equals("org.netbeans.html")) { // NOI18N
   2.103 +            arr[4] = new EventHandler<WindowEvent>() {
   2.104 +                @Override
   2.105 +                public void handle(WindowEvent event) {
   2.106 +                    Window window = (Window) event.getSource();
   2.107 +                    prefs.putDouble("x", window.getX()); // NOI18N
   2.108 +                    prefs.putDouble("y", window.getY()); // NOI18N
   2.109 +                    prefs.putDouble("width", window.getWidth()); // NOI18N
   2.110 +                    prefs.putDouble("height", window.getHeight()); // NOI18N
   2.111 +                }
   2.112 +            };
   2.113 +        }
   2.114 +
   2.115 +        return arr;
   2.116      }
   2.117  
   2.118      private WebView newView(final URL url, final FXPresenter onLoad) {
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/boot-fx/src/test/java/org/netbeans/html/boot/fx/FXBrwsrTest.java	Sun Jul 19 14:04:03 2015 +0200
     3.3 @@ -0,0 +1,84 @@
     3.4 +/**
     3.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3.6 + *
     3.7 + * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
     3.8 + *
     3.9 + * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
    3.10 + * Other names may be trademarks of their respective owners.
    3.11 + *
    3.12 + * The contents of this file are subject to the terms of either the GNU
    3.13 + * General Public License Version 2 only ("GPL") or the Common
    3.14 + * Development and Distribution License("CDDL") (collectively, the
    3.15 + * "License"). You may not use this file except in compliance with the
    3.16 + * License. You can obtain a copy of the License at
    3.17 + * http://www.netbeans.org/cddl-gplv2.html
    3.18 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    3.19 + * specific language governing permissions and limitations under the
    3.20 + * License.  When distributing the software, include this License Header
    3.21 + * Notice in each file and include the License file at
    3.22 + * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
    3.23 + * particular file as subject to the "Classpath" exception as provided
    3.24 + * by Oracle in the GPL Version 2 section of the License file that
    3.25 + * accompanied this code. If applicable, add the following below the
    3.26 + * License Header, with the fields enclosed by brackets [] replaced by
    3.27 + * your own identifying information:
    3.28 + * "Portions Copyrighted [year] [name of copyright owner]"
    3.29 + *
    3.30 + * Contributor(s):
    3.31 + *
    3.32 + * The Original Software is NetBeans. The Initial Developer of the Original
    3.33 + * Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved.
    3.34 + *
    3.35 + * If you wish your version of this file to be governed by only the CDDL
    3.36 + * or only the GPL Version 2, indicate your decision by adding
    3.37 + * "[Contributor] elects to include this software in this distribution
    3.38 + * under the [CDDL or GPL Version 2] license." If you do not indicate a
    3.39 + * single choice of license, a recipient has the option to distribute
    3.40 + * your version of this file under either the CDDL, the GPL Version 2 or
    3.41 + * to extend the choice of license to its licensees as provided above.
    3.42 + * However, if you add GPL Version 2 code and therefore, elected the GPL
    3.43 + * Version 2 license, then the option applies only if the new code is
    3.44 + * made subject to such option by the copyright holder.
    3.45 + */
    3.46 +package org.netbeans.html.boot.fx;
    3.47 +
    3.48 +import org.sample.app.pkg.SampleApp;
    3.49 +import static org.testng.Assert.assertEquals;
    3.50 +import org.testng.annotations.Test;
    3.51 +
    3.52 +public class FXBrwsrTest {
    3.53 +
    3.54 +    public FXBrwsrTest() {
    3.55 +    }
    3.56 +
    3.57 +    @Test
    3.58 +    public void testFindCalleeClassName() throws InterruptedException {
    3.59 +        String callee = invokeMain();
    3.60 +        assertEquals(callee, SampleApp.class.getName(), "Callee is found correctly");
    3.61 +    }
    3.62 +
    3.63 +    synchronized static String invokeMain() throws InterruptedException {
    3.64 +        new Thread("starting main") {
    3.65 +            @Override
    3.66 +            public void run() {
    3.67 +                SampleApp.main();
    3.68 +            }
    3.69 +        }.start();
    3.70 +        for (;;) {
    3.71 +            String callee = System.getProperty("callee");
    3.72 +            if (callee != null) {
    3.73 +                return callee;
    3.74 +            }
    3.75 +            FXBrwsrTest.class.wait();
    3.76 +        }
    3.77 +    }
    3.78 +
    3.79 +
    3.80 +    public static void computeCalleeClassName() {
    3.81 +        String name = FXBrwsr.findCalleeClassName();
    3.82 +        System.setProperty("callee", name);
    3.83 +        synchronized (FXBrwsrTest.class) {
    3.84 +            FXBrwsrTest.class.notifyAll();
    3.85 +        }
    3.86 +    }
    3.87 +}
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/boot-fx/src/test/java/org/sample/app/pkg/SampleApp.java	Sun Jul 19 14:04:03 2015 +0200
     4.3 @@ -0,0 +1,62 @@
     4.4 +/**
     4.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     4.6 + *
     4.7 + * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
     4.8 + *
     4.9 + * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
    4.10 + * Other names may be trademarks of their respective owners.
    4.11 + *
    4.12 + * The contents of this file are subject to the terms of either the GNU
    4.13 + * General Public License Version 2 only ("GPL") or the Common
    4.14 + * Development and Distribution License("CDDL") (collectively, the
    4.15 + * "License"). You may not use this file except in compliance with the
    4.16 + * License. You can obtain a copy of the License at
    4.17 + * http://www.netbeans.org/cddl-gplv2.html
    4.18 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    4.19 + * specific language governing permissions and limitations under the
    4.20 + * License.  When distributing the software, include this License Header
    4.21 + * Notice in each file and include the License file at
    4.22 + * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
    4.23 + * particular file as subject to the "Classpath" exception as provided
    4.24 + * by Oracle in the GPL Version 2 section of the License file that
    4.25 + * accompanied this code. If applicable, add the following below the
    4.26 + * License Header, with the fields enclosed by brackets [] replaced by
    4.27 + * your own identifying information:
    4.28 + * "Portions Copyrighted [year] [name of copyright owner]"
    4.29 + *
    4.30 + * Contributor(s):
    4.31 + *
    4.32 + * The Original Software is NetBeans. The Initial Developer of the Original
    4.33 + * Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved.
    4.34 + *
    4.35 + * If you wish your version of this file to be governed by only the CDDL
    4.36 + * or only the GPL Version 2, indicate your decision by adding
    4.37 + * "[Contributor] elects to include this software in this distribution
    4.38 + * under the [CDDL or GPL Version 2] license." If you do not indicate a
    4.39 + * single choice of license, a recipient has the option to distribute
    4.40 + * your version of this file under either the CDDL, the GPL Version 2 or
    4.41 + * to extend the choice of license to its licensees as provided above.
    4.42 + * However, if you add GPL Version 2 code and therefore, elected the GPL
    4.43 + * Version 2 license, then the option applies only if the new code is
    4.44 + * made subject to such option by the copyright holder.
    4.45 + */
    4.46 +package org.sample.app.pkg;
    4.47 +
    4.48 +import net.java.html.boot.BrowserBuilder;
    4.49 +import org.netbeans.html.boot.fx.FXBrwsrTest;
    4.50 +
    4.51 +public class SampleApp implements Runnable {
    4.52 +
    4.53 +    public static void main() {
    4.54 +        BrowserBuilder.newBrowser()
    4.55 +            .loadPage("org/netbeans/html/boot/fx/empty.html") // NOI18N
    4.56 +            .loadFinished(new SampleApp())
    4.57 +            .showAndWait();
    4.58 +    }
    4.59 +
    4.60 +    @Override
    4.61 +    public void run() {
    4.62 +        FXBrwsrTest.computeCalleeClassName();
    4.63 +    }
    4.64 +
    4.65 +}