From f2cdd8f99d4e51e1893a9603a09fd4cb5a2faba2 Mon Sep 17 00:00:00 2001 From: Henri Sara Date: Wed, 30 Jun 2010 12:17:29 +0000 Subject: [PATCH] #5280 make ContainerHierarchicalWrapper serializable svn changeset:13974/svn branch:6.4 --- .../util/ContainerHierarchicalWrapper.java | 47 ++++++++++++------- 1 file changed, 31 insertions(+), 16 deletions(-) diff --git a/src/com/vaadin/data/util/ContainerHierarchicalWrapper.java b/src/com/vaadin/data/util/ContainerHierarchicalWrapper.java index 668a3fc5dd..4dca5cf52b 100644 --- a/src/com/vaadin/data/util/ContainerHierarchicalWrapper.java +++ b/src/com/vaadin/data/util/ContainerHierarchicalWrapper.java @@ -4,6 +4,7 @@ package com.vaadin.data.util; +import java.io.Serializable; import java.util.Arrays; import java.util.Collection; import java.util.Collections; @@ -58,6 +59,33 @@ public class ContainerHierarchicalWrapper implements Container.Hierarchical, /** Is the wrapped container hierarchical by itself ? */ private boolean hierarchical; + /** + * A comparator that sorts the listed items before other items. Otherwise, + * the order is undefined. + */ + private static class ListedItemsFirstComparator implements + Comparator, Serializable { + private final Collection itemIds; + + private ListedItemsFirstComparator(Collection itemIds) { + this.itemIds = itemIds; + } + + 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; + } + }; + /** * Constructs a new hierarchical wrapper for an existing Container. Works * even if the to-be-wrapped container already implements the @@ -113,22 +141,9 @@ public class ContainerHierarchicalWrapper implements Container.Hierarchical, // 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; - } - }; + Collection itemIds = container.getItemIds(); + Comparator basedOnOrderFromWrappedContainer = new ListedItemsFirstComparator( + itemIds); // Calculate the set of all items in the hierarchy final HashSet s = new HashSet(); -- 2.39.5