From: Artur Signell Date: Thu, 17 Sep 2009 13:23:30 +0000 (+0000) Subject: Merged: Fix for #3095 - Sorting of a HierarchicalContainer does not work X-Git-Tag: 6.7.0.beta1~2476 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=6799e62e05f9ba8554c124261b9e0d545c3fb9e9;p=vaadin-framework.git Merged: Fix for #3095 - Sorting of a HierarchicalContainer does not work svn changeset:8835/svn branch:6.2 --- diff --git a/src/com/vaadin/data/util/HierarchicalContainer.java b/src/com/vaadin/data/util/HierarchicalContainer.java index 9ef4cdeb60..a6af0427ad 100644 --- a/src/com/vaadin/data/util/HierarchicalContainer.java +++ b/src/com/vaadin/data/util/HierarchicalContainer.java @@ -6,8 +6,8 @@ package com.vaadin.data.util; import java.util.Collection; import java.util.Collections; +import java.util.HashMap; import java.util.HashSet; -import java.util.Hashtable; import java.util.LinkedList; import com.vaadin.data.Container; @@ -29,22 +29,22 @@ public class HierarchicalContainer extends IndexedContainer implements /** * Set of IDs of those contained Items that can't have children. */ - private final HashSet noChildrenAllowed = new HashSet(); + private final HashSet noChildrenAllowed = new HashSet(); /** * Mapping from Item ID to parent Item. */ - private final Hashtable parent = new Hashtable(); + private final HashMap parent = new HashMap(); /** * Mapping from Item ID to a list of child IDs. */ - private final Hashtable children = new Hashtable(); + private final HashMap> children = new HashMap>(); /** * List that contains all root elements of the container. */ - private final LinkedList roots = new LinkedList(); + private final LinkedList roots = new LinkedList(); /* * Can the specified Item have any children? Don't add a JavaDoc comment @@ -60,7 +60,7 @@ public class HierarchicalContainer extends IndexedContainer implements * interface. */ public Collection getChildren(Object itemId) { - final Collection c = (Collection) children.get(itemId); + final Collection c = children.get(itemId); if (c == null) { return null; } @@ -176,7 +176,7 @@ public class HierarchicalContainer extends IndexedContainer implements if (newParentId == null) { // Removes from old parents children list - final LinkedList l = (LinkedList) children.get(itemId); + final LinkedList l = children.get(itemId); if (l != null) { l.remove(itemId); if (l.isEmpty()) { @@ -210,7 +210,7 @@ public class HierarchicalContainer extends IndexedContainer implements // Updates parent parent.put(itemId, newParentId); - LinkedList pcl = (LinkedList) children.get(newParentId); + LinkedList pcl = children.get(newParentId); if (pcl == null) { pcl = new LinkedList(); children.put(newParentId, pcl); @@ -221,7 +221,7 @@ public class HierarchicalContainer extends IndexedContainer implements if (oldParentId == null) { roots.remove(itemId); } else { - final LinkedList l = (LinkedList) children.get(oldParentId); + final LinkedList l = children.get(oldParentId); if (l != null) { l.remove(itemId); if (l.isEmpty()) { @@ -251,8 +251,7 @@ public class HierarchicalContainer extends IndexedContainer implements /* * (non-Javadoc) * - * @see - * com.vaadin.data.util.IndexedContainer#addItem(java.lang.Object) + * @see com.vaadin.data.util.IndexedContainer#addItem(java.lang.Object) */ @Override public Item addItem(Object itemId) { @@ -284,9 +283,7 @@ public class HierarchicalContainer extends IndexedContainer implements /* * (non-Javadoc) * - * @see - * com.vaadin.data.util.IndexedContainer#removeItem(java.lang.Object - * ) + * @see com.vaadin.data.util.IndexedContainer#removeItem(java.lang.Object ) */ @Override public boolean removeItem(Object itemId) { @@ -299,7 +296,7 @@ public class HierarchicalContainer extends IndexedContainer implements children.remove(itemId); final Object p = parent.get(itemId); if (p != null) { - final LinkedList c = (LinkedList) children.get(p); + final LinkedList c = children.get(p); if (c != null) { c.remove(itemId); } @@ -311,4 +308,13 @@ public class HierarchicalContainer extends IndexedContainer implements return success; } + @Override + void doSort() { + super.doSort(); + Collections.sort(roots, this); + for (LinkedList childList : children.values()) { + Collections.sort(childList, this); + } + } + } diff --git a/src/com/vaadin/data/util/IndexedContainer.java b/src/com/vaadin/data/util/IndexedContainer.java index 5e52c729ee..b7c6463988 100644 --- a/src/com/vaadin/data/util/IndexedContainer.java +++ b/src/com/vaadin/data/util/IndexedContainer.java @@ -49,7 +49,7 @@ import com.vaadin.data.Property; @SuppressWarnings("serial") public class IndexedContainer implements Container.Indexed, Container.ItemSetChangeNotifier, Container.PropertySetChangeNotifier, - Property.ValueChangeNotifier, Container.Sortable, Comparator, + Property.ValueChangeNotifier, Container.Sortable, Comparator, Cloneable, Container.Filterable { /* Internal structure */ @@ -57,7 +57,7 @@ public class IndexedContainer implements Container.Indexed, /** * Linked list of ordered Item IDs. */ - private ArrayList itemIds = new ArrayList(); + private ArrayList itemIds = new ArrayList(); /** List of item ids that passes the filtering */ private LinkedHashSet filteredItemIds = null; @@ -1399,8 +1399,8 @@ public class IndexedContainer implements Container.Indexed, sortDirection[i] = (orders.get(i)).booleanValue(); } - // Sort - Collections.sort(itemIds, this); + doSort(); + if (filteredItemIds != null) { updateContainerFiltering(); } else { @@ -1413,6 +1413,15 @@ public class IndexedContainer implements Container.Indexed, } + /** + * Perform the sorting of the container. Called when everything needed for + * the compare function has been set up. + * + */ + void doSort() { + Collections.sort(itemIds, this); + } + /* * (non-Javadoc) *