From 44dbd7e9c18c04427acc9e0012b1bfc8d41d61f4 Mon Sep 17 00:00:00 2001 From: Matti Tahvonen Date: Thu, 17 Dec 2009 06:27:06 +0000 Subject: [PATCH] fixes #3688 svn changeset:10362/svn branch:6.2 --- .../util/ContainerHierarchicalWrapper.java | 77 +++++++++++++------ 1 file changed, 54 insertions(+), 23 deletions(-) diff --git a/src/com/vaadin/data/util/ContainerHierarchicalWrapper.java b/src/com/vaadin/data/util/ContainerHierarchicalWrapper.java index e0c1324553..6420aaf838 100644 --- a/src/com/vaadin/data/util/ContainerHierarchicalWrapper.java +++ b/src/com/vaadin/data/util/ContainerHierarchicalWrapper.java @@ -4,8 +4,10 @@ package com.vaadin.data.util; +import java.util.Arrays; import java.util.Collection; import java.util.Collections; +import java.util.Comparator; import java.util.HashSet; import java.util.Hashtable; import java.util.Iterator; @@ -42,16 +44,16 @@ public class ContainerHierarchicalWrapper implements Container.Hierarchical, private final Container container; /** Set of IDs of those contained Items that can't have children. */ - private HashSet noChildrenAllowed = null; + private HashSet noChildrenAllowed = null; - /** Mapping from Item ID to parent Item */ - private Hashtable parent = null; + /** Mapping from Item ID to parent Item ID */ + private Hashtable parent = null; /** Mapping from Item ID to a list of child IDs */ - private Hashtable children = null; + private Hashtable> children = null; /** List that contains all root elements of the container. */ - private LinkedHashSet roots = null; + private LinkedHashSet roots = null; /** Is the wrapped container hierarchical by itself ? */ private boolean hierarchical; @@ -77,10 +79,10 @@ public class ContainerHierarchicalWrapper implements Container.Hierarchical, // Create initial order if needed if (!hierarchical) { - noChildrenAllowed = new HashSet(); - parent = new Hashtable(); - children = new Hashtable(); - roots = new LinkedHashSet(container.getItemIds()); + noChildrenAllowed = new HashSet(); + parent = new Hashtable(); + children = new Hashtable>(); + roots = new LinkedHashSet(container.getItemIds()); } updateHierarchicalWrapper(); @@ -100,23 +102,52 @@ public class ContainerHierarchicalWrapper implements Container.Hierarchical, // Recreate hierarchy and data structures if missing if (noChildrenAllowed == null || parent == null || children == null || roots == null) { - noChildrenAllowed = new HashSet(); - parent = new Hashtable(); - children = new Hashtable(); - roots = new LinkedHashSet(container.getItemIds()); + noChildrenAllowed = new HashSet(); + parent = new Hashtable(); + children = new Hashtable>(); + roots = new LinkedHashSet(container.getItemIds()); } // Check that the hierarchy is up-to-date else { + // ensure order of root and child lists is same as in wrapped + // container + final Collection itemIds = container.getItemIds(); + Comparator basedOnOrderFromWrappedContainer = new Comparator() { + public int compare(Object o1, Object o2) { + if (o1.equals(o2)) { + return 0; + } + for (Object id : itemIds) { + if (id == o1) { + return -1; + } else if (id == o2) { + return 1; + } + } + return 0; + } + }; + Object[] array = roots.toArray(); + Arrays.sort(array, basedOnOrderFromWrappedContainer); + roots = new LinkedHashSet(); + for (int i = 0; i < array.length; i++) { + roots.add(array[i]); + } + for (Object object : children.keySet()) { + LinkedList object2 = children.get(object); + Collections.sort(object2, basedOnOrderFromWrappedContainer); + } + // Calculate the set of all items in the hierarchy - final HashSet s = new HashSet(); + final HashSet s = new HashSet(); s.addAll(parent.keySet()); s.addAll(children.keySet()); s.addAll(roots); // Remove unnecessary items - for (final Iterator i = s.iterator(); i.hasNext();) { + for (final Iterator i = s.iterator(); i.hasNext();) { final Object id = i.next(); if (!container.containsId(id)) { removeFromHierarchyWrapper(id); @@ -124,8 +155,8 @@ public class ContainerHierarchicalWrapper implements Container.Hierarchical, } // Add all the missing items - final Collection ids = container.getItemIds(); - for (final Iterator i = ids.iterator(); i.hasNext();) { + final Collection ids = container.getItemIds(); + for (final Iterator i = ids.iterator(); i.hasNext();) { final Object id = i.next(); if (!s.contains(id)) { addToHierarchyWrapper(id); @@ -153,7 +184,7 @@ public class ContainerHierarchicalWrapper implements Container.Hierarchical, } 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); } @@ -201,7 +232,7 @@ public class ContainerHierarchicalWrapper implements Container.Hierarchical, return ((Container.Hierarchical) container).getChildren(itemId); } - final Collection c = (Collection) children.get(itemId); + final Collection c = children.get(itemId); if (c == null) { return null; } @@ -353,7 +384,7 @@ public class ContainerHierarchicalWrapper implements Container.Hierarchical, if (newParentId == null) { // Remove 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()) { @@ -387,9 +418,9 @@ public class ContainerHierarchicalWrapper implements Container.Hierarchical, // Update parent parent.put(itemId, newParentId); - LinkedList pcl = (LinkedList) children.get(newParentId); + LinkedList pcl = children.get(newParentId); if (pcl == null) { - pcl = new LinkedList(); + pcl = new LinkedList(); children.put(newParentId, pcl); } pcl.add(itemId); @@ -398,7 +429,7 @@ public class ContainerHierarchicalWrapper implements Container.Hierarchical, 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()) { -- 2.39.5