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 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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.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 com.vaadin.client.widgets.Grid.Column;
  26. import elemental.json.JsonObject;
  27. import elemental.json.JsonValue;
  28. /**
  29. * An abstract base class for renderer connectors. A renderer connector is used
  30. * to link a client-side {@link Renderer} to a server-side
  31. * {@link com.vaadin.ui.components.grid.Renderer Renderer}. As a connector, it
  32. * can use the regular Vaadin RPC and shared state mechanism to pass additional
  33. * state and information between the client and the server. This base class
  34. * itself only uses the basic
  35. * {@link com.vaadin.shared.communication.SharedState SharedState} and no RPC
  36. * interfaces.
  37. *
  38. * @param <T>
  39. * the presentation type of the renderer
  40. *
  41. * @since 7.4
  42. * @author Vaadin Ltd
  43. */
  44. public abstract class AbstractRendererConnector<T> extends
  45. AbstractExtensionConnector {
  46. private Renderer<T> renderer = null;
  47. private final Type presentationType = TypeDataStore
  48. .getPresentationType(this.getClass());
  49. protected AbstractRendererConnector() {
  50. if (presentationType == null) {
  51. throw new IllegalStateException(
  52. "No presentation type found for "
  53. + getClass().getSimpleName()
  54. + ". This may be caused by some unspecified problem in widgetset compilation.");
  55. }
  56. }
  57. /**
  58. * Returns the renderer associated with this renderer connector.
  59. * <p>
  60. * A subclass of AbstractRendererConnector should override this method as
  61. * shown below. The framework uses
  62. * {@link com.google.gwt.core.client.GWT#create(Class) GWT.create(Class)} to
  63. * create a renderer based on the return type of the overridden method, but
  64. * only if {@link #createRenderer()} is not overridden as well:
  65. *
  66. * <pre>
  67. * public MyRenderer getRenderer() {
  68. * return (MyRenderer) super.getRenderer();
  69. * }
  70. * </pre>
  71. *
  72. * @return the renderer bound to this connector
  73. */
  74. public Renderer<T> getRenderer() {
  75. if (renderer == null) {
  76. renderer = createRenderer();
  77. }
  78. return renderer;
  79. }
  80. /**
  81. * Creates a new Renderer instance associated with this renderer connector.
  82. * <p>
  83. * You should typically not override this method since the framework by
  84. * default generates an implementation that uses
  85. * {@link com.google.gwt.core.client.GWT#create(Class)} to create a renderer
  86. * of the same type as returned by the most specific override of
  87. * {@link #getRenderer()}. If you do override the method, you can't call
  88. * <code>super.createRenderer()</code> since the metadata needed for that
  89. * implementation is not generated if there's an override of the method.
  90. *
  91. * @return a new renderer to be used with this connector
  92. */
  93. protected Renderer<T> createRenderer() {
  94. // TODO generate type data
  95. Type type = TypeData.getType(getClass());
  96. try {
  97. Type rendererType = type.getMethod("getRenderer").getReturnType();
  98. @SuppressWarnings("unchecked")
  99. Renderer<T> instance = (Renderer<T>) rendererType.createInstance();
  100. return instance;
  101. } catch (NoDataException e) {
  102. throw new IllegalStateException(
  103. "Default implementation of createRenderer() does not work for "
  104. + getClass().getSimpleName()
  105. + ". This might be caused by explicitely using "
  106. + "super.createRenderer() or some unspecified "
  107. + "problem with the widgetset compilation.", e);
  108. }
  109. }
  110. /**
  111. * Decodes the given JSON value into a value of type T so it can be passed
  112. * to the {@link #getRenderer() renderer}.
  113. *
  114. * @param value
  115. * the value to decode
  116. * @return the decoded value of {@code value}
  117. */
  118. public T decode(JsonValue value) {
  119. @SuppressWarnings("unchecked")
  120. T decodedValue = (T) JsonDecoder.decodeValue(presentationType, value,
  121. null, getConnection());
  122. return decodedValue;
  123. }
  124. @Override
  125. @Deprecated
  126. protected void extend(ServerConnector target) {
  127. // NOOP
  128. }
  129. /**
  130. * Gets the row key for a row object.
  131. * <p>
  132. * In case this renderer wants be able to identify a row in such a way that
  133. * the server also understands it, the row key is used for that. Rows are
  134. * identified by unified keys between the client and the server.
  135. *
  136. * @param row
  137. * the row object
  138. * @return the row key for the given row
  139. */
  140. protected String getRowKey(JsonObject row) {
  141. final ServerConnector parent = getParent();
  142. if (parent instanceof GridConnector) {
  143. return ((GridConnector) parent).getRowKey(row);
  144. } else {
  145. throw new IllegalStateException("Renderers can only be used "
  146. + "with a Grid.");
  147. }
  148. }
  149. /**
  150. * Gets the column id for a column.
  151. * <p>
  152. * In case this renderer wants be able to identify a column in such a way
  153. * that the server also understands it, the column id is used for that.
  154. * Columns are identified by unified ids between the client and the server.
  155. *
  156. * @param column
  157. * the column object
  158. * @return the column id for the given column
  159. */
  160. protected String getColumnId(Column<?, JsonObject> column) {
  161. final ServerConnector parent = getParent();
  162. if (parent instanceof GridConnector) {
  163. return ((GridConnector) parent).getColumnId(column);
  164. } else {
  165. throw new IllegalStateException("Renderers can only be used "
  166. + "with a Grid.");
  167. }
  168. }
  169. }