1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
|
/*
@VaadinApache2LicenseForJavaFiles@
*/
package com.vaadin.terminal.gwt.client;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.core.client.JsArray;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.user.client.Element;
import com.vaadin.shared.JavaScriptConnectorState;
import com.vaadin.shared.communication.MethodInvocation;
import com.vaadin.terminal.gwt.client.communication.StateChangeEvent;
import com.vaadin.terminal.gwt.client.communication.StateChangeEvent.StateChangeHandler;
public class JavaScriptConnectorHelper {
private final ServerConnector connector;
private final JavaScriptObject nativeState = JavaScriptObject
.createObject();
private final JavaScriptObject rpcMap = JavaScriptObject.createObject();
private final Map<String, JavaScriptObject> rpcObjects = new HashMap<String, JavaScriptObject>();
private final Map<String, Set<String>> rpcMethods = new HashMap<String, Set<String>>();
private JavaScriptObject connectorWrapper;
private int tag;
private boolean inited = false;
public JavaScriptConnectorHelper(ServerConnector connector) {
this.connector = connector;
// Wildcard rpc object
rpcObjects.put("", JavaScriptObject.createObject());
}
public void init() {
connector.addStateChangeHandler(new StateChangeHandler() {
@Override
public void onStateChanged(StateChangeEvent stateChangeEvent) {
JavaScriptObject wrapper = getConnectorWrapper();
JavaScriptConnectorState state = getConnectorState();
for (String callback : state.getCallbackNames()) {
ensureCallback(JavaScriptConnectorHelper.this, wrapper,
callback);
}
for (Entry<String, Set<String>> entry : state
.getRpcInterfaces().entrySet()) {
String rpcName = entry.getKey();
String jsName = getJsInterfaceName(rpcName);
if (!rpcObjects.containsKey(jsName)) {
Set<String> methods = entry.getValue();
rpcObjects.put(jsName,
createRpcObject(rpcName, methods));
// Init all methods for wildcard rpc
for (String method : methods) {
JavaScriptObject wildcardRpcObject = rpcObjects
.get("");
Set<String> interfaces = rpcMethods.get(method);
if (interfaces == null) {
interfaces = new HashSet<String>();
rpcMethods.put(method, interfaces);
attachRpcMethod(wildcardRpcObject, null, method);
}
interfaces.add(rpcName);
}
}
}
// Init after setting up callbacks & rpc
if (!inited) {
initJavaScript();
inited = true;
}
fireNativeStateChange(wrapper);
}
});
}
private static String getJsInterfaceName(String rpcName) {
return rpcName.replace('$', '.');
}
protected JavaScriptObject createRpcObject(String iface, Set<String> methods) {
JavaScriptObject object = JavaScriptObject.createObject();
for (String method : methods) {
attachRpcMethod(object, iface, method);
}
return object;
}
private boolean initJavaScript() {
ApplicationConfiguration conf = connector.getConnection()
.getConfiguration();
ArrayList<String> attemptedNames = new ArrayList<String>();
Integer tag = Integer.valueOf(this.tag);
while (tag != null) {
String serverSideClassName = conf.getServerSideClassNameForTag(tag);
String initFunctionName = serverSideClassName
.replaceAll("\\.", "_");
if (tryInitJs(initFunctionName, getConnectorWrapper())) {
VConsole.log("JavaScript connector initialized using "
+ initFunctionName);
return true;
} else {
VConsole.log("No JavaScript function " + initFunctionName
+ " found");
attemptedNames.add(initFunctionName);
tag = conf.getParentTag(tag.intValue());
}
}
VConsole.log("No JavaScript init for connector not found");
showInitProblem(attemptedNames);
return false;
}
protected void showInitProblem(ArrayList<String> attemptedNames) {
// Default does nothing
}
private static native boolean tryInitJs(String initFunctionName,
JavaScriptObject connectorWrapper)
/*-{
if (typeof $wnd[initFunctionName] == 'function') {
$wnd[initFunctionName].apply(connectorWrapper);
return true;
} else {
return false;
}
}-*/;
private JavaScriptObject getConnectorWrapper() {
if (connectorWrapper == null) {
connectorWrapper = createConnectorWrapper(this,
connector.getConnection(), nativeState, rpcMap,
connector.getConnectorId(), rpcObjects);
}
return connectorWrapper;
}
private static native void fireNativeStateChange(
JavaScriptObject connectorWrapper)
/*-{
if (typeof connectorWrapper.onStateChange == 'function') {
connectorWrapper.onStateChange();
}
}-*/;
private static native JavaScriptObject createConnectorWrapper(
JavaScriptConnectorHelper h, ApplicationConnection c,
JavaScriptObject nativeState, JavaScriptObject registeredRpc,
String connectorId, Map<String, JavaScriptObject> rpcObjects)
/*-{
return {
'getConnectorId': function() {
return connectorId;
},
'getParentId': $entry(function(connectorId) {
return h.@com.vaadin.terminal.gwt.client.JavaScriptConnectorHelper::getParentId(Ljava/lang/String;)(connectorId);
}),
'getState': function() {
return nativeState;
},
'getRpcProxy': $entry(function(iface) {
if (!iface) {
iface = '';
}
return rpcObjects.@java.util.Map::get(Ljava/lang/Object;)(iface);
}),
'getElement': $entry(function(connectorId) {
return h.@com.vaadin.terminal.gwt.client.JavaScriptConnectorHelper::getWidgetElement(Ljava/lang/String;)(connectorId);
}),
'registerRpc': function(iface, rpcHandler) {
//registerRpc(handler) -> registerRpc('', handler);
if (!rpcHandler) {
rpcHandler = iface;
iface = '';
}
if (!registeredRpc[iface]) {
registeredRpc[iface] = [];
}
registeredRpc[iface].push(rpcHandler);
},
'translateVaadinUri': $entry(function(uri) {
return c.@com.vaadin.terminal.gwt.client.ApplicationConnection::translateVaadinUri(Ljava/lang/String;)(uri);
}),
};
}-*/;
private native void attachRpcMethod(JavaScriptObject rpc, String iface,
String method)
/*-{
var self = this;
rpc[method] = $entry(function() {
self.@com.vaadin.terminal.gwt.client.JavaScriptConnectorHelper::fireRpc(Ljava/lang/String;Ljava/lang/String;Lcom/google/gwt/core/client/JsArray;)(iface, method, arguments);
});
}-*/;
private String getParentId(String connectorId) {
ServerConnector target = getConnector(connectorId);
if (target == null) {
return null;
}
ServerConnector parent = target.getParent();
if (parent == null) {
return null;
} else {
return parent.getConnectorId();
}
}
private Element getWidgetElement(String connectorId) {
ServerConnector target = getConnector(connectorId);
if (target instanceof ComponentConnector) {
return ((ComponentConnector) target).getWidget().getElement();
} else {
return null;
}
}
private ServerConnector getConnector(String connectorId) {
if (connectorId == null || connectorId.length() == 0) {
return connector;
}
return ConnectorMap.get(connector.getConnection()).getConnector(
connectorId);
}
private void fireRpc(String iface, String method,
JsArray<JavaScriptObject> arguments) {
if (iface == null) {
iface = findWildcardInterface(method);
}
JSONArray argumentsArray = new JSONArray(arguments);
Object[] parameters = new Object[arguments.length()];
for (int i = 0; i < parameters.length; i++) {
parameters[i] = argumentsArray.get(i);
}
connector.getConnection().addMethodInvocationToQueue(
new MethodInvocation(connector.getConnectorId(), iface, method,
parameters), true);
}
private String findWildcardInterface(String method) {
Set<String> interfaces = rpcMethods.get(method);
if (interfaces.size() == 1) {
return interfaces.iterator().next();
} else {
// TODO Resolve conflicts using argument count and types
String interfaceList = "";
for (String iface : interfaces) {
if (interfaceList.length() != 0) {
interfaceList += ", ";
}
interfaceList += getJsInterfaceName(iface);
}
throw new IllegalStateException(
"Can not call method "
+ method
+ " for wildcard rpc proxy because the function is defined for multiple rpc interfaces: "
+ interfaceList
+ ". Retrieve a rpc proxy for a specific interface using getRpcProxy(interfaceName) to use the function.");
}
}
private void fireCallback(String name, JsArray<JavaScriptObject> arguments) {
MethodInvocation invocation = new MethodInvocation(
connector.getConnectorId(),
"com.vaadin.ui.JavaScript$JavaScriptCallbackRpc", "call",
new Object[] { name, new JSONArray(arguments) });
connector.getConnection().addMethodInvocationToQueue(invocation, true);
}
public void setNativeState(JavaScriptObject state) {
updateNativeState(nativeState, state);
}
private static native void updateNativeState(JavaScriptObject state,
JavaScriptObject input)
/*-{
// Copy all fields to existing state object
for(var key in state) {
if (state.hasOwnProperty(key)) {
delete state[key];
}
}
for(var key in input) {
if (input.hasOwnProperty(key)) {
state[key] = input[key];
}
}
}-*/;
public Object[] decodeRpcParameters(JSONArray parametersJson) {
return new Object[] { parametersJson.getJavaScriptObject() };
}
public void setTag(int tag) {
this.tag = tag;
}
public void invokeJsRpc(MethodInvocation invocation,
JSONArray parametersJson) {
String iface = invocation.getInterfaceName();
String method = invocation.getMethodName();
if ("com.vaadin.ui.JavaScript$JavaScriptCallbackRpc".equals(iface)
&& "call".equals(method)) {
String callbackName = parametersJson.get(0).isString()
.stringValue();
JavaScriptObject arguments = parametersJson.get(1).isArray()
.getJavaScriptObject();
invokeCallback(getConnectorWrapper(), callbackName, arguments);
} else {
JavaScriptObject arguments = parametersJson.getJavaScriptObject();
invokeJsRpc(rpcMap, iface, method, arguments);
// Also invoke wildcard interface
invokeJsRpc(rpcMap, "", method, arguments);
}
}
private static native void invokeCallback(JavaScriptObject connector,
String name, JavaScriptObject arguments)
/*-{
connector[name].apply(connector, arguments);
}-*/;
private static native void invokeJsRpc(JavaScriptObject rpcMap,
String interfaceName, String methodName, JavaScriptObject parameters)
/*-{
var targets = rpcMap[interfaceName];
if (!targets) {
return;
}
for(var i = 0; i < targets.length; i++) {
var target = targets[i];
target[methodName].apply(target, parameters);
}
}-*/;
private static native void ensureCallback(JavaScriptConnectorHelper h,
JavaScriptObject connector, String name)
/*-{
connector[name] = $entry(function() {
var args = Array.prototype.slice.call(arguments, 0);
h.@com.vaadin.terminal.gwt.client.JavaScriptConnectorHelper::fireCallback(Ljava/lang/String;Lcom/google/gwt/core/client/JsArray;)(name, args);
});
}-*/;
private JavaScriptConnectorState getConnectorState() {
return (JavaScriptConnectorState) connector.getState();
}
}
|