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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * Copyright 2011 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.terminal.gwt.client;
  17. import com.google.gwt.core.client.GWT;
  18. import com.vaadin.terminal.gwt.client.communication.HasJavaScriptConnectorHelper;
  19. import com.vaadin.terminal.gwt.client.ui.UnknownComponentConnector;
  20. public class WidgetSet {
  21. /**
  22. * WidgetSet (and its extensions) delegate instantiation of widgets and
  23. * client-server matching to WidgetMap. The actual implementations are
  24. * generated with gwts generators/deferred binding.
  25. */
  26. private WidgetMap widgetMap = GWT.create(WidgetMap.class);
  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. Class<? extends ServerConnector> classType = resolveInheritedConnectorType(
  50. conf, tag);
  51. if (classType == null || classType == UnknownComponentConnector.class) {
  52. String serverSideName = conf.getUnknownServerClassNameByTag(tag);
  53. UnknownComponentConnector c = GWT
  54. .create(UnknownComponentConnector.class);
  55. c.setServerSideClassName(serverSideName);
  56. return c;
  57. } else {
  58. /*
  59. * let the auto generated code instantiate this type
  60. */
  61. ServerConnector connector = widgetMap.instantiate(classType);
  62. if (connector instanceof HasJavaScriptConnectorHelper) {
  63. ((HasJavaScriptConnectorHelper) connector)
  64. .getJavascriptConnectorHelper().setTag(tag);
  65. }
  66. return connector;
  67. }
  68. }
  69. private Class<? extends ServerConnector> resolveInheritedConnectorType(
  70. ApplicationConfiguration conf, int tag) {
  71. Class<? extends ServerConnector> classType = null;
  72. Integer t = tag;
  73. do {
  74. classType = resolveConnectorType(t, conf);
  75. t = conf.getParentTag(t);
  76. } while (classType == null && t != null);
  77. return classType;
  78. }
  79. protected Class<? extends ServerConnector> resolveConnectorType(int tag,
  80. ApplicationConfiguration conf) {
  81. Class<? extends ServerConnector> connectorClass = conf
  82. .getConnectorClassByEncodedTag(tag);
  83. return connectorClass;
  84. }
  85. /**
  86. * Due its nature, GWT does not support dynamic classloading. To bypass this
  87. * limitation, widgetset must have function that returns Class by its fully
  88. * qualified name.
  89. *
  90. * @param tag
  91. * @param applicationConfiguration
  92. * @return
  93. */
  94. public Class<? extends ServerConnector> getConnectorClassByTag(int tag,
  95. ApplicationConfiguration conf) {
  96. Class<? extends ServerConnector> connectorClass = null;
  97. Integer t = tag;
  98. do {
  99. String serverSideClassName = conf.getServerSideClassNameForTag(t);
  100. connectorClass = widgetMap
  101. .getConnectorClassForServerSideClassName(serverSideClassName);
  102. t = conf.getParentTag(t);
  103. } while (connectorClass == UnknownComponentConnector.class && t != null);
  104. return connectorClass;
  105. }
  106. public Class<? extends ServerConnector>[] getDeferredLoadedConnectors() {
  107. return widgetMap.getDeferredLoadedConnectors();
  108. }
  109. public void loadImplementation(Class<? extends ServerConnector> nextType) {
  110. widgetMap.ensureInstantiator(nextType);
  111. }
  112. }