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.

AbstractContainer.java 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. @VaadinApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.data.util;
  5. import java.io.Serializable;
  6. import java.util.Collection;
  7. import java.util.Collections;
  8. import java.util.EventObject;
  9. import java.util.LinkedList;
  10. import com.vaadin.data.Container;
  11. /**
  12. * Abstract container class that manages event listeners and sending events to
  13. * them ({@link PropertySetChangeNotifier}, {@link ItemSetChangeNotifier}).
  14. *
  15. * Note that this class provides the internal implementations for both types of
  16. * events and notifiers as protected methods, but does not implement the
  17. * {@link PropertySetChangeNotifier} and {@link ItemSetChangeNotifier}
  18. * interfaces directly. This way, subclasses can choose not to implement them.
  19. * Subclasses implementing those interfaces should also override the
  20. * corresponding {@link #addListener()} and {@link #removeListener()} methods to
  21. * make them public.
  22. *
  23. * @since 6.6
  24. */
  25. public abstract class AbstractContainer implements Container {
  26. /**
  27. * List of all Property set change event listeners.
  28. */
  29. private Collection<Container.PropertySetChangeListener> propertySetChangeListeners = null;
  30. /**
  31. * List of all container Item set change event listeners.
  32. */
  33. private Collection<Container.ItemSetChangeListener> itemSetChangeListeners = null;
  34. /**
  35. * An <code>event</code> object specifying the container whose Property set
  36. * has changed.
  37. *
  38. * This class does not provide information about which properties were
  39. * concerned by the change, but subclasses can provide additional
  40. * information about the changes.
  41. */
  42. protected static class BasePropertySetChangeEvent extends EventObject
  43. implements Container.PropertySetChangeEvent, Serializable {
  44. protected BasePropertySetChangeEvent(Container source) {
  45. super(source);
  46. }
  47. public Container getContainer() {
  48. return (Container) getSource();
  49. }
  50. }
  51. /**
  52. * An <code>event</code> object specifying the container whose Item set has
  53. * changed.
  54. *
  55. * This class does not provide information about the exact changes
  56. * performed, but subclasses can add provide additional information about
  57. * the changes.
  58. */
  59. protected static class BaseItemSetChangeEvent extends EventObject implements
  60. Container.ItemSetChangeEvent, Serializable {
  61. protected BaseItemSetChangeEvent(Container source) {
  62. super(source);
  63. }
  64. public Container getContainer() {
  65. return (Container) getSource();
  66. }
  67. }
  68. // PropertySetChangeNotifier
  69. /**
  70. * Implementation of the corresponding method in
  71. * {@link PropertySetChangeNotifier}, override with the corresponding public
  72. * method and implement the interface to use this.
  73. *
  74. * @see PropertySetChangeNotifier#addListener(com.vaadin.data.Container.PropertySetChangeListener)
  75. */
  76. protected void addListener(Container.PropertySetChangeListener listener) {
  77. if (getPropertySetChangeListeners() == null) {
  78. setPropertySetChangeListeners(new LinkedList<Container.PropertySetChangeListener>());
  79. }
  80. getPropertySetChangeListeners().add(listener);
  81. }
  82. /**
  83. * Implementation of the corresponding method in
  84. * {@link PropertySetChangeNotifier}, override with the corresponding public
  85. * method and implement the interface to use this.
  86. *
  87. * @see PropertySetChangeNotifier#removeListener(com.vaadin.data.Container.
  88. * PropertySetChangeListener)
  89. */
  90. protected void removeListener(Container.PropertySetChangeListener listener) {
  91. if (getPropertySetChangeListeners() != null) {
  92. getPropertySetChangeListeners().remove(listener);
  93. }
  94. }
  95. // ItemSetChangeNotifier
  96. /**
  97. * Implementation of the corresponding method in
  98. * {@link ItemSetChangeNotifier}, override with the corresponding public
  99. * method and implement the interface to use this.
  100. *
  101. * @see ItemSetChangeNotifier#addListener(com.vaadin.data.Container.ItemSetChangeListener)
  102. */
  103. protected void addListener(Container.ItemSetChangeListener listener) {
  104. if (getItemSetChangeListeners() == null) {
  105. setItemSetChangeListeners(new LinkedList<Container.ItemSetChangeListener>());
  106. }
  107. getItemSetChangeListeners().add(listener);
  108. }
  109. /**
  110. * Implementation of the corresponding method in
  111. * {@link ItemSetChangeNotifier}, override with the corresponding public
  112. * method and implement the interface to use this.
  113. *
  114. * @see ItemSetChangeNotifier#removeListener(com.vaadin.data.Container.ItemSetChangeListener)
  115. */
  116. protected void removeListener(Container.ItemSetChangeListener listener) {
  117. if (getItemSetChangeListeners() != null) {
  118. getItemSetChangeListeners().remove(listener);
  119. }
  120. }
  121. /**
  122. * Sends a simple Property set change event to all interested listeners.
  123. */
  124. protected void fireContainerPropertySetChange() {
  125. fireContainerPropertySetChange(new BasePropertySetChangeEvent(this));
  126. }
  127. /**
  128. * Sends a Property set change event to all interested listeners.
  129. *
  130. * Use {@link #fireContainerPropertySetChange()} instead of this method
  131. * unless additional information about the exact changes is available and
  132. * should be included in the event.
  133. *
  134. * @param event
  135. * the property change event to send, optionally with additional
  136. * information
  137. */
  138. protected void fireContainerPropertySetChange(
  139. Container.PropertySetChangeEvent event) {
  140. if (getPropertySetChangeListeners() != null) {
  141. final Object[] l = getPropertySetChangeListeners().toArray();
  142. for (int i = 0; i < l.length; i++) {
  143. ((Container.PropertySetChangeListener) l[i])
  144. .containerPropertySetChange(event);
  145. }
  146. }
  147. }
  148. /**
  149. * Sends a simple Item set change event to all interested listeners,
  150. * indicating that anything in the contents may have changed (items added,
  151. * removed etc.).
  152. */
  153. protected void fireItemSetChange() {
  154. fireItemSetChange(new BaseItemSetChangeEvent(this));
  155. }
  156. /**
  157. * Sends an Item set change event to all registered interested listeners.
  158. *
  159. * @param event
  160. * the item set change event to send, optionally with additional
  161. * information
  162. */
  163. protected void fireItemSetChange(ItemSetChangeEvent event) {
  164. if (getItemSetChangeListeners() != null) {
  165. final Object[] l = getItemSetChangeListeners().toArray();
  166. for (int i = 0; i < l.length; i++) {
  167. ((Container.ItemSetChangeListener) l[i])
  168. .containerItemSetChange(event);
  169. }
  170. }
  171. }
  172. /**
  173. * Sets the property set change listener collection. For internal use only.
  174. *
  175. * @param propertySetChangeListeners
  176. */
  177. protected void setPropertySetChangeListeners(
  178. Collection<Container.PropertySetChangeListener> propertySetChangeListeners) {
  179. this.propertySetChangeListeners = propertySetChangeListeners;
  180. }
  181. /**
  182. * Returns the property set change listener collection. For internal use
  183. * only.
  184. */
  185. protected Collection<Container.PropertySetChangeListener> getPropertySetChangeListeners() {
  186. return propertySetChangeListeners;
  187. }
  188. /**
  189. * Sets the item set change listener collection. For internal use only.
  190. *
  191. * @param itemSetChangeListeners
  192. */
  193. protected void setItemSetChangeListeners(
  194. Collection<Container.ItemSetChangeListener> itemSetChangeListeners) {
  195. this.itemSetChangeListeners = itemSetChangeListeners;
  196. }
  197. /**
  198. * Returns the item set change listener collection. For internal use only.
  199. */
  200. protected Collection<Container.ItemSetChangeListener> getItemSetChangeListeners() {
  201. return itemSetChangeListeners;
  202. }
  203. public Collection<?> getListeners(Class<?> eventType) {
  204. if (Container.PropertySetChangeEvent.class.isAssignableFrom(eventType)) {
  205. if (propertySetChangeListeners == null) {
  206. return Collections.EMPTY_LIST;
  207. } else {
  208. return Collections
  209. .unmodifiableCollection(propertySetChangeListeners);
  210. }
  211. } else if (Container.ItemSetChangeEvent.class
  212. .isAssignableFrom(eventType)) {
  213. if (itemSetChangeListeners == null) {
  214. return Collections.EMPTY_LIST;
  215. } else {
  216. return Collections
  217. .unmodifiableCollection(itemSetChangeListeners);
  218. }
  219. }
  220. return Collections.EMPTY_LIST;
  221. }
  222. }