console logging is a feature of FX presenter, not KO binding UniversalKO
authorJaroslav Tulach <jaroslav.tulach@netbeans.org>
Wed, 08 Jan 2014 15:50:50 +0100
branchUniversalKO
changeset 435ff846f5f2f66
parent 434 e1fe37b03c3f
child 436 e48fbed169c1
console logging is a feature of FX presenter, not KO binding
boot-fx/src/main/java/org/netbeans/html/boot/fx/FXBrwsr.java
boot-fx/src/main/java/org/netbeans/html/boot/fx/FXConsole.java
boot-fx/src/test/java/org/netbeans/html/boot/fx/FXPresenterTst.java
ko-fx/src/main/java/org/netbeans/html/kofx/Console.java
ko-fx/src/main/java/org/netbeans/html/kofx/Knockout.java
     1.1 --- a/boot-fx/src/main/java/org/netbeans/html/boot/fx/FXBrwsr.java	Wed Jan 08 14:02:27 2014 +0100
     1.2 +++ b/boot-fx/src/main/java/org/netbeans/html/boot/fx/FXBrwsr.java	Wed Jan 08 15:50:50 2014 +0100
     1.3 @@ -169,6 +169,7 @@
     1.4              @Override
     1.5              public void changed(ObservableValue<? extends Worker.State> ov, Worker.State t, Worker.State newState) {
     1.6                  if (newState.equals(Worker.State.SUCCEEDED)) {
     1.7 +                    FXConsole.register(view.getEngine());
     1.8                      onLoad.onPageLoad();
     1.9                  }
    1.10                  if (newState.equals(Worker.State.FAILED)) {
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/boot-fx/src/main/java/org/netbeans/html/boot/fx/FXConsole.java	Wed Jan 08 15:50:50 2014 +0100
     2.3 @@ -0,0 +1,84 @@
     2.4 +/**
     2.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     2.6 + *
     2.7 + * Copyright 2013-2013 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-2013 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.logging.Level;
    2.49 +import java.util.logging.Logger;
    2.50 +import javafx.scene.web.WebEngine;
    2.51 +import netscape.javascript.JSObject;
    2.52 +
    2.53 +/** This is an implementation package - just
    2.54 + * include its JAR on classpath and use official {@link Context} API
    2.55 + * to access the functionality.
    2.56 + * <p>
    2.57 + * Redirects JavaScript's messages to Java's {@link Logger}.
    2.58 + *
    2.59 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    2.60 + */
    2.61 +public final class FXConsole {
    2.62 +    static final Logger LOG = Logger.getLogger(FXConsole.class.getName());
    2.63 +    
    2.64 +    private FXConsole() {
    2.65 +    }
    2.66 +
    2.67 +    static void register(WebEngine eng) {
    2.68 +        JSObject fn = (JSObject) eng.executeScript(""
    2.69 +            + "(function(attr, l, c) {"
    2.70 +            + "  window.console[attr] = function(msg) { c.log(l, msg); };"
    2.71 +            + "})"
    2.72 +        );
    2.73 +        FXConsole c = new FXConsole();
    2.74 +        c.registerImpl(fn, "log", Level.INFO);
    2.75 +        c.registerImpl(fn, "info", Level.INFO);
    2.76 +        c.registerImpl(fn, "warn", Level.WARNING);
    2.77 +        c.registerImpl(fn, "error", Level.SEVERE);
    2.78 +    }
    2.79 +    
    2.80 +    private void registerImpl(JSObject eng, String attr, Level l) {
    2.81 +        eng.call("call", null, attr, l, this);
    2.82 +    }
    2.83 +    
    2.84 +    public void log(Level l, String msg) {
    2.85 +        LOG.log(l, msg);
    2.86 +    }
    2.87 +}
     3.1 --- a/boot-fx/src/test/java/org/netbeans/html/boot/fx/FXPresenterTst.java	Wed Jan 08 14:02:27 2014 +0100
     3.2 +++ b/boot-fx/src/test/java/org/netbeans/html/boot/fx/FXPresenterTst.java	Wed Jan 08 15:50:50 2014 +0100
     3.3 @@ -42,6 +42,8 @@
     3.4   */
     3.5  package org.netbeans.html.boot.fx;
     3.6  
     3.7 +import java.util.logging.Handler;
     3.8 +import java.util.logging.LogRecord;
     3.9  import net.java.html.js.JavaScriptBody;
    3.10  import static org.testng.Assert.*;
    3.11  import org.testng.annotations.Test;
    3.12 @@ -57,9 +59,39 @@
    3.13          assertEquals(run.cnt, 1, "Can call even private implementation classes");
    3.14      }
    3.15      
    3.16 +    @Test public void checkConsoleLogging() {
    3.17 +        class H extends Handler {
    3.18 +            LogRecord record;
    3.19 +            
    3.20 +            @Override
    3.21 +            public void publish(LogRecord record) {
    3.22 +                assert this.record == null;
    3.23 +                this.record = record;
    3.24 +            }
    3.25 +
    3.26 +            @Override
    3.27 +            public void flush() {
    3.28 +            }
    3.29 +
    3.30 +            @Override
    3.31 +            public void close() throws SecurityException {
    3.32 +            }
    3.33 +        }
    3.34 +        H h = new H();
    3.35 +        FXConsole.LOG.addHandler(h);
    3.36 +
    3.37 +        log("Ahoj");
    3.38 +        
    3.39 +        assert h.record != null : "Some log record obtained";
    3.40 +        assert "Ahoj".equals(h.record.getMessage()) : "It is our Ahoj: " + h.record.getMessage();
    3.41 +    }
    3.42 +    
    3.43      @JavaScriptBody(args = { "r" }, javacall = true, body = "r.@java.lang.Runnable::run()();")
    3.44      private static native void callback(Runnable r);
    3.45  
    3.46 +    @JavaScriptBody(args = { "msg" }, body = "console.log(msg);")
    3.47 +    private static native void log(String msg);
    3.48 +
    3.49      private static class R implements Runnable {
    3.50          int cnt;
    3.51  
     4.1 --- a/ko-fx/src/main/java/org/netbeans/html/kofx/Console.java	Wed Jan 08 14:02:27 2014 +0100
     4.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.3 @@ -1,81 +0,0 @@
     4.4 -/**
     4.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     4.6 - *
     4.7 - * Copyright 2013-2013 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-2013 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.netbeans.html.kofx;
    4.47 -
    4.48 -import java.util.logging.Level;
    4.49 -import java.util.logging.Logger;
    4.50 -import net.java.html.js.JavaScriptBody;
    4.51 -
    4.52 -/** This is an implementation package - just
    4.53 - * include its JAR on classpath and use official {@link Context} API
    4.54 - * to access the functionality.
    4.55 - * <p>
    4.56 - * Redirects JavaScript's messages to Java's {@link Logger}.
    4.57 - *
    4.58 - * @author Jaroslav Tulach <jtulach@netbeans.org>
    4.59 - */
    4.60 -final class Console {
    4.61 -    private static final Logger LOG = Logger.getLogger(Console.class.getName());
    4.62 -    
    4.63 -    private Console() {
    4.64 -    }
    4.65 -
    4.66 -    static void register() {
    4.67 -        registerImpl("log", Level.INFO);
    4.68 -        registerImpl("info", Level.INFO);
    4.69 -        registerImpl("warn", Level.WARNING);
    4.70 -        registerImpl("error", Level.SEVERE);
    4.71 -    }
    4.72 -    
    4.73 -    @JavaScriptBody(args = { "attr", "l" }, 
    4.74 -        javacall = true, body = 
    4.75 -        "  window.console[attr] = function(m) {\n"
    4.76 -      + "    @org.netbeans.html.kofx.Console::log(Ljava/util/logging/Level;Ljava/lang/String;)(l, m);\n"
    4.77 -      + "  };\n"
    4.78 -    )
    4.79 -    private static native void registerImpl(String attr, Level l);
    4.80 -    
    4.81 -    static void log(Level l, String msg) {
    4.82 -        LOG.log(l, msg);
    4.83 -    }
    4.84 -}
     5.1 --- a/ko-fx/src/main/java/org/netbeans/html/kofx/Knockout.java	Wed Jan 08 14:02:27 2014 +0100
     5.2 +++ b/ko-fx/src/main/java/org/netbeans/html/kofx/Knockout.java	Wed Jan 08 15:50:50 2014 +0100
     5.3 @@ -60,7 +60,6 @@
     5.4  @JavaScriptResource("knockout-2.2.1.js")
     5.5  final class Knockout {
     5.6      static {
     5.7 -        Console.register();
     5.8          loadKnockout();
     5.9      }
    5.10