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.8KB

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