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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * Copyright 2000-2014 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.getType(
  65. 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 "
  76. + classType
  77. + ". Did you remember to compile the right widgetset?",
  78. e);
  79. }
  80. }
  81. }
  82. private Class<? extends ServerConnector> resolveInheritedConnectorType(
  83. ApplicationConfiguration conf, int tag) {
  84. Class<? extends ServerConnector> classType = null;
  85. Integer t = tag;
  86. do {
  87. classType = resolveConnectorType(t, conf);
  88. t = conf.getParentTag(t);
  89. } while (classType == null && t != null);
  90. return classType;
  91. }
  92. protected Class<? extends ServerConnector> resolveConnectorType(int tag,
  93. ApplicationConfiguration conf) {
  94. Class<? extends ServerConnector> connectorClass = conf
  95. .getConnectorClassByEncodedTag(tag);
  96. return connectorClass;
  97. }
  98. /**
  99. * Due its nature, GWT does not support dynamic classloading. To bypass this
  100. * limitation, widgetset must have function that returns Class by its fully
  101. * qualified name.
  102. *
  103. * @param tag
  104. * @param applicationConfiguration
  105. * @return
  106. */
  107. public void ensureConnectorLoaded(int tag, ApplicationConfiguration conf) {
  108. ConnectorBundleLoader loader = ConnectorBundleLoader.get();
  109. String bundleName = null;
  110. Integer t = tag;
  111. do {
  112. String 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. ApplicationConfiguration.startDependencyLoading();
  118. loader.loadBundle(bundleName, new BundleLoadCallback() {
  119. @Override
  120. public void loaded() {
  121. ApplicationConfiguration.endDependencyLoading();
  122. }
  123. @Override
  124. public void failed(Throwable reason) {
  125. getLogger().log(Level.SEVERE, "Error loading bundle",
  126. reason);
  127. ApplicationConfiguration.endDependencyLoading();
  128. }
  129. });
  130. }
  131. }
  132. public static Logger getLogger() {
  133. return Logger.getLogger(WidgetSet.class.getName());
  134. }
  135. }