2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
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.
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.
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.
18 package org.apidesign.bck2brwsr.kofx;
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;
33 /** This is an implementation package - just
34 * include its JAR on classpath and use official {@link Context} API
35 * to access the functionality.
37 * Registers {@link ContextProvider}, so {@link ServiceLoader} can find it.
39 * @author Jaroslav Tulach <jtulach@netbeans.org>
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());
47 public void fillContext(Contexts.Builder context, Class<?> requestor) {
48 context.register(Technology.class, this, 100);
49 context.register(Transfer.class, this, 100);
53 public JSObject wrapModel(Object model) {
54 return (JSObject) Knockout.createBinding(model).koData();
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);
65 public void valueHasMutated(JSObject data, String propertyName) {
66 Knockout.valueHasMutated(data, propertyName);
70 public void expose(FunctionBinding fb, Object model, JSObject d) {
71 Knockout.expose(d, fb);
75 public void applyBindings(JSObject data) {
76 Knockout.applyBindings(data);
80 public Object wrapArray(Object[] arr) {
81 return Knockout.toArray(arr);
85 public void extract(Object obj, String[] props, Object[] values) {
86 LoadJSON.extractJSON(obj, props, values);
90 public void loadJSON(final JSONCall call) {
91 LoadJSON.loadJSON(call);
95 public <M> M toModel(Class<M> modelClass, Object data) {
96 if (data instanceof JSObject) {
97 data = ((JSObject)data).getMember("ko-fx.model"); // NOI18N
99 return modelClass.cast(data);
103 public Object toJSON(InputStream is) throws IOException {
104 return LoadJSON.parse(is);