From 3db3e34887cc7b6067463ebb07ffe004d3783def Mon Sep 17 00:00:00 2001 From: Matti Tahvonen Date: Sun, 21 Dec 2008 17:21:55 +0000 Subject: [PATCH] fixes #2398 svn changeset:6312/svn branch:trunk --- .../toolkit/data/util/IndexedContainer.java | 42 ++++++++++++++++--- 1 file changed, 37 insertions(+), 5 deletions(-) diff --git a/src/com/itmill/toolkit/data/util/IndexedContainer.java b/src/com/itmill/toolkit/data/util/IndexedContainer.java index 807ef3034b..bf08158ee0 100644 --- a/src/com/itmill/toolkit/data/util/IndexedContainer.java +++ b/src/com/itmill/toolkit/data/util/IndexedContainer.java @@ -10,6 +10,7 @@ import java.util.Collection; import java.util.Collections; import java.util.Comparator; import java.util.EventObject; +import java.util.HashMap; import java.util.HashSet; import java.util.Hashtable; import java.util.Iterator; @@ -114,6 +115,8 @@ public class IndexedContainer implements Container, Container.Indexed, */ private HashSet filters; + private HashMap defaultPropertyValues; + /* Container constructors */ public IndexedContainer() { @@ -272,10 +275,16 @@ public class IndexedContainer implements Container, Container.Indexed, // If default value is given, set it if (defaultValue != null) { + // for existing rows for (final Iterator i = itemIds.iterator(); i.hasNext();) { getItem(i.next()).getItemProperty(propertyId).setValue( defaultValue); } + // store for next rows + if (defaultPropertyValues == null) { + defaultPropertyValues = new HashMap(); + } + defaultPropertyValues.put(propertyId, defaultValue); } // Sends a change event @@ -352,7 +361,11 @@ public class IndexedContainer implements Container, Container.Indexed, // Adds the Item to container itemIds.add(itemId); - items.put(itemId, new Hashtable()); + Hashtable t = new Hashtable(); + items.put(itemId, t); + + addDefaultValues(t); + final Item item = new IndexedContainerItem(itemId); if (filteredItemIds != null) { if (passesFilters(item)) { @@ -366,6 +379,20 @@ public class IndexedContainer implements Container, Container.Indexed, return item; } + /** + * Helper method to add default values for items if available + * + * @param t + * data table of added item + */ + private void addDefaultValues(Hashtable t) { + if (defaultPropertyValues != null) { + for (Object key : defaultPropertyValues.keySet()) { + t.put(key, defaultPropertyValues.get(key)); + } + } + } + /** * Removes the Item corresponding to the given Item ID from the list. * @@ -408,6 +435,7 @@ public class IndexedContainer implements Container, Container.Indexed, // Removes the Property to Property list and types propertyIds.remove(propertyId); types.remove(propertyId); + defaultPropertyValues.remove(propertyId); // If remove the Property from all Items for (final Iterator i = itemIds.iterator(); i.hasNext();) { @@ -673,7 +701,9 @@ public class IndexedContainer implements Container, Container.Indexed, // Adds the Item to container itemIds.add(index, newItemId); - items.put(newItemId, new Hashtable()); + Hashtable t = new Hashtable(); + items.put(newItemId, t); + addDefaultValues(t); if (filteredItemIds != null) { updateContainerFiltering(); @@ -1230,10 +1260,12 @@ public class IndexedContainer implements Container, Container.Indexed, } /** - *

Tests if the Property is in read-only mode. In read-only mode - * calls to the method setValue will throw + *

+ * Tests if the Property is in read-only mode. In read-only mode calls + * to the method setValue will throw * ReadOnlyException s and will not modify the value of the - * Property.

+ * Property. + *

* * @return true if the Property is in read-only mode, * false if it's not. -- 2.39.5