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.

WidgetSet.java 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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;
  17. import java.util.logging.Level;
  18. import java.util.logging.Logger;
  19. import com.google.gwt.core.client.GWT;
  20. import com.vaadin.client.communication.HasJavaScriptConnectorHelper;
  21. import com.vaadin.client.metadata.BundleLoadCallback;
  22. import com.vaadin.client.metadata.ConnectorBundleLoader;
  23. import com.vaadin.client.metadata.NoDataException;
  24. import com.vaadin.client.metadata.TypeData;
  25. import com.vaadin.client.ui.UnknownComponentConnector;
  26. import com.vaadin.client.ui.UnknownExtensionConnector;
  27. import com.vaadin.client.ui.UnknownExtensionConnector;
  28. public class WidgetSet {
  29. /**
  30. * Create an uninitialized connector that best matches given UIDL. The
  31. * connector must implement {@link ServerConnector}.
  32. *
  33. * @param tag
  34. * connector type tag for the connector to create
  35. * @param conf
  36. * the application configuration to use when creating the
  37. * connector
  38. *
  39. * @return New uninitialized and unregistered connector that can paint given
  40. * UIDL.
  41. */
  42. public ServerConnector createConnector(int tag,
  43. ApplicationConfiguration conf) {
  44. /*
  45. * Yes, this (including the generated code in WidgetMap) may look very
  46. * odd code, but due the nature of GWT, we cannot do this any cleaner.
  47. * Luckily this is mostly written by WidgetSetGenerator, here are just
  48. * some hacks. Extra instantiation code is needed if client side
  49. * connector has no "native" counterpart on client side.
  50. */
  51. Profiler.enter("WidgetSet.createConnector");
  52. Class<? extends ServerConnector> classType = resolveInheritedConnectorType(
  53. conf, tag);
  54. try {
  55. if (classType == null
  56. || classType == UnknownComponentConnector.class
  57. || classType == UnknownExtensionConnector.class) {
  58. String serverSideName = conf
  59. .getUnknownServerClassNameByTag(tag);
  60. if (classType == UnknownExtensionConnector.class) {
  61. // Display message in the console for non-visual connectors
  62. getLogger().severe(UnknownComponentConnector
  63. .createMessage(serverSideName));
  64. return GWT.create(UnknownExtensionConnector.class);
  65. } else {
  66. UnknownComponentConnector c = GWT
  67. .create(UnknownComponentConnector.class);
  68. // Set message to be shown in a widget for visual connectors
  69. c.setServerSideClassName(serverSideName);
  70. return c;
  71. }
  72. } else {
  73. /*
  74. * let the auto generated code instantiate this type
  75. */
  76. ServerConnector connector = (ServerConnector) TypeData
  77. .getType(classType).createInstance();
  78. if (connector instanceof HasJavaScriptConnectorHelper) {
  79. ((HasJavaScriptConnectorHelper) connector)
  80. .getJavascriptConnectorHelper().setTag(tag);
  81. }
  82. return connector;
  83. }
  84. } catch (NoDataException e) {
  85. throw new IllegalStateException(
  86. "There is no information about " + classType
  87. + ". Did you remember to compile the right widgetset?",
  88. e);
  89. } finally {
  90. Profiler.leave("WidgetSet.createConnector");
  91. }
  92. }
  93. private Class<? extends ServerConnector> resolveInheritedConnectorType(
  94. ApplicationConfiguration conf, int tag) {
  95. Class<? extends ServerConnector> classType = null;
  96. Integer t = tag;
  97. do {
  98. classType = resolveConnectorType(t, conf);
  99. t = conf.getParentTag(t);
  100. } while (classType == null && t != null);
  101. return classType;
  102. }
  103. protected Class<? extends ServerConnector> resolveConnectorType(int tag,
  104. ApplicationConfiguration conf) {
  105. Class<? extends ServerConnector> connectorClass = conf
  106. .getConnectorClassByEncodedTag(tag);
  107. return connectorClass;
  108. }
  109. /**
  110. * Due its nature, GWT does not support dynamic classloading. To bypass this
  111. * limitation, widgetset must have function that returns Class by its fully
  112. * qualified name.
  113. *
  114. * @param tag
  115. * @param applicationConfiguration
  116. * @return
  117. */
  118. public void ensureConnectorLoaded(int tag, ApplicationConfiguration conf) {
  119. ConnectorBundleLoader loader = ConnectorBundleLoader.get();
  120. String bundleName = null;
  121. Integer t = tag;
  122. String serverSideClassName = "";
  123. do {
  124. serverSideClassName = conf.getServerSideClassNameForTag(t);
  125. bundleName = loader.getBundleForIdentifier(serverSideClassName);
  126. t = conf.getParentTag(t);
  127. } while (bundleName == null && t != null);
  128. if (bundleName != null && !loader.isBundleLoaded(bundleName)) {
  129. getLogger().info("Loading bundle " + bundleName
  130. + " to be able to render server side class "
  131. + serverSideClassName);
  132. ApplicationConfiguration.startDependencyLoading();
  133. loader.loadBundle(bundleName, new BundleLoadCallback() {
  134. @Override
  135. public void loaded() {
  136. ApplicationConfiguration.endDependencyLoading();
  137. }
  138. @Override
  139. public void failed(Throwable reason) {
  140. getLogger().log(Level.SEVERE, "Error loading bundle",
  141. reason);
  142. ApplicationConfiguration.endDependencyLoading();
  143. }
  144. });
  145. }
  146. }
  147. private static Logger getLogger() {
  148. return Logger.getLogger(WidgetSet.class.getName());
  149. }
  150. }