diff options
author | Henri Sara <henri.sara@itmill.com> | 2011-02-28 14:37:24 +0000 |
---|---|---|
committer | Henri Sara <henri.sara@itmill.com> | 2011-02-28 14:37:24 +0000 |
commit | 5d278e372b7b431e3fd4dcc60417ec1cda313c2c (patch) | |
tree | e100ac14bdc8b67f8844c5bd647d7030a04ba618 /src/com | |
parent | 87b14ef3366f71c7cc29f371bb44d1c09d3d7c1d (diff) | |
download | vaadin-framework-5d278e372b7b431e3fd4dcc60417ec1cda313c2c.tar.gz vaadin-framework-5d278e372b7b431e3fd4dcc60417ec1cda313c2c.zip |
#6521 Container refactoring: AbstractContainer handling listener registrations and event firing
svn changeset:17504/svn branch:6.6
Diffstat (limited to 'src/com')
-rw-r--r-- | src/com/vaadin/data/util/AbstractBeanContainer.java | 57 | ||||
-rw-r--r-- | src/com/vaadin/data/util/AbstractContainer.java | 223 | ||||
-rw-r--r-- | src/com/vaadin/data/util/IndexedContainer.java | 138 |
3 files changed, 252 insertions, 166 deletions
diff --git a/src/com/vaadin/data/util/AbstractBeanContainer.java b/src/com/vaadin/data/util/AbstractBeanContainer.java index 7adc57257a..f8ea7ebdd1 100644 --- a/src/com/vaadin/data/util/AbstractBeanContainer.java +++ b/src/com/vaadin/data/util/AbstractBeanContainer.java @@ -60,9 +60,9 @@ import com.vaadin.data.Property.ValueChangeNotifier; * * @since 6.5 */ -public abstract class AbstractBeanContainer<IDTYPE, BEANTYPE> implements - Indexed, Filterable, Sortable, ValueChangeListener, - ItemSetChangeNotifier { +public abstract class AbstractBeanContainer<IDTYPE, BEANTYPE> extends + AbstractContainer implements Indexed, Filterable, Sortable, + ValueChangeListener, ItemSetChangeNotifier { /** * Resolver that maps beans to their (item) identifiers, removing the need @@ -183,12 +183,6 @@ public abstract class AbstractBeanContainer<IDTYPE, BEANTYPE> implements private transient LinkedHashMap<String, PropertyDescriptor> model; /** - * Collection of listeners interested in - * {@link Container.ItemSetChangeEvent ItemSetChangeEvent} events. - */ - private List<ItemSetChangeListener> itemSetChangeListeners; - - /** * Constructs a {@code AbstractBeanContainer} for beans of the given type. * * @param type @@ -274,47 +268,14 @@ public abstract class AbstractBeanContainer<IDTYPE, BEANTYPE> implements throw new UnsupportedOperationException(); } - /* - * (non-Javadoc) - * - * @see - * com.vaadin.data.Container.ItemSetChangeNotifier#addListener(com.vaadin - * .data.Container.ItemSetChangeListener) - */ - public void addListener(ItemSetChangeListener listener) { - if (itemSetChangeListeners == null) { - itemSetChangeListeners = new LinkedList<ItemSetChangeListener>(); - } - itemSetChangeListeners.add(listener); - } - - /* - * (non-Javadoc) - * - * @see - * com.vaadin.data.Container.ItemSetChangeNotifier#removeListener(com.vaadin - * .data.Container.ItemSetChangeListener) - */ - public void removeListener(ItemSetChangeListener listener) { - if (itemSetChangeListeners != null) { - itemSetChangeListeners.remove(listener); - } + @Override + public void addListener(Container.ItemSetChangeListener listener) { + super.addListener(listener); } - /** - * Send an ItemSetChange event to all listeners. - */ - protected void fireItemSetChange() { - if (itemSetChangeListeners != null) { - final Container.ItemSetChangeEvent event = new Container.ItemSetChangeEvent() { - public Container getContainer() { - return AbstractBeanContainer.this; - } - }; - for (ItemSetChangeListener listener : itemSetChangeListeners) { - listener.containerItemSetChange(event); - } - } + @Override + public void removeListener(Container.ItemSetChangeListener listener) { + super.removeListener(listener); } /* diff --git a/src/com/vaadin/data/util/AbstractContainer.java b/src/com/vaadin/data/util/AbstractContainer.java new file mode 100644 index 0000000000..915d4e1bcb --- /dev/null +++ b/src/com/vaadin/data/util/AbstractContainer.java @@ -0,0 +1,223 @@ +package com.vaadin.data.util; + +import java.io.Serializable; +import java.util.Collection; +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); + } + + 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); + } + + 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. + */ + 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; + } + +} diff --git a/src/com/vaadin/data/util/IndexedContainer.java b/src/com/vaadin/data/util/IndexedContainer.java index f35e09a68d..6610cb3767 100644 --- a/src/com/vaadin/data/util/IndexedContainer.java +++ b/src/com/vaadin/data/util/IndexedContainer.java @@ -34,7 +34,7 @@ import com.vaadin.data.Property; * <li> {@link Container.Ordered} * <li> {@link Container.Sortable} * <li> {@link Container.Filterable} - * <li> {@link Cloneable} + * <li> {@link Cloneable} (deprecated, might be removed in the future) * <li>Sends all needed events on content changes. * </ul> * @@ -47,10 +47,10 @@ import com.vaadin.data.Property; */ @SuppressWarnings("serial") -public class IndexedContainer implements Container.Indexed, - Container.ItemSetChangeNotifier, Container.PropertySetChangeNotifier, - Property.ValueChangeNotifier, Container.Sortable, Cloneable, - Container.Filterable { +public class IndexedContainer extends AbstractContainer implements + Container.Indexed, Container.ItemSetChangeNotifier, + Container.PropertySetChangeNotifier, Property.ValueChangeNotifier, + Container.Sortable, Cloneable, Container.Filterable { /* Internal structure */ @@ -98,16 +98,6 @@ public class IndexedContainer implements Container.Indexed, private Hashtable<Object, Map<Object, List<Property.ValueChangeListener>>> singlePropertyValueChangeListeners = null; /** - * List of all Property set change event listeners. - */ - private LinkedList<Container.PropertySetChangeListener> propertySetChangeListeners = null; - - /** - * List of all container Item set change event listeners. - */ - private LinkedList<Container.ItemSetChangeListener> itemSetChangeListeners = null; - - /** * The item sorter which is used for sorting the container. */ private ItemSorter itemSorter = new DefaultItemSorter(); @@ -739,32 +729,6 @@ public class IndexedContainer implements Container.Indexed, } /** - * An <code>event</code> object specifying the list whose Property set has - * changed. - * - * @author IT Mill Ltd. - * @version - * @VERSION@ - * @since 3.0 - */ - private class PropertySetChangeEvent extends EventObject implements - Container.PropertySetChangeEvent, Serializable { - - private PropertySetChangeEvent(IndexedContainer source) { - super(source); - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.Container.PropertySetChangeEvent#getContainer () - */ - public Container getContainer() { - return (Container) getSource(); - } - } - - /** * An <code>event</code> object specifying the list whose Item set has * changed. * @@ -773,8 +737,7 @@ public class IndexedContainer implements Container.Indexed, * @VERSION@ * @since 3.0 */ - public class ItemSetChangeEvent extends EventObject implements - Container.ItemSetChangeEvent, Serializable { + public class ItemSetChangeEvent extends BaseItemSetChangeEvent { private final int addedItemIndex; @@ -783,15 +746,6 @@ public class IndexedContainer implements Container.Indexed, this.addedItemIndex = addedItemIndex; } - /* - * (non-Javadoc) - * - * @see com.vaadin.data.Container.ItemSetChangeEvent#getContainer() - */ - public Container getContainer() { - return (Container) getSource(); - } - /** * Iff one item is added, gives its index. * @@ -831,54 +785,20 @@ public class IndexedContainer implements Container.Indexed, } - /* - * (non-Javadoc) - * - * @see com.vaadin.data.Container.PropertySetChangeNotifier#addListener - * (com.vaadin.data.Container.PropertySetChangeListener) - */ public void addListener(Container.PropertySetChangeListener listener) { - if (propertySetChangeListeners == null) { - propertySetChangeListeners = new LinkedList<Container.PropertySetChangeListener>(); - } - propertySetChangeListeners.add(listener); + super.addListener(listener); } - /* - * (non-Javadoc) - * - * @see com.vaadin.data.Container.PropertySetChangeNotifier#removeListener - * (com.vaadin.data.Container.PropertySetChangeListener) - */ public void removeListener(Container.PropertySetChangeListener listener) { - if (propertySetChangeListeners != null) { - propertySetChangeListeners.remove(listener); - } + super.removeListener(listener); } - /* - * (non-Javadoc) - * - * @see com.vaadin.data.Container.ItemSetChangeNotifier#addListener(com - * .vaadin.data.Container.ItemSetChangeListener) - */ public void addListener(Container.ItemSetChangeListener listener) { - if (itemSetChangeListeners == null) { - itemSetChangeListeners = new LinkedList<Container.ItemSetChangeListener>(); - } - itemSetChangeListeners.add(listener); + super.addListener(listener); } - /* - * (non-Javadoc) - * - * @see com.vaadin.data.Container.ItemSetChangeNotifier#removeListener - * (com.vaadin.data.Container.ItemSetChangeListener) - */ public void removeListener(Container.ItemSetChangeListener listener) { - if (itemSetChangeListeners != null) { - itemSetChangeListeners.remove(listener); - } + super.removeListener(listener); } /* @@ -946,36 +866,14 @@ public class IndexedContainer implements Container.Indexed, } /** - * Sends a Property set change event to all interested listeners. - */ - private void fireContainerPropertySetChange() { - if (propertySetChangeListeners != null) { - final Object[] l = propertySetChangeListeners.toArray(); - final Container.PropertySetChangeEvent event = new IndexedContainer.PropertySetChangeEvent( - this); - for (int i = 0; i < l.length; i++) { - ((Container.PropertySetChangeListener) l[i]) - .containerPropertySetChange(event); - } - } - } - - /** * Sends Item set change event to all registered interested listeners. * * @param addedItemIndex * index of new item if change event was an item addition */ protected void fireContentsChange(int addedItemIndex) { - if (itemSetChangeListeners != null) { - final Object[] l = itemSetChangeListeners.toArray(); - final Container.ItemSetChangeEvent event = new IndexedContainer.ItemSetChangeEvent( - this, addedItemIndex); - for (int i = 0; i < l.length; i++) { - ((Container.ItemSetChangeListener) l[i]) - .containerItemSetChange(event); - } - } + fireItemSetChange(new IndexedContainer.ItemSetChangeEvent(this, + addedItemIndex)); } /** @@ -1465,7 +1363,11 @@ public class IndexedContainer implements Container.Indexed, * * @throws CloneNotSupportedException * if an object cannot be cloned. . + * + * @deprecated cloning support might be removed from IndexedContainer in the + * future */ + @Deprecated @Override public Object clone() throws CloneNotSupportedException { @@ -1475,12 +1377,12 @@ public class IndexedContainer implements Container.Indexed, // Clone the shallow properties nc.itemIds = itemIds != null ? (ArrayList<Object>) itemIds.clone() : null; - nc.itemSetChangeListeners = itemSetChangeListeners != null ? (LinkedList<Container.ItemSetChangeListener>) itemSetChangeListeners - .clone() : null; + nc.setItemSetChangeListeners(getItemSetChangeListeners() != null ? new LinkedList<Container.ItemSetChangeListener>( + getItemSetChangeListeners()) : null); nc.propertyIds = propertyIds != null ? (ArrayList<Object>) propertyIds .clone() : null; - nc.propertySetChangeListeners = propertySetChangeListeners != null ? (LinkedList<Container.PropertySetChangeListener>) propertySetChangeListeners - .clone() : null; + nc.setPropertySetChangeListeners(getPropertySetChangeListeners() != null ? new LinkedList<Container.PropertySetChangeListener>( + getPropertySetChangeListeners()) : null); nc.propertyValueChangeListeners = propertyValueChangeListeners != null ? (LinkedList<Property.ValueChangeListener>) propertyValueChangeListeners .clone() : null; nc.readOnlyProperties = readOnlyProperties != null ? (HashSet<Property>) readOnlyProperties |