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
|
/*
* Copyright 2011 Vaadin Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.vaadin.tests.components.javascriptcomponent;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import com.vaadin.annotations.JavaScript;
import com.vaadin.external.json.JSONArray;
import com.vaadin.external.json.JSONException;
import com.vaadin.server.DynamicConnectorResource;
import com.vaadin.server.Resource;
import com.vaadin.server.ResourceReference;
import com.vaadin.server.WrappedRequest;
import com.vaadin.shared.communication.ClientRpc;
import com.vaadin.shared.communication.ServerRpc;
import com.vaadin.shared.communication.URLReference;
import com.vaadin.shared.ui.JavaScriptComponentState;
import com.vaadin.tests.components.AbstractTestUI;
import com.vaadin.tests.util.Log;
import com.vaadin.ui.AbstractJavaScriptComponent;
import com.vaadin.ui.HasComponents;
import com.vaadin.ui.JavaScriptFunction;
public class BasicJavaScriptComponent extends AbstractTestUI {
public interface TestRpc extends ServerRpc, ClientRpc {
public void sendRpc(String message);
}
public static class TestState extends JavaScriptComponentState {
private List<String> messages = new ArrayList<String>();
private URLReference url;
public List<String> getMessages() {
return messages;
}
public void setMessages(List<String> messages) {
this.messages = messages;
}
public URLReference getUrl() {
return url;
}
public void setUrl(URLReference url) {
this.url = url;
}
}
@JavaScript("BasicJavaScriptComponentConnector.js")
public class ExampleWidget extends AbstractJavaScriptComponent {
public ExampleWidget() {
registerRpc(new TestRpc() {
@Override
public void sendRpc(String message) {
log.log("Got RPC message: " + message);
}
});
addFunction("messageToServer", new JavaScriptFunction() {
@Override
public void call(JSONArray arguments) throws JSONException {
log.log("Got callback message: " + arguments.getString(0));
}
});
addFunction("reportParentIds", new JavaScriptFunction() {
@Override
public void call(JSONArray arguments) throws JSONException {
JSONArray parentIds = arguments.getJSONArray(0);
if (!parentIds.getString(0).equals(getConnectorId())) {
log.log("Connector ids doesn't match");
}
HasComponents parent = getParent();
int i = 1;
while (parent != null) {
if (!parentIds.getString(i).equals(
parent.getConnectorId())) {
log.log("parentIds[" + i + "] doesn't match");
}
i++;
parent = parent.getParent();
}
log.log("Parent ids checked");
}
});
getRpcProxy(TestRpc.class).sendRpc("RPC message");
callFunction("messageToClient", "Callback message");
getState()
.setMessages(
Arrays.asList("First state message",
"Second state message"));
Resource resource = new DynamicConnectorResource(this, "test");
getState().setUrl(new ResourceReference(resource, null, null));
}
@Override
public TestState getState() {
return (TestState) super.getState();
}
}
private final Log log = new Log(5);
@Override
protected void setup(WrappedRequest request) {
addComponent(log);
ExampleWidget c = new ExampleWidget();
c.setCaption("Component caption");
addComponent(c);
}
@Override
protected String getTestDescription() {
return "Test for basic JavaScript component functionality.";
}
@Override
protected Integer getTicketNumber() {
return Integer.valueOf(8888);
}
}
|