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.

AbstractRendererConnector.java 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright 2000-2016 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.client.connectors;
  17. import com.vaadin.client.ServerConnector;
  18. import com.vaadin.client.communication.JsonDecoder;
  19. import com.vaadin.client.extensions.AbstractExtensionConnector;
  20. import com.vaadin.client.metadata.NoDataException;
  21. import com.vaadin.client.metadata.Type;
  22. import com.vaadin.client.metadata.TypeData;
  23. import com.vaadin.client.metadata.TypeDataStore;
  24. import com.vaadin.client.renderers.Renderer;
  25. import elemental.json.JsonValue;
  26. /**
  27. * An abstract base class for renderer connectors.
  28. *
  29. * @param <T>
  30. * the presentation type of the renderer
  31. */
  32. public abstract class AbstractRendererConnector<T>
  33. extends AbstractExtensionConnector {
  34. private Renderer<T> renderer = null;
  35. private final Type presentationType = TypeDataStore
  36. .getPresentationType(this.getClass());
  37. protected AbstractRendererConnector() {
  38. if (presentationType == null) {
  39. throw new IllegalStateException("No presentation type found for "
  40. + getClass().getSimpleName()
  41. + ". This may be caused by some unspecified problem in widgetset compilation.");
  42. }
  43. }
  44. /**
  45. * Returns the renderer associated with this renderer connector.
  46. * <p>
  47. * A subclass of AbstractRendererConnector should override this method as
  48. * shown below. The framework uses
  49. * {@link com.google.gwt.core.client.GWT#create(Class) GWT.create(Class)} to
  50. * create a renderer based on the return type of the overridden method, but
  51. * only if {@link #createRenderer()} is not overridden as well:
  52. *
  53. * <pre>
  54. * public MyRenderer getRenderer() {
  55. * return (MyRenderer) super.getRenderer();
  56. * }
  57. * </pre>
  58. *
  59. * @return the renderer bound to this connector
  60. */
  61. public Renderer<T> getRenderer() {
  62. if (renderer == null) {
  63. renderer = createRenderer();
  64. }
  65. return renderer;
  66. }
  67. /**
  68. * Creates a new Renderer instance associated with this renderer connector.
  69. * <p>
  70. * You should typically not override this method since the framework by
  71. * default generates an implementation that uses
  72. * {@link com.google.gwt.core.client.GWT#create(Class)} to create a renderer
  73. * of the same type as returned by the most specific override of
  74. * {@link #getRenderer()}. If you do override the method, you can't call
  75. * <code>super.createRenderer()</code> since the metadata needed for that
  76. * implementation is not generated if there's an override of the method.
  77. *
  78. * @return a new renderer to be used with this connector
  79. */
  80. protected Renderer<T> createRenderer() {
  81. // TODO generate type data
  82. Type type = TypeData.getType(getClass());
  83. try {
  84. Type rendererType = type.getMethod("getRenderer").getReturnType();
  85. @SuppressWarnings("unchecked")
  86. Renderer<T> instance = (Renderer<T>) rendererType.createInstance();
  87. return instance;
  88. } catch (NoDataException e) {
  89. throw new IllegalStateException(
  90. "Default implementation of createRenderer() does not work for "
  91. + getClass().getSimpleName()
  92. + ". This might be caused by explicitely using "
  93. + "super.createRenderer() or some unspecified "
  94. + "problem with the widgetset compilation.",
  95. e);
  96. }
  97. }
  98. /**
  99. * Decodes the given JSON value into a value of type T so it can be passed
  100. * to the {@link #getRenderer() renderer}.
  101. *
  102. * @param value
  103. * the value to decode
  104. * @return the decoded value of {@code value}
  105. */
  106. public T decode(JsonValue value) {
  107. @SuppressWarnings("unchecked")
  108. T decodedValue = (T) JsonDecoder.decodeValue(presentationType, value,
  109. null, getConnection());
  110. return decodedValue;
  111. }
  112. @Override
  113. @Deprecated
  114. protected void extend(ServerConnector target) {
  115. // NOOP
  116. }
  117. }