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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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.metadata.BundleLoadCallback;
  21. import com.vaadin.client.metadata.ConnectorBundleLoader;
  22. import com.vaadin.client.metadata.NoDataException;
  23. import com.vaadin.client.metadata.TypeData;
  24. import com.vaadin.client.ui.UnknownComponentConnector;
  25. import com.vaadin.client.ui.UnknownExtensionConnector;
  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. try {
  53. if (classType == null
  54. || classType == UnknownComponentConnector.class
  55. || classType == UnknownExtensionConnector.class) {
  56. String serverSideName = conf
  57. .getUnknownServerClassNameByTag(tag);
  58. if (classType == UnknownExtensionConnector.class) {
  59. // Display message in the console for non-visual connectors
  60. getLogger().severe(UnknownComponentConnector
  61. .createMessage(serverSideName));
  62. return GWT.create(UnknownExtensionConnector.class);
  63. } else {
  64. UnknownComponentConnector c = GWT
  65. .create(UnknownComponentConnector.class);
  66. // Set message to be shown in a widget for visual connectors
  67. c.setServerSideClassName(serverSideName);
  68. return c;
  69. }
  70. } else {
  71. /*
  72. * let the auto generated code instantiate this type
  73. */
  74. ServerConnector connector = (ServerConnector) TypeData
  75. .getType(classType).createInstance();
  76. connector.setTag(tag);
  77. return connector;
  78. }
  79. } catch (NoDataException e) {
  80. throw new IllegalStateException(
  81. "There is no information about " + classType
  82. + ". Did you remember to compile the right widgetset?",
  83. e);
  84. } finally {
  85. Profiler.leave("WidgetSet.createConnector");
  86. }
  87. }
  88. private Class<? extends ServerConnector> resolveInheritedConnectorType(
  89. ApplicationConfiguration conf, int tag) {
  90. Class<? extends ServerConnector> classType = null;
  91. Integer t = tag;
  92. do {
  93. classType = resolveConnectorType(t, conf);
  94. t = conf.getParentTag(t);
  95. } while (classType == null && t != null);
  96. return classType;
  97. }
  98. protected Class<? extends ServerConnector> resolveConnectorType(int tag,
  99. ApplicationConfiguration conf) {
  100. Class<? extends ServerConnector> connectorClass = conf
  101. .getConnectorClassByEncodedTag(tag);
  102. return connectorClass;
  103. }
  104. /**
  105. * Due its nature, GWT does not support dynamic classloading. To bypass this
  106. * limitation, widgetset must have function that returns Class by its fully
  107. * qualified name.
  108. *
  109. * @param tag
  110. * @param applicationConfiguration
  111. * @return
  112. */
  113. public void ensureConnectorLoaded(int tag, ApplicationConfiguration conf) {
  114. ConnectorBundleLoader loader = ConnectorBundleLoader.get();
  115. String bundleName = null;
  116. Integer t = tag;
  117. String serverSideClassName = "";
  118. do {
  119. serverSideClassName = conf.getServerSideClassNameForTag(t);
  120. bundleName = loader.getBundleForIdentifier(serverSideClassName);
  121. t = conf.getParentTag(t);
  122. } while (bundleName == null && t != null);
  123. if (bundleName != null && !loader.isBundleLoaded(bundleName)) {
  124. getLogger().info("Loading bundle " + bundleName
  125. + " to be able to render server side class "
  126. + serverSideClassName);
  127. ApplicationConfiguration.startDependencyLoading();
  128. loader.loadBundle(bundleName, new BundleLoadCallback() {
  129. @Override
  130. public void loaded() {
  131. ApplicationConfiguration.endDependencyLoading();
  132. }
  133. @Override
  134. public void failed(Throwable reason) {
  135. getLogger().log(Level.SEVERE, "Error loading bundle",
  136. reason);
  137. ApplicationConfiguration.endDependencyLoading();
  138. }
  139. });
  140. }
  141. }
  142. private static Logger getLogger() {
  143. return Logger.getLogger(WidgetSet.class.getName());
  144. }
  145. }