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

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