1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/ko/fx/src/main/java/org/apidesign/bck2brwsr/kofx/FXContext.java Wed Jun 26 19:57:38 2013 +0200
1.3 @@ -0,0 +1,106 @@
1.4 +/**
1.5 + * Back 2 Browser Bytecode Translator
1.6 + * Copyright (C) 2012 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.
1.16 + *
1.17 + * You should have received a copy of the GNU General Public License
1.18 + * along with this program. Look for COPYING file in the top folder.
1.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
1.20 + */
1.21 +package org.apidesign.bck2brwsr.kofx;
1.22 +
1.23 +import java.io.IOException;
1.24 +import java.io.InputStream;
1.25 +import java.util.ServiceLoader;
1.26 +import java.util.logging.Logger;
1.27 +import netscape.javascript.JSObject;
1.28 +import org.apidesign.html.context.spi.Contexts;
1.29 +import org.apidesign.html.json.spi.FunctionBinding;
1.30 +import org.apidesign.html.json.spi.JSONCall;
1.31 +import org.apidesign.html.json.spi.PropertyBinding;
1.32 +import org.apidesign.html.json.spi.Technology;
1.33 +import org.apidesign.html.json.spi.Transfer;
1.34 +import org.openide.util.lookup.ServiceProvider;
1.35 +
1.36 +/** This is an implementation package - just
1.37 + * include its JAR on classpath and use official {@link Context} API
1.38 + * to access the functionality.
1.39 + * <p>
1.40 + * Registers {@link ContextProvider}, so {@link ServiceLoader} can find it.
1.41 + *
1.42 + * @author Jaroslav Tulach <jtulach@netbeans.org>
1.43 + */
1.44 +@ServiceProvider(service = Contexts.Provider.class)
1.45 +public final class FXContext
1.46 +implements Technology<JSObject>, Transfer, Contexts.Provider {
1.47 + static final Logger LOG = Logger.getLogger(FXContext.class.getName());
1.48 +
1.49 + @Override
1.50 + public void fillContext(Contexts.Builder context, Class<?> requestor) {
1.51 + context.register(Technology.class, this, 100);
1.52 + context.register(Transfer.class, this, 100);
1.53 + }
1.54 +
1.55 + @Override
1.56 + public JSObject wrapModel(Object model) {
1.57 + return (JSObject) Knockout.createBinding(model).koData();
1.58 + }
1.59 +
1.60 + @Override
1.61 + public void bind(PropertyBinding b, Object model, JSObject data) {
1.62 + final boolean isList = false;
1.63 + final boolean isPrimitive = false;
1.64 + Knockout.bind(data, model, b, isPrimitive, isList);
1.65 + }
1.66 +
1.67 + @Override
1.68 + public void valueHasMutated(JSObject data, String propertyName) {
1.69 + Knockout.valueHasMutated(data, propertyName);
1.70 + }
1.71 +
1.72 + @Override
1.73 + public void expose(FunctionBinding fb, Object model, JSObject d) {
1.74 + Knockout.expose(d, fb);
1.75 + }
1.76 +
1.77 + @Override
1.78 + public void applyBindings(JSObject data) {
1.79 + Knockout.applyBindings(data);
1.80 + }
1.81 +
1.82 + @Override
1.83 + public Object wrapArray(Object[] arr) {
1.84 + return Knockout.toArray(arr);
1.85 + }
1.86 +
1.87 + @Override
1.88 + public void extract(Object obj, String[] props, Object[] values) {
1.89 + LoadJSON.extractJSON(obj, props, values);
1.90 + }
1.91 +
1.92 + @Override
1.93 + public void loadJSON(final JSONCall call) {
1.94 + LoadJSON.loadJSON(call);
1.95 + }
1.96 +
1.97 + @Override
1.98 + public <M> M toModel(Class<M> modelClass, Object data) {
1.99 + if (data instanceof JSObject) {
1.100 + data = ((JSObject)data).getMember("ko-fx.model"); // NOI18N
1.101 + }
1.102 + return modelClass.cast(data);
1.103 + }
1.104 +
1.105 + @Override
1.106 + public Object toJSON(InputStream is) throws IOException {
1.107 + return LoadJSON.parse(is);
1.108 + }
1.109 +}