jaroslav@1189
|
1 |
/**
|
jaroslav@1189
|
2 |
* HTML via Java(tm) Language Bindings
|
jaroslav@1189
|
3 |
* Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
|
jaroslav@1189
|
4 |
*
|
jaroslav@1189
|
5 |
* This program is free software: you can redistribute it and/or modify
|
jaroslav@1189
|
6 |
* it under the terms of the GNU General Public License as published by
|
jaroslav@1189
|
7 |
* the Free Software Foundation, version 2 of the License.
|
jaroslav@1189
|
8 |
*
|
jaroslav@1189
|
9 |
* This program is distributed in the hope that it will be useful,
|
jaroslav@1189
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
jaroslav@1189
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
jaroslav@1189
|
12 |
* GNU General Public License for more details. apidesign.org
|
jaroslav@1189
|
13 |
* designates this particular file as subject to the
|
jaroslav@1189
|
14 |
* "Classpath" exception as provided by apidesign.org
|
jaroslav@1189
|
15 |
* in the License file that accompanied this code.
|
jaroslav@1189
|
16 |
*
|
jaroslav@1189
|
17 |
* You should have received a copy of the GNU General Public License
|
jaroslav@1189
|
18 |
* along with this program. Look for COPYING file in the top folder.
|
jaroslav@1189
|
19 |
* If not, see http://wiki.apidesign.org/wiki/GPLwithClassPathException
|
jaroslav@1189
|
20 |
*/
|
jaroslav@1189
|
21 |
package org.apidesign.html.ko2brwsr;
|
jaroslav@1189
|
22 |
|
jaroslav@1189
|
23 |
import net.java.html.json.Context;
|
jaroslav@1189
|
24 |
import org.apidesign.html.json.spi.ContextBuilder;
|
jaroslav@1189
|
25 |
import org.apidesign.html.json.spi.FunctionBinding;
|
jaroslav@1189
|
26 |
import org.apidesign.html.json.spi.JSONCall;
|
jaroslav@1189
|
27 |
import org.apidesign.html.json.spi.PropertyBinding;
|
jaroslav@1189
|
28 |
import org.apidesign.html.json.spi.Technology;
|
jaroslav@1189
|
29 |
import org.apidesign.html.json.spi.Transfer;
|
jaroslav@1189
|
30 |
|
jaroslav@1189
|
31 |
/**
|
jaroslav@1189
|
32 |
*
|
jaroslav@1189
|
33 |
* @author Jaroslav Tulach <jtulach@netbeans.org>
|
jaroslav@1189
|
34 |
*/
|
jaroslav@1194
|
35 |
final class BrwsrCntxt implements Technology<Object>, Transfer {
|
jaroslav@1189
|
36 |
private BrwsrCntxt() {}
|
jaroslav@1189
|
37 |
|
jaroslav@1189
|
38 |
public static final Context DEFAULT;
|
jaroslav@1189
|
39 |
static {
|
jaroslav@1189
|
40 |
BrwsrCntxt c = new BrwsrCntxt();
|
jaroslav@1189
|
41 |
DEFAULT = ContextBuilder.create().withTechnology(c).withTransfer(c).build();
|
jaroslav@1189
|
42 |
}
|
jaroslav@1189
|
43 |
|
jaroslav@1189
|
44 |
@Override
|
jaroslav@1189
|
45 |
public void extract(Object obj, String[] props, Object[] values) {
|
jaroslav@1189
|
46 |
ConvertTypes.extractJSON(obj, props, values);
|
jaroslav@1189
|
47 |
}
|
jaroslav@1189
|
48 |
|
jaroslav@1189
|
49 |
@Override
|
jaroslav@1189
|
50 |
public void loadJSON(final JSONCall call) {
|
jaroslav@1189
|
51 |
class R implements Runnable {
|
jaroslav@1189
|
52 |
Object[] arr = { null };
|
jaroslav@1189
|
53 |
@Override
|
jaroslav@1189
|
54 |
public void run() {
|
jaroslav@1189
|
55 |
call.notifySuccess(arr[0]);
|
jaroslav@1189
|
56 |
}
|
jaroslav@1189
|
57 |
}
|
jaroslav@1189
|
58 |
R r = new R();
|
jaroslav@1189
|
59 |
if (call.isJSONP()) {
|
jaroslav@1189
|
60 |
String me = ConvertTypes.createJSONP(r.arr, r);
|
jaroslav@1189
|
61 |
ConvertTypes.loadJSON(call.composeURL(me), r.arr, r, me);
|
jaroslav@1189
|
62 |
} else {
|
jaroslav@1189
|
63 |
ConvertTypes.loadJSON(call.composeURL(null), r.arr, r, null);
|
jaroslav@1189
|
64 |
}
|
jaroslav@1189
|
65 |
}
|
jaroslav@1189
|
66 |
|
jaroslav@1189
|
67 |
@Override
|
jaroslav@1189
|
68 |
public Object wrapModel(Object model) {
|
jaroslav@1189
|
69 |
return model;
|
jaroslav@1189
|
70 |
}
|
jaroslav@1189
|
71 |
|
jaroslav@1189
|
72 |
@Override
|
jaroslav@1189
|
73 |
public void bind(PropertyBinding b, Object model, Object data) {
|
jaroslav@1189
|
74 |
Knockout.bind(data, b, b.getPropertyName(),
|
jaroslav@1189
|
75 |
"getValue__Ljava_lang_Object_2",
|
jaroslav@1189
|
76 |
b.isReadOnly() ? null : "setValue__VLjava_lang_Object_2",
|
jaroslav@1189
|
77 |
false, false
|
jaroslav@1189
|
78 |
);
|
jaroslav@1189
|
79 |
}
|
jaroslav@1189
|
80 |
|
jaroslav@1189
|
81 |
@Override
|
jaroslav@1189
|
82 |
public void valueHasMutated(Object data, String propertyName) {
|
jaroslav@1189
|
83 |
Knockout.valueHasMutated(data, propertyName);
|
jaroslav@1189
|
84 |
}
|
jaroslav@1189
|
85 |
|
jaroslav@1189
|
86 |
@Override
|
jaroslav@1189
|
87 |
public void expose(FunctionBinding fb, Object model, Object d) {
|
jaroslav@1189
|
88 |
Knockout.expose(d, fb, fb.getFunctionName(), "call__VLjava_lang_Object_2Ljava_lang_Object_2");
|
jaroslav@1189
|
89 |
}
|
jaroslav@1189
|
90 |
|
jaroslav@1189
|
91 |
@Override
|
jaroslav@1189
|
92 |
public void applyBindings(Object data) {
|
jaroslav@1189
|
93 |
Knockout.applyBindings(data);
|
jaroslav@1189
|
94 |
}
|
jaroslav@1189
|
95 |
|
jaroslav@1189
|
96 |
@Override
|
jaroslav@1189
|
97 |
public Object wrapArray(Object[] arr) {
|
jaroslav@1189
|
98 |
return arr;
|
jaroslav@1189
|
99 |
}
|
jaroslav@1189
|
100 |
|
jaroslav@1189
|
101 |
@Override
|
jaroslav@1189
|
102 |
public <M> M toModel(Class<M> modelClass, Object data) {
|
jaroslav@1189
|
103 |
return modelClass.cast(data);
|
jaroslav@1189
|
104 |
}
|
jaroslav@1189
|
105 |
}
|