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.

EncoderResultDisplayConnector.java 2.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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.widgetset.client;
  17. import java.util.Arrays;
  18. import java.util.List;
  19. import com.vaadin.client.ServerConnector;
  20. import com.vaadin.client.communication.JsonEncoder;
  21. import com.vaadin.client.extensions.AbstractExtensionConnector;
  22. import com.vaadin.client.metadata.Type;
  23. import com.vaadin.client.metadata.TypeData;
  24. import com.vaadin.shared.communication.ServerRpc;
  25. import com.vaadin.shared.ui.Connect;
  26. import com.vaadin.tests.serialization.EncodeResultDisplay.EncoderResultDisplayExtension;
  27. import elemental.json.JsonValue;
  28. @Connect(EncoderResultDisplayExtension.class)
  29. public class EncoderResultDisplayConnector extends AbstractExtensionConnector {
  30. private ReportRpc reporter;
  31. public interface ReportRpc extends ServerRpc {
  32. public void report(String name, String encodedValue);
  33. }
  34. @Override
  35. protected void extend(ServerConnector target) {
  36. reporter = getRpcProxy(ReportRpc.class);
  37. reportEncode("My string");
  38. reportEncode(Character.valueOf('v'));
  39. reportEncode(Byte.valueOf((byte) 1));
  40. reportEncode(Integer.valueOf(3));
  41. reportEncode(Long.valueOf(Integer.MAX_VALUE + 1l));
  42. reportEncode(Float.valueOf((float) 1.1));
  43. reportEncode(Double.valueOf("2.2"));
  44. reportEncode(new String[] { "One", "Two" });
  45. reportEncode(
  46. "List",
  47. Arrays.asList("Three", "Four"),
  48. new Type(List.class.getName(), new Type[] { TypeData
  49. .getType(String.class) }));
  50. reportEncode(new SimpleTestBean(5));
  51. reportEncode(Void.class.getSimpleName(), null,
  52. TypeData.getType(Void.class));
  53. }
  54. private void reportEncode(Object value) {
  55. Type type = TypeData.getType(value.getClass());
  56. reportEncode(value.getClass().getSimpleName(), value, type);
  57. }
  58. private void reportEncode(String name, Object value, Type type) {
  59. JsonValue encodedValue = JsonEncoder.encode(value, type,
  60. getConnection());
  61. reporter.report(name, encodedValue.toJson());
  62. }
  63. }