*/
package com.vaadin.tests.components.javascriptcomponent;
+import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import com.vaadin.external.json.JSONArray;
import com.vaadin.external.json.JSONException;
import com.vaadin.terminal.WrappedRequest;
+import com.vaadin.terminal.gwt.client.communication.ClientRpc;
import com.vaadin.terminal.gwt.client.communication.ServerRpc;
import com.vaadin.terminal.gwt.client.ui.JavaScriptComponentState;
import com.vaadin.tests.components.AbstractTestRoot;
+import com.vaadin.tests.util.Log;
import com.vaadin.ui.AbstractJavaScriptComponent;
+import com.vaadin.ui.HasComponents;
import com.vaadin.ui.JavaScriptCallback;
-import com.vaadin.ui.Notification;
public class BasicJavaScriptComponent extends AbstractTestRoot {
- public interface ExampleClickRpc extends ServerRpc {
- public void onClick(String message);
+ public interface TestRpc extends ServerRpc, ClientRpc {
+ public void sendRpc(String message);
}
- public static class SpecialState extends JavaScriptComponentState {
- private List<String> data;
+ public static class TestState extends JavaScriptComponentState {
+ private List<String> messages = new ArrayList<String>();
- public List<String> getData() {
- return data;
+ public List<String> getMessages() {
+ return messages;
}
- public void setData(List<String> data) {
- this.data = data;
+ public void setMessages(List<String> messages) {
+ this.messages = messages;
}
}
- @JavaScript("/statictestfiles/jsconnector.js")
- public static class ExampleWidget extends AbstractJavaScriptComponent {
+ @JavaScript("BasicJavaScriptComponentConnector.js")
+ public class ExampleWidget extends AbstractJavaScriptComponent {
public ExampleWidget() {
- registerRpc(new ExampleClickRpc() {
- public void onClick(String message) {
- Notification.show("Got a click: " + message);
+ registerRpc(new TestRpc() {
+ public void sendRpc(String message) {
+ log.log("Got RPC message: " + message);
}
});
- registerCallback("onclick", new JavaScriptCallback() {
+ registerCallback("messageToServer", new JavaScriptCallback() {
public void call(JSONArray arguments) throws JSONException {
- Notification.show("Got a callback: "
- + arguments.getString(0));
+ log.log("Got callback message: " + arguments.getString(0));
}
});
- getState().setData(Arrays.asList("a", "b", "c"));
+
+ registerCallback("reportParentIds", new JavaScriptCallback() {
+ 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");
+ invokeCallback("messageToClient", "Callback message");
+
+ getState()
+ .setMessages(
+ Arrays.asList("First state message",
+ "Second state message"));
}
@Override
- public SpecialState getState() {
- return (SpecialState) super.getState();
+ 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("test caption");
- c.setDescription("Some description");
+ c.setCaption("Component caption");
addComponent(c);
}
@Override
protected String getTestDescription() {
- // TODO Auto-generated method stub
- return null;
+ return "Test for basic JavaScript component functionality.";
}
@Override
protected Integer getTicketNumber() {
- // TODO Auto-generated method stub
- return null;
+ return Integer.valueOf(8888);
}
}
--- /dev/null
+window.com_vaadin_tests_components_javascriptcomponent_BasicJavaScriptComponent_ExampleWidget = function() {
+ var parentIds = [];
+ var connectorId = this.getConnectorId();
+ while(connectorId) {
+ parentIds.push(connectorId);
+ connectorId = this.getParentId(connectorId);
+ }
+ this.reportParentIds(parentIds);
+
+ this.onStateChange = function() {
+ var e = this.getElement();
+
+ e.innerHTML = '';
+
+ var row = 1;
+ var log = function(text) {
+ e.innerHTML = "<div>" + row++ + ". " + text + "</div>" + e.innerHTML;
+ }
+
+ log("Parent element className: " + this.getElement(this.getParentId()).className);
+
+ var messages = this.getState().messages;
+ for(var i = 0; i < messages.length; i++) {
+ log("State message: " + messages[i]);
+ }
+ }
+
+ this.registerRpc({
+ sendRpc: function(message) {
+ this.getRpcProxy().sendRpc(message + " processed");
+ }
+ });
+
+ this.messageToClient = function(message) {
+ this.messageToServer(message + " processed");
+ }
+}
\ No newline at end of file