jaroslav@37
|
1 |
/**
|
jaroslav@358
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
jaroslav@37
|
3 |
*
|
jaroslav@365
|
4 |
* Copyright 2013-2013 Oracle and/or its affiliates. All rights reserved.
|
jaroslav@37
|
5 |
*
|
jaroslav@358
|
6 |
* Oracle and Java are registered trademarks of Oracle and/or its affiliates.
|
jaroslav@358
|
7 |
* Other names may be trademarks of their respective owners.
|
jaroslav@37
|
8 |
*
|
jaroslav@358
|
9 |
* The contents of this file are subject to the terms of either the GNU
|
jaroslav@358
|
10 |
* General Public License Version 2 only ("GPL") or the Common
|
jaroslav@358
|
11 |
* Development and Distribution License("CDDL") (collectively, the
|
jaroslav@358
|
12 |
* "License"). You may not use this file except in compliance with the
|
jaroslav@358
|
13 |
* License. You can obtain a copy of the License at
|
jaroslav@358
|
14 |
* http://www.netbeans.org/cddl-gplv2.html
|
jaroslav@358
|
15 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
|
jaroslav@358
|
16 |
* specific language governing permissions and limitations under the
|
jaroslav@358
|
17 |
* License. When distributing the software, include this License Header
|
jaroslav@358
|
18 |
* Notice in each file and include the License file at
|
jaroslav@358
|
19 |
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
|
jaroslav@358
|
20 |
* particular file as subject to the "Classpath" exception as provided
|
jaroslav@358
|
21 |
* by Oracle in the GPL Version 2 section of the License file that
|
jaroslav@358
|
22 |
* accompanied this code. If applicable, add the following below the
|
jaroslav@358
|
23 |
* License Header, with the fields enclosed by brackets [] replaced by
|
jaroslav@358
|
24 |
* your own identifying information:
|
jaroslav@358
|
25 |
* "Portions Copyrighted [year] [name of copyright owner]"
|
jaroslav@358
|
26 |
*
|
jaroslav@358
|
27 |
* Contributor(s):
|
jaroslav@358
|
28 |
*
|
jaroslav@358
|
29 |
* The Original Software is NetBeans. The Initial Developer of the Original
|
jaroslav@358
|
30 |
* Software is Oracle. Portions Copyright 2013-2013 Oracle. All Rights Reserved.
|
jaroslav@358
|
31 |
*
|
jaroslav@358
|
32 |
* If you wish your version of this file to be governed by only the CDDL
|
jaroslav@358
|
33 |
* or only the GPL Version 2, indicate your decision by adding
|
jaroslav@358
|
34 |
* "[Contributor] elects to include this software in this distribution
|
jaroslav@358
|
35 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a
|
jaroslav@358
|
36 |
* single choice of license, a recipient has the option to distribute
|
jaroslav@358
|
37 |
* your version of this file under either the CDDL, the GPL Version 2 or
|
jaroslav@358
|
38 |
* to extend the choice of license to its licensees as provided above.
|
jaroslav@358
|
39 |
* However, if you add GPL Version 2 code and therefore, elected the GPL
|
jaroslav@358
|
40 |
* Version 2 license, then the option applies only if the new code is
|
jaroslav@358
|
41 |
* made subject to such option by the copyright holder.
|
jaroslav@37
|
42 |
*/
|
jaroslav@442
|
43 |
package org.netbeans.html.ko4j;
|
jaroslav@37
|
44 |
|
jaroslav@446
|
45 |
import java.io.ByteArrayOutputStream;
|
jaroslav@322
|
46 |
import java.io.Closeable;
|
jaroslav@60
|
47 |
import java.io.IOException;
|
jaroslav@60
|
48 |
import java.io.InputStream;
|
jaroslav@446
|
49 |
import java.io.InputStreamReader;
|
jaroslav@431
|
50 |
import java.util.concurrent.Executor;
|
jaroslav@37
|
51 |
import java.util.logging.Logger;
|
jaroslav@150
|
52 |
import net.java.html.js.JavaScriptBody;
|
jaroslav@288
|
53 |
import org.apidesign.html.boot.spi.Fn;
|
jaroslav@37
|
54 |
import org.apidesign.html.json.spi.FunctionBinding;
|
jaroslav@37
|
55 |
import org.apidesign.html.json.spi.JSONCall;
|
jaroslav@37
|
56 |
import org.apidesign.html.json.spi.PropertyBinding;
|
jaroslav@37
|
57 |
import org.apidesign.html.json.spi.Technology;
|
jaroslav@37
|
58 |
import org.apidesign.html.json.spi.Transfer;
|
jaroslav@258
|
59 |
import org.apidesign.html.json.spi.WSTransfer;
|
jaroslav@37
|
60 |
|
jaroslav@54
|
61 |
/** This is an implementation package - just
|
jaroslav@54
|
62 |
* include its JAR on classpath and use official {@link Context} API
|
jaroslav@54
|
63 |
* to access the functionality.
|
jaroslav@54
|
64 |
* <p>
|
jaroslav@37
|
65 |
*
|
jaroslav@37
|
66 |
* @author Jaroslav Tulach <jtulach@netbeans.org>
|
jaroslav@37
|
67 |
*/
|
jaroslav@442
|
68 |
final class FXContext
|
jaroslav@430
|
69 |
implements Technology.BatchInit<Object>, Transfer, WSTransfer<LoadWS> {
|
jaroslav@37
|
70 |
static final Logger LOG = Logger.getLogger(FXContext.class.getName());
|
jaroslav@254
|
71 |
private static Boolean javaScriptEnabled;
|
jaroslav@288
|
72 |
private final Fn.Presenter browserContext;
|
jaroslav@288
|
73 |
|
jaroslav@288
|
74 |
public FXContext(Fn.Presenter browserContext) {
|
jaroslav@288
|
75 |
this.browserContext = browserContext;
|
jaroslav@288
|
76 |
}
|
jaroslav@150
|
77 |
|
jaroslav@446
|
78 |
@JavaScriptBody(args = {}, body = "if (window) return true; else return false;")
|
jaroslav@254
|
79 |
private static boolean isJavaScriptEnabledJs() {
|
jaroslav@254
|
80 |
return false;
|
jaroslav@254
|
81 |
}
|
jaroslav@254
|
82 |
|
jaroslav@288
|
83 |
static boolean isJavaScriptEnabled() {
|
jaroslav@254
|
84 |
if (javaScriptEnabled != null) {
|
jaroslav@254
|
85 |
return javaScriptEnabled;
|
jaroslav@254
|
86 |
}
|
jaroslav@254
|
87 |
return javaScriptEnabled = isJavaScriptEnabledJs();
|
jaroslav@150
|
88 |
}
|
jaroslav@40
|
89 |
|
jaroslav@258
|
90 |
final boolean areWebSocketsSupported() {
|
jaroslav@258
|
91 |
return LoadWS.isSupported();
|
jaroslav@258
|
92 |
}
|
jaroslav@258
|
93 |
|
jaroslav@275
|
94 |
|
jaroslav@275
|
95 |
@Override
|
jaroslav@430
|
96 |
public Object wrapModel(Object model, PropertyBinding[] propArr, FunctionBinding[] funcArr) {
|
jaroslav@275
|
97 |
String[] propNames = new String[propArr.length];
|
jaroslav@275
|
98 |
boolean[] propReadOnly = new boolean[propArr.length];
|
jaroslav@279
|
99 |
Object[] propValues = new Object[propArr.length];
|
jaroslav@275
|
100 |
for (int i = 0; i < propNames.length; i++) {
|
jaroslav@275
|
101 |
propNames[i] = propArr[i].getPropertyName();
|
jaroslav@275
|
102 |
propReadOnly[i] = propArr[i].isReadOnly();
|
jaroslav@279
|
103 |
propValues[i] = propArr[i].getValue();
|
jaroslav@275
|
104 |
}
|
jaroslav@275
|
105 |
String[] funcNames = new String[funcArr.length];
|
jaroslav@275
|
106 |
for (int i = 0; i < funcNames.length; i++) {
|
jaroslav@275
|
107 |
funcNames[i] = funcArr[i].getFunctionName();
|
jaroslav@275
|
108 |
}
|
jaroslav@430
|
109 |
Object ret = Knockout.wrapModel(model,
|
jaroslav@430
|
110 |
propNames, propReadOnly, propValues, propArr,
|
jaroslav@279
|
111 |
funcNames, funcArr
|
jaroslav@279
|
112 |
);
|
jaroslav@430
|
113 |
return ret;
|
jaroslav@275
|
114 |
}
|
jaroslav@275
|
115 |
|
jaroslav@37
|
116 |
@Override
|
jaroslav@430
|
117 |
public Object wrapModel(Object model) {
|
jaroslav@276
|
118 |
throw new UnsupportedOperationException();
|
jaroslav@37
|
119 |
}
|
jaroslav@37
|
120 |
|
jaroslav@37
|
121 |
@Override
|
jaroslav@430
|
122 |
public void bind(PropertyBinding b, Object model, Object data) {
|
jaroslav@276
|
123 |
throw new UnsupportedOperationException();
|
jaroslav@37
|
124 |
}
|
jaroslav@37
|
125 |
|
jaroslav@37
|
126 |
@Override
|
jaroslav@430
|
127 |
public void valueHasMutated(Object data, String propertyName) {
|
jaroslav@37
|
128 |
Knockout.valueHasMutated(data, propertyName);
|
jaroslav@37
|
129 |
}
|
jaroslav@37
|
130 |
|
jaroslav@37
|
131 |
@Override
|
jaroslav@430
|
132 |
public void expose(FunctionBinding fb, Object model, Object d) {
|
jaroslav@276
|
133 |
throw new UnsupportedOperationException();
|
jaroslav@37
|
134 |
}
|
jaroslav@37
|
135 |
|
jaroslav@37
|
136 |
@Override
|
jaroslav@430
|
137 |
public void applyBindings(Object data) {
|
jaroslav@37
|
138 |
Knockout.applyBindings(data);
|
jaroslav@37
|
139 |
}
|
jaroslav@37
|
140 |
|
jaroslav@37
|
141 |
@Override
|
jaroslav@37
|
142 |
public Object wrapArray(Object[] arr) {
|
jaroslav@430
|
143 |
return arr;
|
jaroslav@37
|
144 |
}
|
jaroslav@37
|
145 |
|
jaroslav@37
|
146 |
@Override
|
jaroslav@37
|
147 |
public void extract(Object obj, String[] props, Object[] values) {
|
jaroslav@37
|
148 |
LoadJSON.extractJSON(obj, props, values);
|
jaroslav@37
|
149 |
}
|
jaroslav@37
|
150 |
|
jaroslav@37
|
151 |
@Override
|
jaroslav@37
|
152 |
public void loadJSON(final JSONCall call) {
|
jaroslav@446
|
153 |
if (call.isJSONP()) {
|
jaroslav@446
|
154 |
String me = LoadJSON.createJSONP(call);
|
jaroslav@446
|
155 |
LoadJSON.loadJSONP(call.composeURL(me), me);
|
jaroslav@446
|
156 |
} else {
|
jaroslav@446
|
157 |
String data = null;
|
jaroslav@446
|
158 |
if (call.isDoOutput()) {
|
jaroslav@446
|
159 |
try {
|
jaroslav@446
|
160 |
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
jaroslav@446
|
161 |
call.writeData(bos);
|
jaroslav@446
|
162 |
data = new String(bos.toByteArray(), "UTF-8");
|
jaroslav@446
|
163 |
} catch (IOException ex) {
|
jaroslav@446
|
164 |
call.notifyError(ex);
|
jaroslav@446
|
165 |
}
|
jaroslav@446
|
166 |
}
|
jaroslav@446
|
167 |
LoadJSON.loadJSON(call.composeURL(null), call, call.getMethod(), data);
|
jaroslav@446
|
168 |
}
|
jaroslav@37
|
169 |
}
|
jaroslav@38
|
170 |
|
jaroslav@38
|
171 |
@Override
|
jaroslav@38
|
172 |
public <M> M toModel(Class<M> modelClass, Object data) {
|
jaroslav@430
|
173 |
return modelClass.cast(Knockout.toModel(data));
|
jaroslav@38
|
174 |
}
|
jaroslav@60
|
175 |
|
jaroslav@60
|
176 |
@Override
|
jaroslav@60
|
177 |
public Object toJSON(InputStream is) throws IOException {
|
jaroslav@446
|
178 |
StringBuilder sb = new StringBuilder();
|
jaroslav@446
|
179 |
InputStreamReader r = new InputStreamReader(is);
|
jaroslav@446
|
180 |
for (;;) {
|
jaroslav@446
|
181 |
int ch = r.read();
|
jaroslav@446
|
182 |
if (ch == -1) {
|
jaroslav@446
|
183 |
break;
|
jaroslav@446
|
184 |
}
|
jaroslav@446
|
185 |
sb.append((char)ch);
|
jaroslav@446
|
186 |
}
|
jaroslav@446
|
187 |
return LoadJSON.parse(sb.toString());
|
jaroslav@60
|
188 |
}
|
jaroslav@240
|
189 |
|
jaroslav@240
|
190 |
@Override
|
jaroslav@288
|
191 |
public void runSafe(final Runnable r) {
|
jaroslav@288
|
192 |
class Wrap implements Runnable {
|
jaroslav@288
|
193 |
@Override public void run() {
|
jaroslav@430
|
194 |
Closeable c = Fn.activate(browserContext);
|
jaroslav@430
|
195 |
try {
|
jaroslav@288
|
196 |
r.run();
|
jaroslav@430
|
197 |
} finally {
|
jaroslav@430
|
198 |
try {
|
jaroslav@430
|
199 |
c.close();
|
jaroslav@430
|
200 |
} catch (IOException ex) {
|
jaroslav@430
|
201 |
// cannot be thrown
|
jaroslav@430
|
202 |
}
|
jaroslav@288
|
203 |
}
|
jaroslav@288
|
204 |
}
|
jaroslav@288
|
205 |
}
|
jaroslav@288
|
206 |
Wrap w = new Wrap();
|
jaroslav@431
|
207 |
if (browserContext instanceof Executor) {
|
jaroslav@431
|
208 |
((Executor)browserContext).execute(w);
|
jaroslav@431
|
209 |
} else {
|
jaroslav@431
|
210 |
w.run();
|
jaroslav@262
|
211 |
}
|
jaroslav@240
|
212 |
}
|
jaroslav@258
|
213 |
|
jaroslav@258
|
214 |
@Override
|
jaroslav@258
|
215 |
public LoadWS open(String url, JSONCall onReply) {
|
jaroslav@258
|
216 |
return new LoadWS(onReply, url);
|
jaroslav@258
|
217 |
}
|
jaroslav@258
|
218 |
|
jaroslav@258
|
219 |
@Override
|
jaroslav@258
|
220 |
public void send(LoadWS socket, JSONCall data) {
|
jaroslav@258
|
221 |
socket.send(data);
|
jaroslav@258
|
222 |
}
|
jaroslav@258
|
223 |
|
jaroslav@258
|
224 |
@Override
|
jaroslav@258
|
225 |
public void close(LoadWS socket) {
|
jaroslav@258
|
226 |
socket.close();
|
jaroslav@258
|
227 |
}
|
jaroslav@288
|
228 |
|
jaroslav@37
|
229 |
}
|