1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/ko-bck2brwsr/src/main/java/org/apidesign/html/ko2brwsr/BrwsrCntxt.java Mon Apr 29 12:50:19 2013 +0200
1.3 @@ -0,0 +1,105 @@
1.4 +/**
1.5 + * HTML via Java(tm) Language Bindings
1.6 + * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
1.7 + *
1.8 + * This program is free software: you can redistribute it and/or modify
1.9 + * it under the terms of the GNU General Public License as published by
1.10 + * the Free Software Foundation, version 2 of the License.
1.11 + *
1.12 + * This program is distributed in the hope that it will be useful,
1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.15 + * GNU General Public License for more details. apidesign.org
1.16 + * designates this particular file as subject to the
1.17 + * "Classpath" exception as provided by apidesign.org
1.18 + * in the License file that accompanied this code.
1.19 + *
1.20 + * You should have received a copy of the GNU General Public License
1.21 + * along with this program. Look for COPYING file in the top folder.
1.22 + * If not, see http://wiki.apidesign.org/wiki/GPLwithClassPathException
1.23 + */
1.24 +package org.apidesign.html.ko2brwsr;
1.25 +
1.26 +import net.java.html.json.Context;
1.27 +import org.apidesign.html.json.spi.ContextBuilder;
1.28 +import org.apidesign.html.json.spi.FunctionBinding;
1.29 +import org.apidesign.html.json.spi.JSONCall;
1.30 +import org.apidesign.html.json.spi.PropertyBinding;
1.31 +import org.apidesign.html.json.spi.Technology;
1.32 +import org.apidesign.html.json.spi.Transfer;
1.33 +
1.34 +/**
1.35 + *
1.36 + * @author Jaroslav Tulach <jtulach@netbeans.org>
1.37 + */
1.38 +public final class BrwsrCntxt implements Technology<Object>, Transfer {
1.39 + private BrwsrCntxt() {}
1.40 +
1.41 + public static final Context DEFAULT;
1.42 + static {
1.43 + BrwsrCntxt c = new BrwsrCntxt();
1.44 + DEFAULT = ContextBuilder.create().withTechnology(c).withTransfer(c).build();
1.45 + }
1.46 +
1.47 + @Override
1.48 + public void extract(Object obj, String[] props, Object[] values) {
1.49 + ConvertTypes.extractJSON(obj, props, values);
1.50 + }
1.51 +
1.52 + @Override
1.53 + public void loadJSON(final JSONCall call) {
1.54 + class R implements Runnable {
1.55 + Object[] arr = { null };
1.56 + @Override
1.57 + public void run() {
1.58 + call.notifySuccess(arr[0]);
1.59 + }
1.60 + }
1.61 + R r = new R();
1.62 + if (call.isJSONP()) {
1.63 + String me = ConvertTypes.createJSONP(r.arr, r);
1.64 + ConvertTypes.loadJSON(call.composeURL(me), r.arr, r, me);
1.65 + } else {
1.66 + ConvertTypes.loadJSON(call.composeURL(null), r.arr, r, null);
1.67 + }
1.68 + }
1.69 +
1.70 + @Override
1.71 + public Object wrapModel(Object model) {
1.72 + return model;
1.73 + }
1.74 +
1.75 + @Override
1.76 + public void bind(PropertyBinding b, Object model, Object data) {
1.77 + Knockout.bind(data, b, b.getPropertyName(),
1.78 + "getValue__Ljava_lang_Object_2",
1.79 + b.isReadOnly() ? null : "setValue__VLjava_lang_Object_2",
1.80 + false, false
1.81 + );
1.82 + }
1.83 +
1.84 + @Override
1.85 + public void valueHasMutated(Object data, String propertyName) {
1.86 + Knockout.valueHasMutated(data, propertyName);
1.87 + }
1.88 +
1.89 + @Override
1.90 + public void expose(FunctionBinding fb, Object model, Object d) {
1.91 + Knockout.expose(d, fb, fb.getFunctionName(), "call__VLjava_lang_Object_2Ljava_lang_Object_2");
1.92 + }
1.93 +
1.94 + @Override
1.95 + public void applyBindings(Object data) {
1.96 + Knockout.applyBindings(data);
1.97 + }
1.98 +
1.99 + @Override
1.100 + public Object wrapArray(Object[] arr) {
1.101 + return arr;
1.102 + }
1.103 +
1.104 + @Override
1.105 + public <M> M toModel(Class<M> modelClass, Object data) {
1.106 + return modelClass.cast(data);
1.107 + }
1.108 +}