#247466: Title of the main window showing JavaFX WebView is updated according to title of the HTML page in it
authorJaroslav Tulach <jtulach@netbeans.org>
Thu, 16 Oct 2014 11:52:26 +0200
changeset 86349eff98a9310
parent 862 7cacce04dfd8
child 865 e91d7eda0b9d
#247466: Title of the main window showing JavaFX WebView is updated according to title of the HTML page in it
boot-fx/src/main/java/org/netbeans/html/boot/fx/FXBrwsr.java
boot-fx/src/test/java/org/netbeans/html/boot/fx/TitleTest.java
     1.1 --- a/boot-fx/src/main/java/org/netbeans/html/boot/fx/FXBrwsr.java	Thu Oct 16 09:12:59 2014 +0200
     1.2 +++ b/boot-fx/src/main/java/org/netbeans/html/boot/fx/FXBrwsr.java	Thu Oct 16 11:52:26 2014 +0200
     1.3 @@ -126,20 +126,28 @@
     1.4              return INSTANCE.newView(url, onLoad);
     1.5          }
     1.6      }
     1.7 +    
     1.8 +    static synchronized Stage findStage() throws InterruptedException {
     1.9 +        while (INSTANCE == null) {
    1.10 +            FXBrwsr.class.wait();
    1.11 +        }
    1.12 +        return INSTANCE.stage;
    1.13 +    }
    1.14 +    
    1.15      private Stage stage;
    1.16  
    1.17      @Override
    1.18      public void start(Stage primaryStage) throws Exception {
    1.19 +        BorderPane r = new BorderPane();
    1.20 +        Scene scene = new Scene(r, 800, 600);
    1.21 +        primaryStage.setScene(scene);
    1.22 +        this.root = r;
    1.23 +        this.stage = primaryStage;
    1.24          synchronized (FXBrwsr.class) {
    1.25              INSTANCE = this;
    1.26              FXBrwsr.class.notifyAll();
    1.27          }
    1.28 -        BorderPane r = new BorderPane();
    1.29 -        Scene scene = new Scene(r, 800, 600);
    1.30 -        primaryStage.setScene(scene);
    1.31          primaryStage.show();
    1.32 -        this.root = r;
    1.33 -        this.stage = primaryStage;
    1.34      }
    1.35  
    1.36      private WebView newView(final URL url, final FXPresenter onLoad) {
    1.37 @@ -265,6 +273,25 @@
    1.38              }
    1.39              
    1.40          });
    1.41 +        class Title implements ChangeListener<String> {
    1.42 +
    1.43 +            private String title;
    1.44 +
    1.45 +            public Title() {
    1.46 +                super();
    1.47 +            }
    1.48 +
    1.49 +            @Override
    1.50 +            public void changed(ObservableValue<? extends String> ov, String t, String t1) {
    1.51 +                title = view.getEngine().getTitle();
    1.52 +                if (title != null) {
    1.53 +                    stage.setTitle(title);
    1.54 +                }
    1.55 +            }
    1.56 +        }
    1.57 +        final Title x = new Title();
    1.58 +        view.getEngine().titleProperty().addListener(x);
    1.59 +        x.changed(null, null, null);
    1.60          return view;
    1.61      }
    1.62  
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/boot-fx/src/test/java/org/netbeans/html/boot/fx/TitleTest.java	Thu Oct 16 11:52:26 2014 +0200
     2.3 @@ -0,0 +1,146 @@
     2.4 +/**
     2.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     2.6 + *
     2.7 + * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
     2.8 + *
     2.9 + * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
    2.10 + * Other names may be trademarks of their respective owners.
    2.11 + *
    2.12 + * The contents of this file are subject to the terms of either the GNU
    2.13 + * General Public License Version 2 only ("GPL") or the Common
    2.14 + * Development and Distribution License("CDDL") (collectively, the
    2.15 + * "License"). You may not use this file except in compliance with the
    2.16 + * License. You can obtain a copy of the License at
    2.17 + * http://www.netbeans.org/cddl-gplv2.html
    2.18 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    2.19 + * specific language governing permissions and limitations under the
    2.20 + * License.  When distributing the software, include this License Header
    2.21 + * Notice in each file and include the License file at
    2.22 + * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
    2.23 + * particular file as subject to the "Classpath" exception as provided
    2.24 + * by Oracle in the GPL Version 2 section of the License file that
    2.25 + * accompanied this code. If applicable, add the following below the
    2.26 + * License Header, with the fields enclosed by brackets [] replaced by
    2.27 + * your own identifying information:
    2.28 + * "Portions Copyrighted [year] [name of copyright owner]"
    2.29 + *
    2.30 + * Contributor(s):
    2.31 + *
    2.32 + * The Original Software is NetBeans. The Initial Developer of the Original
    2.33 + * Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved.
    2.34 + *
    2.35 + * If you wish your version of this file to be governed by only the CDDL
    2.36 + * or only the GPL Version 2, indicate your decision by adding
    2.37 + * "[Contributor] elects to include this software in this distribution
    2.38 + * under the [CDDL or GPL Version 2] license." If you do not indicate a
    2.39 + * single choice of license, a recipient has the option to distribute
    2.40 + * your version of this file under either the CDDL, the GPL Version 2 or
    2.41 + * to extend the choice of license to its licensees as provided above.
    2.42 + * However, if you add GPL Version 2 code and therefore, elected the GPL
    2.43 + * Version 2 license, then the option applies only if the new code is
    2.44 + * made subject to such option by the copyright holder.
    2.45 + */
    2.46 +package org.netbeans.html.boot.fx;
    2.47 +
    2.48 +import java.util.concurrent.CountDownLatch;
    2.49 +import java.util.concurrent.Executors;
    2.50 +import java.util.concurrent.TimeUnit;
    2.51 +import javafx.beans.value.ChangeListener;
    2.52 +import javafx.beans.value.ObservableValue;
    2.53 +import javafx.stage.Stage;
    2.54 +import net.java.html.BrwsrCtx;
    2.55 +import net.java.html.boot.BrowserBuilder;
    2.56 +import net.java.html.js.JavaScriptBody;
    2.57 +import org.netbeans.html.boot.spi.Fn;
    2.58 +import static org.testng.Assert.*;
    2.59 +import org.testng.annotations.Test;
    2.60 +
    2.61 +/**
    2.62 + *
    2.63 + * @author Jaroslav Tulach
    2.64 + */
    2.65 +public class TitleTest {
    2.66 +    private static Runnable whenInitialized;
    2.67 +    
    2.68 +    public TitleTest() {
    2.69 +    }
    2.70 +
    2.71 +    @JavaScriptBody(args = { "a", "b"  }, body = "return a + b;")
    2.72 +    private static native int plus(int a, int b);
    2.73 +    
    2.74 +    @Test public void checkReload() throws Throwable {
    2.75 +        final Throwable[] arr = { null };
    2.76 +        
    2.77 +        final BrowserBuilder bb = BrowserBuilder.newBrowser().loadClass(TitleTest.class).
    2.78 +                loadPage("empty.html").
    2.79 +                invoke("initialized");
    2.80 +        
    2.81 +        class ShowBrowser implements Runnable {
    2.82 +            @Override
    2.83 +            public void run() {
    2.84 +                bb.showAndWait();
    2.85 +            }
    2.86 +        }
    2.87 +        
    2.88 +        class WhenInitialized implements Runnable {
    2.89 +            CountDownLatch cdl = new CountDownLatch(1);
    2.90 +            AbstractFXPresenter p;
    2.91 +            BrwsrCtx ctx;
    2.92 +            
    2.93 +            @Override
    2.94 +            public void run() {
    2.95 +                try {
    2.96 +                    whenInitialized = null;
    2.97 +                    doCheckReload();
    2.98 +                    p = (AbstractFXPresenter) Fn.activePresenter();
    2.99 +                    assertNotNull(p, "Presenter is defined");
   2.100 +                    ctx = BrwsrCtx.findDefault(WhenInitialized.class);
   2.101 +                } catch (Throwable ex) {
   2.102 +                    arr[0] = ex;
   2.103 +                } finally {
   2.104 +                    cdl.countDown();
   2.105 +                }
   2.106 +            }
   2.107 +        }
   2.108 +        WhenInitialized when = new WhenInitialized();
   2.109 +        whenInitialized = when;
   2.110 +        Executors.newSingleThreadExecutor().submit(new ShowBrowser());
   2.111 +        when.cdl.await();
   2.112 +        if (arr[0] != null) throw arr[0];
   2.113 +        
   2.114 +        Stage s = FXBrwsr.findStage();
   2.115 +        assertEquals(s.getTitle(), "FX Presenter Harness");
   2.116 +        
   2.117 +        final CountDownLatch propChange = new CountDownLatch(1);
   2.118 +        s.titleProperty().addListener(new ChangeListener<String>() {
   2.119 +            @Override
   2.120 +            public void changed(ObservableValue<? extends String> ov, String t, String t1) {
   2.121 +                propChange.countDown();
   2.122 +            }
   2.123 +        });
   2.124 +        
   2.125 +        when.ctx.execute(new Runnable() {
   2.126 +            @Override
   2.127 +            public void run() {
   2.128 +                changeTitle("New title");
   2.129 +            }
   2.130 +        });
   2.131 +
   2.132 +        propChange.await(5, TimeUnit.SECONDS);
   2.133 +        assertEquals(s.getTitle(), "New title");
   2.134 +    }
   2.135 +    
   2.136 +    final void doCheckReload() throws Exception {
   2.137 +        int res = plus(30, 12);
   2.138 +        assertEquals(res, 42, "Meaning of world computed");
   2.139 +    }
   2.140 +    
   2.141 +    public static synchronized void initialized() throws Exception {
   2.142 +        whenInitialized.run();
   2.143 +    }
   2.144 +    
   2.145 +    @JavaScriptBody(args = { "s" }, body = 
   2.146 +        "document.getElementsByTagName('title')[0].innerHTML = s;"
   2.147 +    )
   2.148 +    static native void changeTitle(String s);
   2.149 +}