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.

HasComponents.java 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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.io.Serializable;
  18. import java.lang.reflect.Method;
  19. import java.util.Iterator;
  20. import com.vaadin.shared.Registration;
  21. import com.vaadin.util.ReflectTools;
  22. /**
  23. * Interface that must be implemented by all {@link Component}s that contain
  24. * other {@link Component}s.
  25. *
  26. * @author Vaadin Ltd
  27. * @since 7.0.0
  28. *
  29. */
  30. public interface HasComponents extends Component, Iterable<Component> {
  31. /**
  32. * Gets an iterator to the collection of contained components. Using this
  33. * iterator it is possible to step through all components contained in this
  34. * container.
  35. * <p>
  36. * The iterator is typically unmodifiable, and calls to
  37. * {@link Iterator#remove()} throw an exception.
  38. *
  39. * @return the component iterator.
  40. */
  41. @Override
  42. public Iterator<Component> iterator();
  43. /**
  44. * Interface for {@link HasComponents} implementations that support sending
  45. * attach and detach events for components.
  46. *
  47. * @since 7.0
  48. */
  49. public interface ComponentAttachDetachNotifier extends Serializable {
  50. /**
  51. * Listens the component attach events.
  52. *
  53. * @see Registration
  54. *
  55. * @param listener
  56. * the listener to add, not null
  57. * @return a registration object for removing the listener
  58. * @since 8.0
  59. */
  60. public Registration addComponentAttachListener(
  61. ComponentAttachListener listener);
  62. /**
  63. * Stops the listening component attach events.
  64. *
  65. * @param listener
  66. * the listener to removed.
  67. *
  68. * @deprecated As of 8.0, replaced by {@link Registration#remove()} in
  69. * the registration object returned from
  70. * {@link #addComponentAttachListener(ComponentAttachListener)}
  71. * .
  72. * @since 8.0
  73. */
  74. @Deprecated
  75. public void removeComponentAttachListener(
  76. ComponentAttachListener listener);
  77. /**
  78. * Listens the component detach events.
  79. */
  80. public Registration addComponentDetachListener(
  81. ComponentDetachListener listener);
  82. /**
  83. * Stops the listening component detach events.
  84. */
  85. @Deprecated
  86. public void removeComponentDetachListener(
  87. ComponentDetachListener listener);
  88. }
  89. /**
  90. * Component attach listener interface.
  91. */
  92. @FunctionalInterface
  93. public interface ComponentAttachListener extends Serializable {
  94. public static final Method attachMethod = ReflectTools.findMethod(
  95. ComponentAttachListener.class, "componentAttachedToContainer",
  96. ComponentAttachEvent.class);
  97. /**
  98. * A new component is attached to container.
  99. *
  100. * @param event
  101. * the component attach event.
  102. */
  103. public void componentAttachedToContainer(ComponentAttachEvent event);
  104. }
  105. /**
  106. * Component detach listener interface.
  107. */
  108. @FunctionalInterface
  109. public interface ComponentDetachListener extends Serializable {
  110. public static final Method detachMethod = ReflectTools.findMethod(
  111. ComponentDetachListener.class, "componentDetachedFromContainer",
  112. ComponentDetachEvent.class);
  113. /**
  114. * A component has been detached from container.
  115. *
  116. * @param event
  117. * the component detach event.
  118. */
  119. public void componentDetachedFromContainer(ComponentDetachEvent event);
  120. }
  121. /**
  122. * Component attach event sent when a component is attached to container.
  123. */
  124. @SuppressWarnings("serial")
  125. public static class ComponentAttachEvent extends Component.Event {
  126. private final Component component;
  127. /**
  128. * Creates a new attach event.
  129. *
  130. * @param container
  131. * the container the component has been detached to.
  132. * @param attachedComponent
  133. * the component that has been attached.
  134. */
  135. public ComponentAttachEvent(HasComponents container,
  136. Component attachedComponent) {
  137. super(container);
  138. component = attachedComponent;
  139. }
  140. /**
  141. * Gets the component container.
  142. *
  143. */
  144. public HasComponents getContainer() {
  145. return (HasComponents) getSource();
  146. }
  147. /**
  148. * Gets the attached component.
  149. *
  150. */
  151. public Component getAttachedComponent() {
  152. return component;
  153. }
  154. }
  155. /**
  156. * Component detach event sent when a component is detached from container.
  157. */
  158. @SuppressWarnings("serial")
  159. public static class ComponentDetachEvent extends Component.Event {
  160. private final Component component;
  161. /**
  162. * Creates a new detach event.
  163. *
  164. * @param container
  165. * the container the component has been detached from.
  166. * @param detachedComponent
  167. * the component that has been detached.
  168. */
  169. public ComponentDetachEvent(HasComponents container,
  170. Component detachedComponent) {
  171. super(container);
  172. component = detachedComponent;
  173. }
  174. /**
  175. * Gets the component container.
  176. *
  177. */
  178. public HasComponents getContainer() {
  179. return (HasComponents) getSource();
  180. }
  181. /**
  182. * Gets the detached component.
  183. *
  184. * @return the detached component.
  185. */
  186. public Component getDetachedComponent() {
  187. return component;
  188. }
  189. }
  190. }