You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SimpleJavaScriptExtensionTest.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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.extensions;
  17. import com.vaadin.annotations.JavaScript;
  18. import com.vaadin.annotations.StyleSheet;
  19. import com.vaadin.server.AbstractJavaScriptExtension;
  20. import com.vaadin.server.VaadinRequest;
  21. import com.vaadin.shared.JavaScriptExtensionState;
  22. import com.vaadin.shared.communication.ClientRpc;
  23. import com.vaadin.shared.communication.ServerRpc;
  24. import com.vaadin.tests.components.AbstractTestUI;
  25. import com.vaadin.ui.Button;
  26. import com.vaadin.ui.Button.ClickEvent;
  27. import com.vaadin.ui.JavaScriptFunction;
  28. import com.vaadin.ui.Notification;
  29. import elemental.json.JsonArray;
  30. public class SimpleJavaScriptExtensionTest extends AbstractTestUI {
  31. public static class SimpleJavaScriptExtensionState extends
  32. JavaScriptExtensionState {
  33. private String prefix;
  34. public void setPrefix(String prefix) {
  35. this.prefix = prefix;
  36. }
  37. public String getPrefix() {
  38. return prefix;
  39. }
  40. }
  41. public static interface SimpleJavaScriptExtensionClientRpc extends
  42. ClientRpc {
  43. public void greet(String message);
  44. }
  45. public static interface SimpleJavaScriptExtensionServerRpc extends
  46. ServerRpc {
  47. public void greet(String message);
  48. }
  49. @JavaScript("/statictestfiles/jsextension.js")
  50. @StyleSheet("/VAADIN/external1.css")
  51. public static class SimpleJavascriptExtension extends
  52. AbstractJavaScriptExtension {
  53. public SimpleJavascriptExtension() {
  54. registerRpc(new SimpleJavaScriptExtensionServerRpc() {
  55. @Override
  56. public void greet(String message) {
  57. Notification.show(getState().getPrefix() + message);
  58. }
  59. });
  60. addFunction("greetToServer", new JavaScriptFunction() {
  61. @Override
  62. public void call(JsonArray arguments) {
  63. Notification.show(getState().getPrefix()
  64. + arguments.getString(0));
  65. }
  66. });
  67. }
  68. @Override
  69. public SimpleJavaScriptExtensionState getState() {
  70. return (SimpleJavaScriptExtensionState) super.getState();
  71. }
  72. public void setPrefix(String prefix) {
  73. getState().setPrefix(prefix);
  74. }
  75. public void greetRpc(String message) {
  76. getRpcProxy(SimpleJavaScriptExtensionClientRpc.class)
  77. .greet(message);
  78. }
  79. public void greetCallback(String message) {
  80. callFunction("greetToClient", message);
  81. }
  82. }
  83. @Override
  84. protected void setup(VaadinRequest request) {
  85. final SimpleJavascriptExtension simpleJavascriptExtension = new SimpleJavascriptExtension();
  86. simpleJavascriptExtension.setPrefix("Prefix: ");
  87. addExtension(simpleJavascriptExtension);
  88. addComponent(new Button("Send rpc greeting",
  89. new Button.ClickListener() {
  90. @Override
  91. public void buttonClick(ClickEvent event) {
  92. simpleJavascriptExtension.greetRpc("Rpc greeting");
  93. }
  94. }));
  95. addComponent(new Button("Send callback greeting",
  96. new Button.ClickListener() {
  97. @Override
  98. public void buttonClick(ClickEvent event) {
  99. simpleJavascriptExtension
  100. .greetCallback("Callback greeting");
  101. }
  102. }));
  103. }
  104. @Override
  105. protected String getTestDescription() {
  106. // TODO Auto-generated method stub
  107. return null;
  108. }
  109. @Override
  110. protected Integer getTicketNumber() {
  111. // TODO Auto-generated method stub
  112. return null;
  113. }
  114. }