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

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