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.

CompositeConnector.java 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * Copyright 2000-2018 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.composite;
  17. import com.google.gwt.dom.client.EventTarget;
  18. import com.google.gwt.user.client.ui.Label;
  19. import com.google.gwt.user.client.ui.Widget;
  20. import com.vaadin.client.ComponentConnector;
  21. import com.vaadin.client.ConnectorHierarchyChangeEvent;
  22. import com.vaadin.client.DirectionalManagedLayout;
  23. import com.vaadin.client.HasComponentsConnector;
  24. import com.vaadin.client.ui.AbstractHasComponentsConnector;
  25. import com.vaadin.client.ui.SimpleManagedLayout;
  26. import com.vaadin.shared.AbstractComponentState;
  27. import com.vaadin.shared.MouseEventDetails;
  28. import com.vaadin.shared.ui.Connect;
  29. import com.vaadin.shared.ui.Connect.LoadStyle;
  30. import com.vaadin.ui.Composite;
  31. /**
  32. * Connector for the Composite component.
  33. *
  34. * @author Vaadin Ltd
  35. * @since 8.1
  36. */
  37. @Connect(value = Composite.class, loadStyle = LoadStyle.EAGER)
  38. public class CompositeConnector extends AbstractHasComponentsConnector
  39. implements SimpleManagedLayout {
  40. private ComponentConnector childConnector;
  41. @Override
  42. protected Widget createWidget() {
  43. throw new UnsupportedOperationException(
  44. "Composite has no widget of its own");
  45. }
  46. private boolean hasChildConnector() {
  47. return getChildConnector() != null;
  48. }
  49. private ComponentConnector getChildConnector() {
  50. // Must store the child connector to have it available when removing the
  51. // connector
  52. if (childConnector == null && !getChildren().isEmpty()) {
  53. childConnector = (ComponentConnector) getChildren().get(0);
  54. }
  55. return childConnector;
  56. }
  57. @Override
  58. public Widget getWidget() {
  59. if (!hasChildConnector()) {
  60. // This happens in doInit for instance when setConnectorId is called
  61. return new Label("This widget should not end up anywhere ever");
  62. } else {
  63. return getChildConnector().getWidget();
  64. }
  65. }
  66. @Override
  67. public HasComponentsConnector getParent() {
  68. return (HasComponentsConnector) super.getParent();
  69. }
  70. @Override
  71. public void updateCaption(ComponentConnector component) {
  72. // Parent might assume that the connector is always a child connector,
  73. // therefore passing "this" instead of the child connector. The child
  74. // caption will be returned as getState() returns the child's state.
  75. getParent().updateCaption(this);
  76. }
  77. @Override
  78. public AbstractComponentState getState() {
  79. if (!hasChildConnector()) {
  80. return new AbstractComponentState();
  81. } else {
  82. return getChildConnector().getState();
  83. }
  84. }
  85. @Override
  86. public void onConnectorHierarchyChange(
  87. ConnectorHierarchyChangeEvent event) {
  88. // Handled in getChildConnector
  89. }
  90. @Override
  91. protected void sendContextClickEvent(MouseEventDetails details,
  92. EventTarget eventTarget) {
  93. // Do nothing, because Composite is not an actual component, and the
  94. // event must be handled in inner components.
  95. }
  96. @Override
  97. public void layout() {
  98. // Pass on the layout to the appropriate method in child connector
  99. if (childConnector instanceof SimpleManagedLayout) {
  100. ((SimpleManagedLayout) childConnector).layout();
  101. } else if (childConnector instanceof DirectionalManagedLayout) {
  102. ((DirectionalManagedLayout) childConnector).layoutHorizontally();
  103. ((DirectionalManagedLayout) childConnector).layoutVertically();
  104. } else {
  105. getLayoutManager().setNeedsMeasureRecursively(childConnector);
  106. }
  107. }
  108. @Override
  109. public boolean delegateCaptionHandling() {
  110. if (!hasChildConnector()) {
  111. return true;
  112. } else {
  113. return getChildConnector().delegateCaptionHandling();
  114. }
  115. }
  116. }