diff options
author | Henri Sara <henri.sara@itmill.com> | 2011-03-04 11:14:32 +0000 |
---|---|---|
committer | Henri Sara <henri.sara@itmill.com> | 2011-03-04 11:14:32 +0000 |
commit | affb4dbd876eba5527d39edda404013fea2e28c2 (patch) | |
tree | e7fcdaf7f6763a6a1b2a435ab7af95a39bd90723 | |
parent | b9a512986633326706e95902f3dfbf120a9e54bb (diff) | |
download | vaadin-framework-affb4dbd876eba5527d39edda404013fea2e28c2.tar.gz vaadin-framework-affb4dbd876eba5527d39edda404013fea2e28c2.zip |
#6527 Container refactoring: moved most of Sortable implementation to AbstractInMemoryContainer
svn changeset:17604/svn branch:6.6
-rw-r--r-- | src/com/vaadin/data/util/AbstractBeanContainer.java | 60 | ||||
-rw-r--r-- | src/com/vaadin/data/util/AbstractInMemoryContainer.java | 91 | ||||
-rw-r--r-- | src/com/vaadin/data/util/IndexedContainer.java | 64 |
3 files changed, 106 insertions, 109 deletions
diff --git a/src/com/vaadin/data/util/AbstractBeanContainer.java b/src/com/vaadin/data/util/AbstractBeanContainer.java index 56e93733a8..3b8c32455b 100644 --- a/src/com/vaadin/data/util/AbstractBeanContainer.java +++ b/src/com/vaadin/data/util/AbstractBeanContainer.java @@ -9,10 +9,8 @@ import java.io.Serializable; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.Collection; -import java.util.Collections; import java.util.HashMap; import java.util.LinkedHashMap; -import java.util.LinkedList; import java.util.Map; import com.vaadin.data.Container.Filterable; @@ -134,11 +132,6 @@ public abstract class AbstractBeanContainer<IDTYPE, BEANTYPE> extends private BeanIdResolver<IDTYPE, BEANTYPE> beanIdResolver = null; /** - * The item sorter which is used for sorting the container. - */ - private ItemSorter itemSorter = new DefaultItemSorter(); - - /** * Maps all item ids in the container (including filtered) to their * corresponding BeanItem. */ @@ -454,16 +447,8 @@ public abstract class AbstractBeanContainer<IDTYPE, BEANTYPE> extends * * @see com.vaadin.data.Container.Sortable#getSortableContainerPropertyIds() */ - public Collection<Object> getSortableContainerPropertyIds() { - LinkedList<Object> sortables = new LinkedList<Object>(); - for (Object propertyId : getContainerPropertyIds()) { - Class<?> propertyType = getType(propertyId); - if (Comparable.class.isAssignableFrom(propertyType) - || propertyType.isPrimitive()) { - sortables.add(propertyId); - } - } - return sortables; + public Collection<?> getSortableContainerPropertyIds() { + return getSortablePropertyIds(); } /* @@ -472,48 +457,19 @@ public abstract class AbstractBeanContainer<IDTYPE, BEANTYPE> extends * @see com.vaadin.data.Container.Sortable#sort(java.lang.Object[], * boolean[]) */ + @Override public void sort(Object[] propertyId, boolean[] ascending) { - itemSorter.setSortProperties(this, propertyId, ascending); - - doSort(); - - // notifies if anything changes in the filtered list, including order - filterAll(); + super.sort(propertyId, ascending); } - /** - * Perform the sorting of the data structures in the container. This is - * invoked when the <code>itemSorter</code> has been prepared for the sort - * operation. Typically this method calls - * <code>Collections.sort(aCollection, getItemSorter())</code> on all arrays - * (containing item ids) that need to be sorted. - * - */ - protected void doSort() { - Collections.sort(allItemIds, getItemSorter()); - } - - /** - * Returns the ItemSorter that is used for sorting the container. - * - * @see #setItemSorter(ItemSorter) - * - * @return The ItemSorter that is used for sorting the container - */ + @Override public ItemSorter getItemSorter() { - return itemSorter; + return super.getItemSorter(); } - /** - * Sets the ItemSorter that is used for sorting the container. The - * {@link ItemSorter#compare(Object, Object)} method is called to compare - * two beans (item ids). - * - * @param itemSorter - * The ItemSorter to use when sorting the container - */ + @Override public void setItemSorter(ItemSorter itemSorter) { - this.itemSorter = itemSorter; + super.setItemSorter(itemSorter); } /** diff --git a/src/com/vaadin/data/util/AbstractInMemoryContainer.java b/src/com/vaadin/data/util/AbstractInMemoryContainer.java index 32ac3a70be..2197c28905 100644 --- a/src/com/vaadin/data/util/AbstractInMemoryContainer.java +++ b/src/com/vaadin/data/util/AbstractInMemoryContainer.java @@ -69,6 +69,11 @@ public abstract class AbstractInMemoryContainer<ITEMIDTYPE, PROPERTYIDCLASS, ITE */ private Set<Filter> filters = new HashSet<Filter>(); + /** + * The item sorter which is used for sorting the container. + */ + private ItemSorter itemSorter = new DefaultItemSorter(); + // Constructors /** @@ -351,6 +356,92 @@ public abstract class AbstractInMemoryContainer<ITEMIDTYPE, PROPERTYIDCLASS, ITE return Collections.emptyList(); } + // sorting + + /** + * Returns the ItemSorter used for comparing items in a sort. See + * {@link #setItemSorter(ItemSorter)} for more information. + * + * @return The ItemSorter used for comparing two items in a sort. + */ + protected ItemSorter getItemSorter() { + return itemSorter; + } + + /** + * Sets the ItemSorter used for comparing items in a sort. The + * {@link ItemSorter#compare(Object, Object)} method is called with item ids + * to perform the sorting. A default ItemSorter is used if this is not + * explicitly set. + * + * @param itemSorter + * The ItemSorter used for comparing two items in a sort (not + * null). + */ + protected void setItemSorter(ItemSorter itemSorter) { + this.itemSorter = itemSorter; + } + + /** + * Sort base implementation to be used to implement {@link Sortable}. + * + * Subclasses should override this with a public + * {@link #sort(Object[], boolean[])} method calling this superclass method + * when implementing Sortable. + * + * @see com.vaadin.data.Container.Sortable#sort(java.lang.Object[], + * boolean[]) + */ + protected void sort(Object[] propertyId, boolean[] ascending) { + if (!(this instanceof Sortable)) { + throw new UnsupportedOperationException( + "Cannot sort a Container that does not implement Sortable"); + } + + // Set up the item sorter for the sort operation + getItemSorter().setSortProperties((Sortable) this, propertyId, + ascending); + + // Perform the actual sort + doSort(); + + // Post sort updates + if (getFilteredItemIds() != null) { + filterAll(); + } else { + fireItemSetChange(); + } + + } + + /** + * Perform the sorting of the data structures in the container. This is + * invoked when the <code>itemSorter</code> has been prepared for the sort + * operation. Typically this method calls + * <code>Collections.sort(aCollection, getItemSorter())</code> on all arrays + * (containing item ids) that need to be sorted. + * + */ + protected void doSort() { + Collections.sort(allItemIds, getItemSorter()); + } + + /** + * Returns the sortable property identifiers for the container. Can be used + * to implement {@link Sortable#getSortableContainerPropertyIds()}. + */ + protected Collection<?> getSortablePropertyIds() { + LinkedList<Object> sortables = new LinkedList<Object>(); + for (Object propertyId : getContainerPropertyIds()) { + Class<?> propertyType = getType(propertyId); + if (Comparable.class.isAssignableFrom(propertyType) + || propertyType.isPrimitive()) { + sortables.add(propertyId); + } + } + return sortables; + } + // removing items /** diff --git a/src/com/vaadin/data/util/IndexedContainer.java b/src/com/vaadin/data/util/IndexedContainer.java index 62ace0f80d..117e1e24ec 100644 --- a/src/com/vaadin/data/util/IndexedContainer.java +++ b/src/com/vaadin/data/util/IndexedContainer.java @@ -89,11 +89,6 @@ public class IndexedContainer extends */ private Hashtable<Object, Map<Object, List<Property.ValueChangeListener>>> singlePropertyValueChangeListeners = null; - /** - * The item sorter which is used for sorting the container. - */ - private ItemSorter itemSorter = new DefaultItemSorter(); - private HashMap<Object, Object> defaultPropertyValues; private int nextGeneratedItemId = 1; @@ -964,32 +959,9 @@ public class IndexedContainer extends * @see com.vaadin.data.Container.Sortable#sort(java.lang.Object[], * boolean[]) */ + @Override public void sort(Object[] propertyId, boolean[] ascending) { - // Set up the item sorter for the sort operation - itemSorter.setSortProperties(this, propertyId, ascending); - - // Perform the actual sort - doSort(); - - // Post sort updates - if (getFilteredItemIds() != null) { - filterAll(); - } else { - fireItemSetChange(); - } - - } - - /** - * Perform the sorting of the data structures in the container. This is - * invoked when the <code>itemSorter</code> has been prepared for the sort - * operation. Typically this method calls - * <code>Collections.sort(aCollection, getItemSorter())</code> on all arrays - * (containing item ids) that need to be sorted. - * - */ - protected void doSort() { - Collections.sort(allItemIds, getItemSorter()); + super.sort(propertyId, ascending); } /* @@ -999,39 +971,17 @@ public class IndexedContainer extends * () */ public Collection<?> getSortableContainerPropertyIds() { - - final List<Object> list = new LinkedList<Object>(); - for (final Iterator<?> i = propertyIds.iterator(); i.hasNext();) { - final Object id = i.next(); - final Class<?> type = getType(id); - if (type != null && Comparable.class.isAssignableFrom(type)) { - list.add(id); - } - } - - return list; + return getSortablePropertyIds(); } - /** - * Returns the ItemSorter used for comparing items in a sort. See - * {@link #setItemSorter(ItemSorter)} for more information. - * - * @return The ItemSorter used for comparing two items in a sort. - */ + @Override public ItemSorter getItemSorter() { - return itemSorter; + return super.getItemSorter(); } - /** - * Sets the ItemSorter used for comparing items in a sort. The ItemSorter is - * called for each collection that needs sorting. A default ItemSorter is - * used if this is not explicitly set. - * - * @param itemSorter - * The ItemSorter used for comparing two items in a sort. - */ + @Override public void setItemSorter(ItemSorter itemSorter) { - this.itemSorter = itemSorter; + super.setItemSorter(itemSorter); } /** |