選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

BasicJavaScriptComponent.java 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * Copyright 2000-2014 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.tests.components.javascriptcomponent;
  17. import java.util.ArrayList;
  18. import java.util.Arrays;
  19. import java.util.List;
  20. import com.vaadin.annotations.JavaScript;
  21. import com.vaadin.server.ConnectorResource;
  22. import com.vaadin.server.DownloadStream;
  23. import com.vaadin.server.Resource;
  24. import com.vaadin.server.ResourceReference;
  25. import com.vaadin.server.VaadinRequest;
  26. import com.vaadin.shared.communication.ClientRpc;
  27. import com.vaadin.shared.communication.ServerRpc;
  28. import com.vaadin.shared.communication.URLReference;
  29. import com.vaadin.shared.ui.JavaScriptComponentState;
  30. import com.vaadin.tests.components.AbstractTestUI;
  31. import com.vaadin.tests.util.Log;
  32. import com.vaadin.ui.AbstractJavaScriptComponent;
  33. import com.vaadin.ui.Button;
  34. import com.vaadin.ui.Button.ClickEvent;
  35. import com.vaadin.ui.HasComponents;
  36. import com.vaadin.ui.JavaScriptFunction;
  37. import elemental.json.JsonArray;
  38. import elemental.json.JsonObject;
  39. import elemental.json.JsonValue;
  40. public class BasicJavaScriptComponent extends AbstractTestUI {
  41. public interface TestRpc extends ServerRpc, ClientRpc {
  42. public void sendRpc(String message);
  43. }
  44. public static class TestState extends JavaScriptComponentState {
  45. private List<String> messages = new ArrayList<String>();
  46. private URLReference url;
  47. public List<String> getMessages() {
  48. return messages;
  49. }
  50. public void setMessages(List<String> messages) {
  51. this.messages = messages;
  52. }
  53. public URLReference getUrl() {
  54. return url;
  55. }
  56. public void setUrl(URLReference url) {
  57. this.url = url;
  58. }
  59. }
  60. @JavaScript("BasicJavaScriptComponentConnector.js")
  61. public class ExampleWidget extends AbstractJavaScriptComponent {
  62. public ExampleWidget() {
  63. registerRpc(new TestRpc() {
  64. @Override
  65. public void sendRpc(String message) {
  66. log.log("Got RPC message: " + message);
  67. }
  68. });
  69. addFunction("messageToServer", new JavaScriptFunction() {
  70. @Override
  71. public void call(JsonArray arguments) {
  72. log.log("Got callback message: " + arguments.getString(0));
  73. }
  74. });
  75. addFunction("reportParentIds", new JavaScriptFunction() {
  76. @Override
  77. public void call(JsonArray arguments) {
  78. JsonArray parentIds = arguments.getArray(0);
  79. if (!parentIds.getString(0).equals(getConnectorId())) {
  80. log.log("Connector ids doesn't match");
  81. }
  82. HasComponents parent = getParent();
  83. int i = 1;
  84. while (parent != null) {
  85. if (!parentIds.getString(i).equals(
  86. parent.getConnectorId())) {
  87. log.log("parentIds[" + i + "] doesn't match");
  88. }
  89. i++;
  90. parent = parent.getParent();
  91. }
  92. log.log("Parent ids checked");
  93. }
  94. });
  95. addFunction("sendDifferentTypeOfData", new JavaScriptFunction() {
  96. @Override
  97. public void call(JsonArray arguments) {
  98. for (int i = 0; i < arguments.length(); i++) {
  99. JsonValue arg = arguments.get(i);
  100. if (arg instanceof JsonObject) {
  101. JsonObject o = (JsonObject) arg;
  102. log.log("Argument[" + i + "] type: "
  103. + arg.getClass().getName());
  104. for (String key : o.keys()) {
  105. JsonValue v = o.get(key);
  106. log.log("Argument[" + i + "][" + key
  107. + "] type: " + v.getClass().getName()
  108. + ", value: " + v.asString());
  109. }
  110. } else {
  111. log.log("Argument[" + i + "] type: "
  112. + arg.getClass().getName() + ", value: "
  113. + arg.asString());
  114. }
  115. }
  116. }
  117. });
  118. getRpcProxy(TestRpc.class).sendRpc("RPC message");
  119. callFunction("messageToClient", "Callback message");
  120. getState()
  121. .setMessages(
  122. Arrays.asList("First state message",
  123. "Second state message"));
  124. // Dummy resource used to test URL translation
  125. Resource resource = new ConnectorResource() {
  126. @Override
  127. public String getMIMEType() {
  128. return null;
  129. }
  130. @Override
  131. public DownloadStream getStream() {
  132. return null;
  133. }
  134. @Override
  135. public String getFilename() {
  136. return null;
  137. }
  138. };
  139. getState().setUrl(new ResourceReference(resource, this, "test"));
  140. }
  141. @Override
  142. public TestState getState() {
  143. return (TestState) super.getState();
  144. }
  145. }
  146. private final Log log = new Log(15);
  147. @Override
  148. protected void setup(VaadinRequest request) {
  149. addComponent(log);
  150. final ExampleWidget c = new ExampleWidget();
  151. c.setCaption("Component caption");
  152. addComponent(c);
  153. Button removeButton = new Button("Remove component",
  154. new Button.ClickListener() {
  155. @Override
  156. public void buttonClick(ClickEvent event) {
  157. removeComponent(c);
  158. }
  159. });
  160. removeButton.setId("RemoveButton");
  161. addComponent(removeButton);
  162. }
  163. @Override
  164. protected String getTestDescription() {
  165. return "Test for basic JavaScript component functionality.";
  166. }
  167. @Override
  168. protected Integer getTicketNumber() {
  169. return Integer.valueOf(8888);
  170. }
  171. }