Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

AbstractComponentContainer.java 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /* *************************************************************************
  2. IT Mill Toolkit
  3. Development of Browser User Interfaces Made Easy
  4. Copyright (C) 2000-2006 IT Mill Ltd
  5. *************************************************************************
  6. This product is distributed under commercial license that can be found
  7. from the product package on license.pdf. Use of this product might
  8. require purchasing a commercial license from IT Mill Ltd. For guidelines
  9. on usage, see licensing-guidelines.html
  10. *************************************************************************
  11. For more information, contact:
  12. IT Mill Ltd phone: +358 2 4802 7180
  13. Ruukinkatu 2-4 fax: +358 2 4802 7181
  14. 20540, Turku email: info@itmill.com
  15. Finland company www: www.itmill.com
  16. Primary source for information and releases: www.itmill.com
  17. ********************************************************************** */
  18. package com.itmill.toolkit.ui;
  19. import java.lang.reflect.Method;
  20. import java.util.LinkedList;
  21. import java.util.Iterator;
  22. /** Extension to {@link AbstractComponent} that defines the default
  23. * implementation for the methods in {@link ComponentContainer}. Basic UI
  24. * components that need to contain other components inherit this class to
  25. * easily qualify as a component container.
  26. *
  27. * @author IT Mill Ltd
  28. * @version @VERSION@
  29. * @since 3.0
  30. */
  31. public abstract class AbstractComponentContainer
  32. extends AbstractComponent implements ComponentContainer {
  33. /** Constructs a new component container. */
  34. public AbstractComponentContainer() {
  35. super();
  36. }
  37. /** Removes all components from the container. This should probably be
  38. * reimplemented in extending classes for a more powerfu
  39. * implementation.
  40. */
  41. public void removeAllComponents() {
  42. LinkedList l = new LinkedList();
  43. // Add all components
  44. for (Iterator i = getComponentIterator(); i.hasNext();)
  45. l.add(i.next());
  46. // Remove all component
  47. for (Iterator i = l.iterator(); i.hasNext();)
  48. removeComponent((Component)i.next());
  49. }
  50. /* Moves all components from an another container into this container.
  51. * Don't add a JavaDoc comment here, we use the default documentation
  52. * from implemented interface.
  53. */
  54. public void moveComponentsFrom(ComponentContainer source) {
  55. LinkedList components = new LinkedList();
  56. for (Iterator i = source.getComponentIterator(); i.hasNext();)
  57. components.add(i.next());
  58. for (Iterator i = components.iterator(); i.hasNext();) {
  59. Component c = (Component) i.next();
  60. source.removeComponent(c);
  61. addComponent(c);
  62. }
  63. }
  64. /** Notifies all contained components that the container is attached to
  65. * a window.
  66. *
  67. * @see com.itmill.toolkit.ui.Component#attach()
  68. */
  69. public void attach() {
  70. super.attach();
  71. for (Iterator i = getComponentIterator(); i.hasNext();)
  72. ((Component)i.next()).attach();
  73. }
  74. /** Notifies all contained components that the container is detached
  75. * from a window.
  76. *
  77. * @see com.itmill.toolkit.ui.Component#detach()
  78. */
  79. public void detach() {
  80. super.detach();
  81. for (Iterator i = getComponentIterator(); i.hasNext();)
  82. ((Component)i.next()).detach();
  83. }
  84. /* Events ************************************************************ */
  85. private static final Method COMPONENT_ATTACHED_METHOD;
  86. private static final Method COMPONENT_DETACHED_METHOD;
  87. static {
  88. try {
  89. COMPONENT_ATTACHED_METHOD =
  90. ComponentAttachListener.class.getDeclaredMethod(
  91. "componentAttachedToContainer",
  92. new Class[] { ComponentAttachEvent.class });
  93. COMPONENT_DETACHED_METHOD =
  94. ComponentDetachListener.class.getDeclaredMethod(
  95. "componentDetachedFromContainer",
  96. new Class[] { ComponentDetachEvent.class });
  97. } catch (java.lang.NoSuchMethodException e) {
  98. // This should never happen
  99. throw new java.lang.RuntimeException();
  100. }
  101. }
  102. /* documented in interface */
  103. public void addListener(ComponentAttachListener listener) {
  104. addListener(ComponentContainer.ComponentAttachEvent.class, listener, COMPONENT_ATTACHED_METHOD);
  105. }
  106. /* documented in interface */
  107. public void addListener(ComponentDetachListener listener) {
  108. addListener(ComponentContainer.ComponentDetachEvent.class, listener, COMPONENT_DETACHED_METHOD);
  109. }
  110. /* documented in interface */
  111. public void removeListener(ComponentAttachListener listener) {
  112. removeListener(ComponentContainer.ComponentAttachEvent.class, listener, COMPONENT_ATTACHED_METHOD);
  113. }
  114. /* documented in interface */
  115. public void removeListener(ComponentDetachListener listener) {
  116. removeListener(ComponentContainer.ComponentDetachEvent.class, listener, COMPONENT_DETACHED_METHOD);
  117. }
  118. /** Fire component attached event. This should be called by the addComponent
  119. * methods after the component have been added to this container.
  120. * @param component The component that has been added to this container.
  121. */
  122. protected void fireComponentAttachEvent(Component component) {
  123. fireEvent(new ComponentAttachEvent(this,component));
  124. }
  125. /** Fire component detached event. This should be called by the removeComponent
  126. * methods after the component have been removed from this container.
  127. * @param component The component that has been removed from this container.
  128. */
  129. protected void fireComponentDetachEvent(Component component) {
  130. fireEvent(new ComponentAttachEvent(this,component));
  131. }
  132. /** This only implements the events and component parent calls. The extending
  133. * classes must implement component list maintenance and call this method
  134. * after component list maintenance.
  135. * @see com.itmill.toolkit.ui.ComponentContainer#addComponent(Component)
  136. */
  137. public void addComponent(Component c) {
  138. c.setParent(this);
  139. fireComponentAttachEvent(c);
  140. }
  141. /** This only implements the events and component parent calls. The extending
  142. * classes must implement component list maintenance and call this method
  143. * before component list maintenance.
  144. * @see com.itmill.toolkit.ui.ComponentContainer#removeComponent(Component)
  145. */
  146. public void removeComponent(Component c) {
  147. c.setParent(null);
  148. fireComponentDetachEvent(c);
  149. }
  150. }