ko-bck2brwsr/src/main/java/org/apidesign/html/ko2brwsr/BrwsrCntxt.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 01 May 2013 07:06:43 +0200
changeset 1194 3213724a4996
parent 1189 9f8b07dcbe79
child 1196 fb83f58ece66
permissions -rw-r--r--
Making more things private, removing KOList and providing some fake Javadoc
     1 /**
     2  * HTML via Java(tm) Language Bindings
     3  * Copyright (C) 2013 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. apidesign.org
    13  * designates this particular file as subject to the
    14  * "Classpath" exception as provided by apidesign.org
    15  * in the License file that accompanied this code.
    16  *
    17  * You should have received a copy of the GNU General Public License
    18  * along with this program. Look for COPYING file in the top folder.
    19  * If not, see http://wiki.apidesign.org/wiki/GPLwithClassPathException
    20  */
    21 package org.apidesign.html.ko2brwsr;
    22 
    23 import net.java.html.json.Context;
    24 import org.apidesign.html.json.spi.ContextBuilder;
    25 import org.apidesign.html.json.spi.FunctionBinding;
    26 import org.apidesign.html.json.spi.JSONCall;
    27 import org.apidesign.html.json.spi.PropertyBinding;
    28 import org.apidesign.html.json.spi.Technology;
    29 import org.apidesign.html.json.spi.Transfer;
    30 
    31 /**
    32  *
    33  * @author Jaroslav Tulach <jtulach@netbeans.org>
    34  */
    35 final class BrwsrCntxt implements Technology<Object>, Transfer {
    36     private BrwsrCntxt() {}
    37     
    38     public static final Context DEFAULT;
    39     static {
    40         BrwsrCntxt c = new BrwsrCntxt();
    41         DEFAULT = ContextBuilder.create().withTechnology(c).withTransfer(c).build();
    42     }
    43     
    44     @Override
    45     public void extract(Object obj, String[] props, Object[] values) {
    46         ConvertTypes.extractJSON(obj, props, values);
    47     }
    48 
    49     @Override
    50     public void loadJSON(final JSONCall call) {
    51         class R implements Runnable {
    52             Object[] arr = { null };
    53             @Override
    54             public void run() {
    55                 call.notifySuccess(arr[0]);
    56             }
    57         }
    58         R r = new R();
    59         if (call.isJSONP()) {
    60             String me = ConvertTypes.createJSONP(r.arr, r);
    61             ConvertTypes.loadJSON(call.composeURL(me), r.arr, r, me);
    62         } else {
    63             ConvertTypes.loadJSON(call.composeURL(null), r.arr, r, null);
    64         }
    65     }
    66 
    67     @Override
    68     public Object wrapModel(Object model) {
    69         return model;
    70     }
    71 
    72     @Override
    73     public void bind(PropertyBinding b, Object model, Object data) {
    74         Knockout.bind(data, b, b.getPropertyName(), 
    75             "getValue__Ljava_lang_Object_2", 
    76             b.isReadOnly() ? null : "setValue__VLjava_lang_Object_2", 
    77             false, false
    78         );
    79     }
    80 
    81     @Override
    82     public void valueHasMutated(Object data, String propertyName) {
    83         Knockout.valueHasMutated(data, propertyName);
    84     }
    85 
    86     @Override
    87     public void expose(FunctionBinding fb, Object model, Object d) {
    88         Knockout.expose(d, fb, fb.getFunctionName(), "call__VLjava_lang_Object_2Ljava_lang_Object_2");
    89     }
    90 
    91     @Override
    92     public void applyBindings(Object data) {
    93         Knockout.applyBindings(data);
    94     }
    95 
    96     @Override
    97     public Object wrapArray(Object[] arr) {
    98         return arr;
    99     }
   100 
   101     @Override
   102     public <M> M toModel(Class<M> modelClass, Object data) {
   103         return modelClass.cast(data);
   104     }
   105 }