您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

WidgetSet.java 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * Copyright 2000-2013 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. Class<? extends ServerConnector> classType = resolveInheritedConnectorType(
  48. conf, tag);
  49. if (classType == null || classType == UnknownComponentConnector.class) {
  50. String serverSideName = conf.getUnknownServerClassNameByTag(tag);
  51. UnknownComponentConnector c = GWT
  52. .create(UnknownComponentConnector.class);
  53. c.setServerSideClassName(serverSideName);
  54. return c;
  55. } else {
  56. /*
  57. * let the auto generated code instantiate this type
  58. */
  59. try {
  60. ServerConnector connector = (ServerConnector) TypeData.getType(
  61. classType).createInstance();
  62. if (connector instanceof HasJavaScriptConnectorHelper) {
  63. ((HasJavaScriptConnectorHelper) connector)
  64. .getJavascriptConnectorHelper().setTag(tag);
  65. }
  66. return connector;
  67. } catch (NoDataException e) {
  68. throw new IllegalStateException(
  69. "There is no information about "
  70. + classType
  71. + ". Did you remember to compile the right widgetset?",
  72. e);
  73. }
  74. }
  75. }
  76. private Class<? extends ServerConnector> resolveInheritedConnectorType(
  77. ApplicationConfiguration conf, int tag) {
  78. Class<? extends ServerConnector> classType = null;
  79. Integer t = tag;
  80. do {
  81. classType = resolveConnectorType(t, conf);
  82. t = conf.getParentTag(t);
  83. } while (classType == null && t != null);
  84. return classType;
  85. }
  86. protected Class<? extends ServerConnector> resolveConnectorType(int tag,
  87. ApplicationConfiguration conf) {
  88. Class<? extends ServerConnector> connectorClass = conf
  89. .getConnectorClassByEncodedTag(tag);
  90. return connectorClass;
  91. }
  92. /**
  93. * Due its nature, GWT does not support dynamic classloading. To bypass this
  94. * limitation, widgetset must have function that returns Class by its fully
  95. * qualified name.
  96. *
  97. * @param tag
  98. * @param applicationConfiguration
  99. * @return
  100. */
  101. public void ensureConnectorLoaded(int tag, ApplicationConfiguration conf) {
  102. ConnectorBundleLoader loader = ConnectorBundleLoader.get();
  103. String bundleName = null;
  104. Integer t = tag;
  105. do {
  106. String serverSideClassName = conf.getServerSideClassNameForTag(t);
  107. bundleName = loader.getBundleForIdentifier(serverSideClassName);
  108. t = conf.getParentTag(t);
  109. } while (bundleName == null && t != null);
  110. if (bundleName != null && !loader.isBundleLoaded(bundleName)) {
  111. ApplicationConfiguration.startDependencyLoading();
  112. loader.loadBundle(bundleName, new BundleLoadCallback() {
  113. @Override
  114. public void loaded() {
  115. ApplicationConfiguration.endDependencyLoading();
  116. }
  117. @Override
  118. public void failed(Throwable reason) {
  119. VConsole.error(reason);
  120. ApplicationConfiguration.endDependencyLoading();
  121. }
  122. });
  123. }
  124. }
  125. }