Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

CustomLayoutConnector.java 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * Copyright 2000-2021 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.ui.customlayout;
  17. import java.util.logging.Logger;
  18. import com.google.gwt.user.client.DOM;
  19. import com.google.gwt.user.client.ui.Widget;
  20. import com.vaadin.client.ApplicationConnection;
  21. import com.vaadin.client.ComponentConnector;
  22. import com.vaadin.client.ConnectorHierarchyChangeEvent;
  23. import com.vaadin.client.Paintable;
  24. import com.vaadin.client.UIDL;
  25. import com.vaadin.client.communication.StateChangeEvent;
  26. import com.vaadin.client.ui.AbstractLayoutConnector;
  27. import com.vaadin.client.ui.SimpleManagedLayout;
  28. import com.vaadin.client.ui.VCustomLayout;
  29. import com.vaadin.shared.ui.Connect;
  30. import com.vaadin.shared.ui.customlayout.CustomLayoutState;
  31. import com.vaadin.ui.CustomLayout;
  32. /**
  33. * A connector class for CustomLayout.
  34. *
  35. * @author Vaadin Ltd
  36. */
  37. @SuppressWarnings("deprecation")
  38. @Connect(CustomLayout.class)
  39. public class CustomLayoutConnector extends AbstractLayoutConnector
  40. implements SimpleManagedLayout, Paintable {
  41. private boolean templateUpdated;
  42. @Override
  43. public CustomLayoutState getState() {
  44. return (CustomLayoutState) super.getState();
  45. }
  46. @Override
  47. protected void init() {
  48. super.init();
  49. getWidget().client = getConnection();
  50. getWidget().pid = getConnectorId();
  51. }
  52. @Override
  53. public void onStateChanged(StateChangeEvent stateChangeEvent) {
  54. super.onStateChanged(stateChangeEvent);
  55. // Ensure the template is initialized even if there are no children
  56. // (#9725)
  57. updateHtmlTemplate();
  58. // Evaluate scripts
  59. VCustomLayout.eval(getWidget().scripts);
  60. getWidget().scripts = null;
  61. }
  62. private void updateHtmlTemplate() {
  63. if (templateUpdated) {
  64. // We (currently) only do this once. You can't change the template
  65. // later on.
  66. return;
  67. }
  68. String templateName = getState().templateName;
  69. String templateContents = getState().templateContents;
  70. if (templateName != null) {
  71. // Get the HTML-template from client. Overrides templateContents
  72. // (even though both can never be given at the same time)
  73. templateContents = getConnection()
  74. .getResource("layouts/" + templateName + ".html");
  75. }
  76. if (templateContents != null) {
  77. // Template ok -> initialize.
  78. getWidget().initializeHTML(templateContents,
  79. getConnection().getThemeUri());
  80. } else {
  81. // Template missing -> show debug notice and render components in
  82. // order.
  83. String warning = templateName != null
  84. ? "Layout file layouts/" + templateName
  85. + ".html is missing."
  86. : "Layout file not specified.";
  87. getWidget().getElement().setInnerHTML("<em>" + warning
  88. + " Components will be drawn for debug purposes.</em>");
  89. }
  90. templateUpdated = true;
  91. }
  92. @Override
  93. public void onConnectorHierarchyChange(
  94. ConnectorHierarchyChangeEvent event) {
  95. // Must call here in addition to onStateChanged because
  96. // onConnectorHierarchyChange is invoked before onStateChanged
  97. updateHtmlTemplate();
  98. // For all contained widgets
  99. for (ComponentConnector child : getChildComponents()) {
  100. String location = getState().childLocations.get(child);
  101. try {
  102. getWidget().setWidget(child.getWidget(), location);
  103. } catch (final IllegalArgumentException e) {
  104. // If no location is found, this component is not visible
  105. getLogger().warning("Child not rendered as no slot with id '"
  106. + location + "' has been defined");
  107. }
  108. }
  109. for (ComponentConnector oldChild : event.getOldChildren()) {
  110. if (oldChild.getParent() == this) {
  111. // Connector still a child of this
  112. continue;
  113. }
  114. Widget oldChildWidget = oldChild.getWidget();
  115. if (oldChildWidget.isAttached()) {
  116. // slot of this widget is emptied, remove it
  117. getWidget().remove(oldChildWidget);
  118. }
  119. }
  120. }
  121. @Override
  122. public VCustomLayout getWidget() {
  123. return (VCustomLayout) super.getWidget();
  124. }
  125. @Override
  126. public void updateCaption(ComponentConnector paintable) {
  127. getWidget().updateCaption(paintable);
  128. }
  129. @Override
  130. public void layout() {
  131. getWidget().iLayoutJS(DOM.getFirstChild(getWidget().getElement()));
  132. }
  133. @Override
  134. public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
  135. // Not interested in anything from the UIDL - just implementing the
  136. // interface to avoid some warning (#8688)
  137. }
  138. private static Logger getLogger() {
  139. return Logger.getLogger(CustomLayoutConnector.class.getName());
  140. }
  141. }