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

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