ko/fx/src/main/java/org/apidesign/bck2brwsr/kofx/FXContext.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 26 Jun 2013 19:57:38 +0200
branchclassloader
changeset 1230 466c30fd9cb0
permissions -rw-r--r--
Adding in launcher based support for knockout
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, version 2 of the License.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program. Look for COPYING file in the top folder.
    16  * If not, see http://opensource.org/licenses/GPL-2.0.
    17  */
    18 package org.apidesign.bck2brwsr.kofx;
    19 
    20 import java.io.IOException;
    21 import java.io.InputStream;
    22 import java.util.ServiceLoader;
    23 import java.util.logging.Logger;
    24 import netscape.javascript.JSObject;
    25 import org.apidesign.html.context.spi.Contexts;
    26 import org.apidesign.html.json.spi.FunctionBinding;
    27 import org.apidesign.html.json.spi.JSONCall;
    28 import org.apidesign.html.json.spi.PropertyBinding;
    29 import org.apidesign.html.json.spi.Technology;
    30 import org.apidesign.html.json.spi.Transfer;
    31 import org.openide.util.lookup.ServiceProvider;
    32 
    33 /** This is an implementation package - just
    34  * include its JAR on classpath and use official {@link Context} API
    35  * to access the functionality.
    36  * <p>
    37  * Registers {@link ContextProvider}, so {@link ServiceLoader} can find it.
    38  *
    39  * @author Jaroslav Tulach <jtulach@netbeans.org>
    40  */
    41 @ServiceProvider(service = Contexts.Provider.class)
    42 public final class FXContext
    43 implements Technology<JSObject>, Transfer, Contexts.Provider {
    44     static final Logger LOG = Logger.getLogger(FXContext.class.getName());
    45 
    46     @Override
    47     public void fillContext(Contexts.Builder context, Class<?> requestor) {
    48         context.register(Technology.class, this, 100);
    49         context.register(Transfer.class, this, 100);
    50     }
    51 
    52     @Override
    53     public JSObject wrapModel(Object model) {
    54         return (JSObject) Knockout.createBinding(model).koData();
    55     }
    56 
    57     @Override
    58     public void bind(PropertyBinding b, Object model, JSObject data) {
    59         final boolean isList = false;
    60         final boolean isPrimitive = false;
    61         Knockout.bind(data, model, b, isPrimitive, isList);
    62     }
    63 
    64     @Override
    65     public void valueHasMutated(JSObject data, String propertyName) {
    66         Knockout.valueHasMutated(data, propertyName);
    67     }
    68 
    69     @Override
    70     public void expose(FunctionBinding fb, Object model, JSObject d) {
    71         Knockout.expose(d, fb);
    72     }
    73 
    74     @Override
    75     public void applyBindings(JSObject data) {
    76         Knockout.applyBindings(data);
    77     }
    78 
    79     @Override
    80     public Object wrapArray(Object[] arr) {
    81         return Knockout.toArray(arr);
    82     }
    83 
    84     @Override
    85     public void extract(Object obj, String[] props, Object[] values) {
    86         LoadJSON.extractJSON(obj, props, values);
    87     }
    88 
    89     @Override
    90     public void loadJSON(final JSONCall call) {
    91         LoadJSON.loadJSON(call);
    92     }
    93 
    94     @Override
    95     public <M> M toModel(Class<M> modelClass, Object data) {
    96         if (data instanceof JSObject) {
    97             data = ((JSObject)data).getMember("ko-fx.model"); // NOI18N
    98         }
    99         return modelClass.cast(data);
   100     }
   101 
   102     @Override
   103     public Object toJSON(InputStream is) throws IOException {
   104         return LoadJSON.parse(is);
   105     }
   106 }