Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

AbstractContainer.java 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. @Override
  48. public Container getContainer() {
  49. return (Container) getSource();
  50. }
  51. }
  52. /**
  53. * An <code>event</code> object specifying the container whose Item set has
  54. * changed.
  55. *
  56. * This class does not provide information about the exact changes
  57. * performed, but subclasses can add provide additional information about
  58. * the changes.
  59. */
  60. protected static class BaseItemSetChangeEvent extends EventObject implements
  61. Container.ItemSetChangeEvent, Serializable {
  62. protected BaseItemSetChangeEvent(Container source) {
  63. super(source);
  64. }
  65. @Override
  66. public Container getContainer() {
  67. return (Container) getSource();
  68. }
  69. }
  70. // PropertySetChangeNotifier
  71. /**
  72. * Implementation of the corresponding method in
  73. * {@link PropertySetChangeNotifier}, override with the corresponding public
  74. * method and implement the interface to use this.
  75. *
  76. * @see PropertySetChangeNotifier#addListener(com.vaadin.data.Container.PropertySetChangeListener)
  77. */
  78. protected void addListener(Container.PropertySetChangeListener listener) {
  79. if (getPropertySetChangeListeners() == null) {
  80. setPropertySetChangeListeners(new LinkedList<Container.PropertySetChangeListener>());
  81. }
  82. getPropertySetChangeListeners().add(listener);
  83. }
  84. /**
  85. * Implementation of the corresponding method in
  86. * {@link PropertySetChangeNotifier}, override with the corresponding public
  87. * method and implement the interface to use this.
  88. *
  89. * @see PropertySetChangeNotifier#removeListener(com.vaadin.data.Container.
  90. * PropertySetChangeListener)
  91. */
  92. protected void removeListener(Container.PropertySetChangeListener listener) {
  93. if (getPropertySetChangeListeners() != null) {
  94. getPropertySetChangeListeners().remove(listener);
  95. }
  96. }
  97. // ItemSetChangeNotifier
  98. /**
  99. * Implementation of the corresponding method in
  100. * {@link ItemSetChangeNotifier}, override with the corresponding public
  101. * method and implement the interface to use this.
  102. *
  103. * @see ItemSetChangeNotifier#addListener(com.vaadin.data.Container.ItemSetChangeListener)
  104. */
  105. protected void addListener(Container.ItemSetChangeListener listener) {
  106. if (getItemSetChangeListeners() == null) {
  107. setItemSetChangeListeners(new LinkedList<Container.ItemSetChangeListener>());
  108. }
  109. getItemSetChangeListeners().add(listener);
  110. }
  111. /**
  112. * Implementation of the corresponding method in
  113. * {@link ItemSetChangeNotifier}, override with the corresponding public
  114. * method and implement the interface to use this.
  115. *
  116. * @see ItemSetChangeNotifier#removeListener(com.vaadin.data.Container.ItemSetChangeListener)
  117. */
  118. protected void removeListener(Container.ItemSetChangeListener listener) {
  119. if (getItemSetChangeListeners() != null) {
  120. getItemSetChangeListeners().remove(listener);
  121. }
  122. }
  123. /**
  124. * Sends a simple Property set change event to all interested listeners.
  125. */
  126. protected void fireContainerPropertySetChange() {
  127. fireContainerPropertySetChange(new BasePropertySetChangeEvent(this));
  128. }
  129. /**
  130. * Sends a Property set change event to all interested listeners.
  131. *
  132. * Use {@link #fireContainerPropertySetChange()} instead of this method
  133. * unless additional information about the exact changes is available and
  134. * should be included in the event.
  135. *
  136. * @param event
  137. * the property change event to send, optionally with additional
  138. * information
  139. */
  140. protected void fireContainerPropertySetChange(
  141. Container.PropertySetChangeEvent event) {
  142. if (getPropertySetChangeListeners() != null) {
  143. final Object[] l = getPropertySetChangeListeners().toArray();
  144. for (int i = 0; i < l.length; i++) {
  145. ((Container.PropertySetChangeListener) l[i])
  146. .containerPropertySetChange(event);
  147. }
  148. }
  149. }
  150. /**
  151. * Sends a simple Item set change event to all interested listeners,
  152. * indicating that anything in the contents may have changed (items added,
  153. * removed etc.).
  154. */
  155. protected void fireItemSetChange() {
  156. fireItemSetChange(new BaseItemSetChangeEvent(this));
  157. }
  158. /**
  159. * Sends an Item set change event to all registered interested listeners.
  160. *
  161. * @param event
  162. * the item set change event to send, optionally with additional
  163. * information
  164. */
  165. protected void fireItemSetChange(ItemSetChangeEvent event) {
  166. if (getItemSetChangeListeners() != null) {
  167. final Object[] l = getItemSetChangeListeners().toArray();
  168. for (int i = 0; i < l.length; i++) {
  169. ((Container.ItemSetChangeListener) l[i])
  170. .containerItemSetChange(event);
  171. }
  172. }
  173. }
  174. /**
  175. * Sets the property set change listener collection. For internal use only.
  176. *
  177. * @param propertySetChangeListeners
  178. */
  179. protected void setPropertySetChangeListeners(
  180. Collection<Container.PropertySetChangeListener> propertySetChangeListeners) {
  181. this.propertySetChangeListeners = propertySetChangeListeners;
  182. }
  183. /**
  184. * Returns the property set change listener collection. For internal use
  185. * only.
  186. */
  187. protected Collection<Container.PropertySetChangeListener> getPropertySetChangeListeners() {
  188. return propertySetChangeListeners;
  189. }
  190. /**
  191. * Sets the item set change listener collection. For internal use only.
  192. *
  193. * @param itemSetChangeListeners
  194. */
  195. protected void setItemSetChangeListeners(
  196. Collection<Container.ItemSetChangeListener> itemSetChangeListeners) {
  197. this.itemSetChangeListeners = itemSetChangeListeners;
  198. }
  199. /**
  200. * Returns the item set change listener collection. For internal use only.
  201. */
  202. protected Collection<Container.ItemSetChangeListener> getItemSetChangeListeners() {
  203. return itemSetChangeListeners;
  204. }
  205. public Collection<?> getListeners(Class<?> eventType) {
  206. if (Container.PropertySetChangeEvent.class.isAssignableFrom(eventType)) {
  207. if (propertySetChangeListeners == null) {
  208. return Collections.EMPTY_LIST;
  209. } else {
  210. return Collections
  211. .unmodifiableCollection(propertySetChangeListeners);
  212. }
  213. } else if (Container.ItemSetChangeEvent.class
  214. .isAssignableFrom(eventType)) {
  215. if (itemSetChangeListeners == null) {
  216. return Collections.EMPTY_LIST;
  217. } else {
  218. return Collections
  219. .unmodifiableCollection(itemSetChangeListeners);
  220. }
  221. }
  222. return Collections.EMPTY_LIST;
  223. }
  224. }