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

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