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.

CustomComponent.java 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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.ui;
  17. import java.util.Collections;
  18. import java.util.Iterator;
  19. import com.vaadin.shared.customcomponent.CustomComponentState;
  20. /**
  21. * Custom component provides a simple implementation of the {@link Component}
  22. * interface to allow creating new UI components by composition of existing
  23. * server-side components.
  24. *
  25. * <p>
  26. * The component is used by inheriting the CustomComponent class and setting the
  27. * composition root component. The composition root must be set with
  28. * {@link #setCompositionRoot(Component)} before the CustomComponent is used,
  29. * such as by adding it to a layout, so it is preferable to set it in the
  30. * constructor.
  31. * </p>
  32. *
  33. * <p>
  34. * The composition root itself can contain more components. The advantage of
  35. * wrapping it in a CustomComponent is that its details, such as interfaces, are
  36. * hidden from the users of the component, thereby contributing to information
  37. * hiding.
  38. * </p>
  39. *
  40. * <p>
  41. * The CustomComponent does not display the caption of the composition root, so
  42. * if you want to have it shown in the layout where the custom component is
  43. * contained, you need to set it as caption of the CustomComponent.
  44. * </p>
  45. *
  46. * <p>
  47. * The component expands horizontally and has undefined height by default.
  48. * </p>
  49. *
  50. * @author Vaadin Ltd.
  51. * @since 3.0
  52. */
  53. @SuppressWarnings("serial")
  54. public class CustomComponent extends AbstractComponent
  55. implements HasComponents {
  56. /**
  57. * The root component implementing the custom component.
  58. */
  59. private Component root = null;
  60. /**
  61. * Constructs a new custom component.
  62. *
  63. * <p>
  64. * Note that you must set the composition root before the component can be
  65. * used, preferably in the constructor.
  66. * </p>
  67. */
  68. public CustomComponent() {
  69. // Expand horizontally by default
  70. setWidth(100, Unit.PERCENTAGE);
  71. }
  72. /**
  73. * Constructs a new custom component.
  74. *
  75. * @param compositionRoot
  76. * the root of the composition component tree. It must not be
  77. * null.
  78. */
  79. public CustomComponent(Component compositionRoot) {
  80. this();
  81. setCompositionRoot(compositionRoot);
  82. }
  83. /**
  84. * Returns the composition root.
  85. *
  86. * @return the Component Composition root.
  87. */
  88. protected Component getCompositionRoot() {
  89. return root;
  90. }
  91. /**
  92. * Sets the composition root for the component.
  93. *
  94. * <p>
  95. * You must set the composition root to a non-null value before the
  96. * component can be used. You can change it later.
  97. * </p>
  98. *
  99. * @param compositionRoot
  100. * the root of the composition component tree.
  101. */
  102. protected void setCompositionRoot(Component compositionRoot) {
  103. if (compositionRoot != root) {
  104. if (root != null && equals(root.getParent())) {
  105. // remove old component
  106. root.setParent(null);
  107. }
  108. if (compositionRoot != null) {
  109. // set new component
  110. if (compositionRoot.getParent() != null) {
  111. // If the component already has a parent, try to remove it
  112. AbstractSingleComponentContainer
  113. .removeFromParent(compositionRoot);
  114. }
  115. compositionRoot.setParent(this);
  116. }
  117. root = compositionRoot;
  118. markAsDirty();
  119. }
  120. }
  121. /* Basic component features ------------------------------------------ */
  122. @Override
  123. public Iterator<Component> iterator() {
  124. if (getCompositionRoot() != null) {
  125. return Collections.singletonList(getCompositionRoot()).iterator();
  126. } else {
  127. return Collections.<Component> emptyList().iterator();
  128. }
  129. }
  130. /**
  131. * Gets the number of contained components.
  132. *
  133. * @return the number of contained components (zero or one)
  134. */
  135. public int getComponentCount() {
  136. return (root != null ? 1 : 0);
  137. }
  138. @Override
  139. protected CustomComponentState getState() {
  140. return (CustomComponentState) super.getState();
  141. }
  142. @Override
  143. protected CustomComponentState getState(boolean markAsDirty) {
  144. return (CustomComponentState) super.getState(markAsDirty);
  145. }
  146. }