123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251 |
- /*
- @VaadinApache2LicenseForJavaFiles@
- */
- package com.vaadin.data.util;
-
- import java.io.Serializable;
- import java.util.Collection;
- import java.util.Collections;
- import java.util.EventObject;
- import java.util.LinkedList;
-
- import com.vaadin.data.Container;
-
- /**
- * Abstract container class that manages event listeners and sending events to
- * them ({@link PropertySetChangeNotifier}, {@link ItemSetChangeNotifier}).
- *
- * Note that this class provides the internal implementations for both types of
- * events and notifiers as protected methods, but does not implement the
- * {@link PropertySetChangeNotifier} and {@link ItemSetChangeNotifier}
- * interfaces directly. This way, subclasses can choose not to implement them.
- * Subclasses implementing those interfaces should also override the
- * corresponding {@link #addListener()} and {@link #removeListener()} methods to
- * make them public.
- *
- * @since 6.6
- */
- public abstract class AbstractContainer implements Container {
-
- /**
- * List of all Property set change event listeners.
- */
- private Collection<Container.PropertySetChangeListener> propertySetChangeListeners = null;
-
- /**
- * List of all container Item set change event listeners.
- */
- private Collection<Container.ItemSetChangeListener> itemSetChangeListeners = null;
-
- /**
- * An <code>event</code> object specifying the container whose Property set
- * has changed.
- *
- * This class does not provide information about which properties were
- * concerned by the change, but subclasses can provide additional
- * information about the changes.
- */
- protected static class BasePropertySetChangeEvent extends EventObject
- implements Container.PropertySetChangeEvent, Serializable {
-
- protected BasePropertySetChangeEvent(Container source) {
- super(source);
- }
-
- @Override
- public Container getContainer() {
- return (Container) getSource();
- }
- }
-
- /**
- * An <code>event</code> object specifying the container whose Item set has
- * changed.
- *
- * This class does not provide information about the exact changes
- * performed, but subclasses can add provide additional information about
- * the changes.
- */
- protected static class BaseItemSetChangeEvent extends EventObject implements
- Container.ItemSetChangeEvent, Serializable {
-
- protected BaseItemSetChangeEvent(Container source) {
- super(source);
- }
-
- @Override
- public Container getContainer() {
- return (Container) getSource();
- }
- }
-
- // PropertySetChangeNotifier
-
- /**
- * Implementation of the corresponding method in
- * {@link PropertySetChangeNotifier}, override with the corresponding public
- * method and implement the interface to use this.
- *
- * @see PropertySetChangeNotifier#addListener(com.vaadin.data.Container.PropertySetChangeListener)
- */
- protected void addListener(Container.PropertySetChangeListener listener) {
- if (getPropertySetChangeListeners() == null) {
- setPropertySetChangeListeners(new LinkedList<Container.PropertySetChangeListener>());
- }
- getPropertySetChangeListeners().add(listener);
- }
-
- /**
- * Implementation of the corresponding method in
- * {@link PropertySetChangeNotifier}, override with the corresponding public
- * method and implement the interface to use this.
- *
- * @see PropertySetChangeNotifier#removeListener(com.vaadin.data.Container.
- * PropertySetChangeListener)
- */
- protected void removeListener(Container.PropertySetChangeListener listener) {
- if (getPropertySetChangeListeners() != null) {
- getPropertySetChangeListeners().remove(listener);
- }
- }
-
- // ItemSetChangeNotifier
-
- /**
- * Implementation of the corresponding method in
- * {@link ItemSetChangeNotifier}, override with the corresponding public
- * method and implement the interface to use this.
- *
- * @see ItemSetChangeNotifier#addListener(com.vaadin.data.Container.ItemSetChangeListener)
- */
- protected void addListener(Container.ItemSetChangeListener listener) {
- if (getItemSetChangeListeners() == null) {
- setItemSetChangeListeners(new LinkedList<Container.ItemSetChangeListener>());
- }
- getItemSetChangeListeners().add(listener);
- }
-
- /**
- * Implementation of the corresponding method in
- * {@link ItemSetChangeNotifier}, override with the corresponding public
- * method and implement the interface to use this.
- *
- * @see ItemSetChangeNotifier#removeListener(com.vaadin.data.Container.ItemSetChangeListener)
- */
- protected void removeListener(Container.ItemSetChangeListener listener) {
- if (getItemSetChangeListeners() != null) {
- getItemSetChangeListeners().remove(listener);
- }
- }
-
- /**
- * Sends a simple Property set change event to all interested listeners.
- */
- protected void fireContainerPropertySetChange() {
- fireContainerPropertySetChange(new BasePropertySetChangeEvent(this));
- }
-
- /**
- * Sends a Property set change event to all interested listeners.
- *
- * Use {@link #fireContainerPropertySetChange()} instead of this method
- * unless additional information about the exact changes is available and
- * should be included in the event.
- *
- * @param event
- * the property change event to send, optionally with additional
- * information
- */
- protected void fireContainerPropertySetChange(
- Container.PropertySetChangeEvent event) {
- if (getPropertySetChangeListeners() != null) {
- final Object[] l = getPropertySetChangeListeners().toArray();
- for (int i = 0; i < l.length; i++) {
- ((Container.PropertySetChangeListener) l[i])
- .containerPropertySetChange(event);
- }
- }
- }
-
- /**
- * Sends a simple Item set change event to all interested listeners,
- * indicating that anything in the contents may have changed (items added,
- * removed etc.).
- */
- protected void fireItemSetChange() {
- fireItemSetChange(new BaseItemSetChangeEvent(this));
- }
-
- /**
- * Sends an Item set change event to all registered interested listeners.
- *
- * @param event
- * the item set change event to send, optionally with additional
- * information
- */
- protected void fireItemSetChange(ItemSetChangeEvent event) {
- if (getItemSetChangeListeners() != null) {
- final Object[] l = getItemSetChangeListeners().toArray();
- for (int i = 0; i < l.length; i++) {
- ((Container.ItemSetChangeListener) l[i])
- .containerItemSetChange(event);
- }
- }
- }
-
- /**
- * Sets the property set change listener collection. For internal use only.
- *
- * @param propertySetChangeListeners
- */
- protected void setPropertySetChangeListeners(
- Collection<Container.PropertySetChangeListener> propertySetChangeListeners) {
- this.propertySetChangeListeners = propertySetChangeListeners;
- }
-
- /**
- * Returns the property set change listener collection. For internal use
- * only.
- */
- protected Collection<Container.PropertySetChangeListener> getPropertySetChangeListeners() {
- return propertySetChangeListeners;
- }
-
- /**
- * Sets the item set change listener collection. For internal use only.
- *
- * @param itemSetChangeListeners
- */
- protected void setItemSetChangeListeners(
- Collection<Container.ItemSetChangeListener> itemSetChangeListeners) {
- this.itemSetChangeListeners = itemSetChangeListeners;
- }
-
- /**
- * Returns the item set change listener collection. For internal use only.
- */
- protected Collection<Container.ItemSetChangeListener> getItemSetChangeListeners() {
- return itemSetChangeListeners;
- }
-
- public Collection<?> getListeners(Class<?> eventType) {
- if (Container.PropertySetChangeEvent.class.isAssignableFrom(eventType)) {
- if (propertySetChangeListeners == null) {
- return Collections.EMPTY_LIST;
- } else {
- return Collections
- .unmodifiableCollection(propertySetChangeListeners);
- }
- } else if (Container.ItemSetChangeEvent.class
- .isAssignableFrom(eventType)) {
- if (itemSetChangeListeners == null) {
- return Collections.EMPTY_LIST;
- } else {
- return Collections
- .unmodifiableCollection(itemSetChangeListeners);
- }
- }
-
- return Collections.EMPTY_LIST;
- }
- }
|