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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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.google.gwt.json.client.JSONValue;
  18. import com.vaadin.client.ServerConnector;
  19. import com.vaadin.client.Util;
  20. import com.vaadin.client.communication.JsonDecoder;
  21. import com.vaadin.client.extensions.AbstractExtensionConnector;
  22. import com.vaadin.client.metadata.NoDataException;
  23. import com.vaadin.client.metadata.Type;
  24. import com.vaadin.client.metadata.TypeData;
  25. import com.vaadin.client.metadata.TypeDataStore;
  26. import com.vaadin.client.ui.grid.Renderer;
  27. /**
  28. * An abstract base class for renderer connectors. A renderer connector is used
  29. * to link a client-side {@link Renderer} to a server-side
  30. * {@link com.vaadin.ui.components.grid.Renderer Renderer}. As a connector, it
  31. * can use the regular Vaadin RPC and shared state mechanism to pass additional
  32. * state and information between the client and the server. This base class
  33. * itself only uses the basic
  34. * {@link com.vaadin.shared.communication.SharedState SharedState} and no RPC
  35. * interfaces.
  36. *
  37. * @param <T>
  38. * the presentation type of the renderer
  39. *
  40. * @since
  41. * @author Vaadin Ltd
  42. */
  43. public abstract class AbstractRendererConnector<T> extends
  44. 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(
  51. "No presentation type found for "
  52. + Util.getSimpleName(this)
  53. + ". This may be caused by some unspecified problem in widgetset compilation.");
  54. }
  55. }
  56. /**
  57. * Returns the renderer associated with this renderer connector.
  58. * <p>
  59. * A subclass of AbstractRendererConnector should override this method as
  60. * shown below. The framework uses
  61. * {@link com.google.gwt.core.client.GWT#create(Class) GWT.create(Class)} to
  62. * create a renderer based on the return type of the overridden method, but
  63. * only if {@link #createRenderer()} is not overridden as well:
  64. *
  65. * <pre>
  66. * public MyRenderer getRenderer() {
  67. * return (MyRenderer) super.getRenderer();
  68. * }
  69. * </pre>
  70. *
  71. * @return the renderer bound to this connector
  72. */
  73. public Renderer<T> getRenderer() {
  74. if (renderer == null) {
  75. renderer = createRenderer();
  76. }
  77. return renderer;
  78. }
  79. /**
  80. * Creates a new Renderer instance associated with this renderer connector.
  81. * <p>
  82. * You should typically not override this method since the framework by
  83. * default generates an implementation that uses
  84. * {@link com.google.gwt.core.client.GWT#create(Class)} to create a renderer
  85. * of the same type as returned by the most specific override of
  86. * {@link #getRenderer()}. If you do override the method, you can't call
  87. * <code>super.createRenderer()</code> since the metadata needed for that
  88. * implementation is not generated if there's an override of the method.
  89. *
  90. * @return a new renderer to be used with this connector
  91. */
  92. protected Renderer<T> createRenderer() {
  93. // TODO generate type data
  94. Type type = TypeData.getType(getClass());
  95. try {
  96. Type rendererType = type.getMethod("getRenderer").getReturnType();
  97. @SuppressWarnings("unchecked")
  98. Renderer<T> instance = (Renderer<T>) rendererType.createInstance();
  99. return instance;
  100. } catch (NoDataException e) {
  101. throw new IllegalStateException(
  102. "Default implementation of createRenderer() does not work for "
  103. + Util.getSimpleName(this)
  104. + ". This might be caused by explicitely using "
  105. + "super.createRenderer() or some unspecified "
  106. + "problem with the widgetset compilation.", 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 index.
  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 index
  136. * the row index for which to get the row key
  137. * @return the row key for the row at {@code index}
  138. */
  139. protected String getRowKey(int index) {
  140. final ServerConnector parent = getParent();
  141. if (parent instanceof GridConnector) {
  142. return ((GridConnector) parent).getRowKey(index);
  143. } else {
  144. throw new IllegalStateException("Renderers can only be used "
  145. + "with a Grid.");
  146. }
  147. }
  148. }