ko-fx/src/main/java/org/netbeans/html/kofx/FXContext.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 16 Dec 2013 16:59:43 +0100
branchnetbeans
changeset 362 92fb71afdc0e
parent 358 ko-fx/src/main/java/org/apidesign/html/kofx/FXContext.java@80702021b851
child 365 5c93ad8c7a15
permissions -rw-r--r--
Moving implementation classes into org.netbeans.html namespace
     1 /**
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved.
     5  *
     6  * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
     7  * Other names may be trademarks of their respective owners.
     8  *
     9  * The contents of this file are subject to the terms of either the GNU
    10  * General Public License Version 2 only ("GPL") or the Common
    11  * Development and Distribution License("CDDL") (collectively, the
    12  * "License"). You may not use this file except in compliance with the
    13  * License. You can obtain a copy of the License at
    14  * http://www.netbeans.org/cddl-gplv2.html
    15  * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    16  * specific language governing permissions and limitations under the
    17  * License.  When distributing the software, include this License Header
    18  * Notice in each file and include the License file at
    19  * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
    20  * particular file as subject to the "Classpath" exception as provided
    21  * by Oracle in the GPL Version 2 section of the License file that
    22  * accompanied this code. If applicable, add the following below the
    23  * License Header, with the fields enclosed by brackets [] replaced by
    24  * your own identifying information:
    25  * "Portions Copyrighted [year] [name of copyright owner]"
    26  *
    27  * Contributor(s):
    28  *
    29  * The Original Software is NetBeans. The Initial Developer of the Original
    30  * Software is Oracle. Portions Copyright 2013-2013 Oracle. All Rights Reserved.
    31  *
    32  * If you wish your version of this file to be governed by only the CDDL
    33  * or only the GPL Version 2, indicate your decision by adding
    34  * "[Contributor] elects to include this software in this distribution
    35  * under the [CDDL or GPL Version 2] license." If you do not indicate a
    36  * single choice of license, a recipient has the option to distribute
    37  * your version of this file under either the CDDL, the GPL Version 2 or
    38  * to extend the choice of license to its licensees as provided above.
    39  * However, if you add GPL Version 2 code and therefore, elected the GPL
    40  * Version 2 license, then the option applies only if the new code is
    41  * made subject to such option by the copyright holder.
    42  */
    43 package org.netbeans.html.kofx;
    44 
    45 import java.io.Closeable;
    46 import java.io.IOException;
    47 import java.io.InputStream;
    48 import java.util.ServiceLoader;
    49 import java.util.logging.Logger;
    50 import javafx.application.Platform;
    51 import net.java.html.js.JavaScriptBody;
    52 import netscape.javascript.JSObject;
    53 import org.apidesign.html.boot.spi.Fn;
    54 import org.apidesign.html.context.spi.Contexts;
    55 import org.apidesign.html.json.spi.FunctionBinding;
    56 import org.apidesign.html.json.spi.JSONCall;
    57 import org.apidesign.html.json.spi.PropertyBinding;
    58 import org.apidesign.html.json.spi.Technology;
    59 import org.apidesign.html.json.spi.Transfer;
    60 import org.apidesign.html.json.spi.WSTransfer;
    61 import org.openide.util.lookup.ServiceProvider;
    62 
    63 /** This is an implementation package - just
    64  * include its JAR on classpath and use official {@link Context} API
    65  * to access the functionality.
    66  * <p>
    67  * Registers {@link ContextProvider}, so {@link ServiceLoader} can find it.
    68  *
    69  * @author Jaroslav Tulach <jtulach@netbeans.org>
    70  */
    71 public final class FXContext
    72 implements Technology.BatchInit<JSObject>, Transfer, WSTransfer<LoadWS> {
    73     static final Logger LOG = Logger.getLogger(FXContext.class.getName());
    74     private static Boolean javaScriptEnabled;
    75     private final Fn.Presenter browserContext;
    76 
    77     public FXContext(Fn.Presenter browserContext) {
    78         this.browserContext = browserContext;
    79     }
    80     
    81     @JavaScriptBody(args = {}, body = "return true;")
    82     private static boolean isJavaScriptEnabledJs() {
    83         return false;
    84     }
    85     
    86     static boolean isJavaScriptEnabled() {
    87         if (javaScriptEnabled != null) {
    88             return javaScriptEnabled;
    89         }
    90         return javaScriptEnabled = isJavaScriptEnabledJs();
    91     }
    92 
    93     final boolean areWebSocketsSupported() {
    94         return LoadWS.isSupported();
    95     }
    96 
    97 
    98     @Override
    99     public JSObject wrapModel(Object model, PropertyBinding[] propArr, FunctionBinding[] funcArr) {
   100         String[] propNames = new String[propArr.length];
   101         boolean[] propReadOnly = new boolean[propArr.length];
   102         Object[] propValues = new Object[propArr.length];
   103         for (int i = 0; i < propNames.length; i++) {
   104             propNames[i] = propArr[i].getPropertyName();
   105             propReadOnly[i] = propArr[i].isReadOnly();
   106             propValues[i] = propArr[i].getValue();
   107         }
   108         String[] funcNames = new String[funcArr.length];
   109         for (int i = 0; i < funcNames.length; i++) {
   110             funcNames[i] = funcArr[i].getFunctionName();
   111         }
   112         
   113         return Knockout.wrapModel(model, 
   114             propNames, propReadOnly, Knockout.toArray(propValues), propArr, 
   115             funcNames, funcArr
   116         );
   117     }
   118     
   119     @Override
   120     public JSObject wrapModel(Object model) {
   121         throw new UnsupportedOperationException();
   122     }
   123 
   124     @Override
   125     public void bind(PropertyBinding b, Object model, JSObject data) {
   126         throw new UnsupportedOperationException();
   127     }
   128 
   129     @Override
   130     public void valueHasMutated(JSObject data, String propertyName) {
   131         Knockout.valueHasMutated(data, propertyName);
   132     }
   133 
   134     @Override
   135     public void expose(FunctionBinding fb, Object model, JSObject d) {
   136         throw new UnsupportedOperationException();
   137     }
   138 
   139     @Override
   140     public void applyBindings(JSObject data) {
   141         Knockout.applyBindings(data);
   142     }
   143 
   144     @Override
   145     public Object wrapArray(Object[] arr) {
   146         return Knockout.toArray(arr);
   147     }
   148 
   149     @Override
   150     public void extract(Object obj, String[] props, Object[] values) {
   151         LoadJSON.extractJSON(obj, props, values);
   152     }
   153 
   154     @Override
   155     public void loadJSON(final JSONCall call) {
   156         LoadJSON.loadJSON(call);
   157     }
   158 
   159     @Override
   160     public <M> M toModel(Class<M> modelClass, Object data) {
   161         if (data instanceof JSObject) {
   162             data = ((JSObject)data).getMember("ko-fx.model"); // NOI18N
   163         }
   164         return modelClass.cast(data);
   165     }
   166 
   167     @Override
   168     public Object toJSON(InputStream is) throws IOException {
   169         return LoadJSON.parse(is);
   170     }
   171 
   172     @Override
   173     public void runSafe(final Runnable r) {
   174         class Wrap implements Runnable {
   175             @Override public void run() {
   176                 try (Closeable c = Fn.activate(browserContext)) {
   177                     r.run();
   178                 } catch (IOException ex) {
   179                     // cannot be thrown
   180                 }
   181             }
   182         }
   183         Wrap w = new Wrap();
   184         
   185         if (Platform.isFxApplicationThread()) {
   186             w.run();
   187         } else {
   188             Platform.runLater(w);
   189         }
   190     }
   191 
   192     @Override
   193     public LoadWS open(String url, JSONCall onReply) {
   194         return new LoadWS(onReply, url);
   195     }
   196 
   197     @Override
   198     public void send(LoadWS socket, JSONCall data) {
   199         socket.send(data);
   200     }
   201 
   202     @Override
   203     public void close(LoadWS socket) {
   204         socket.close();
   205     }
   206     
   207     @ServiceProvider(service = Contexts.Provider.class)
   208     public static final class Prvdr implements Contexts.Provider {
   209         @Override
   210         public void fillContext(Contexts.Builder context, Class<?> requestor) {
   211             if (isJavaScriptEnabled()) {
   212                 FXContext c = new FXContext(Fn.activePresenter());
   213                 
   214                 context.register(Technology.class, c, 100);
   215                 context.register(Transfer.class, c, 100);
   216                 if (c.areWebSocketsSupported()) {
   217                     context.register(WSTransfer.class, c, 100);
   218                 }
   219             }
   220         }
   221     }
   222 }