return itemDescriptionGenerator;
}
+ @Override
+ public void containerItemSetChange(
+ com.vaadin.data.Container.ItemSetChangeEvent event) {
+
+ // Ensure removed items are cleaned up from expanded list
+ for (Object expandedItemId : expanded) {
+ if (getItem(expandedItemId) == null) {
+ expanded.remove(expandedItemId);
+ if (this.expandedItemId == expandedItemId) {
+ this.expandedItemId = null;
+ }
+ }
+ }
+
+ super.containerItemSetChange(event);
+ }
+
}
package com.vaadin.tests.server.component.tree;
+import java.lang.reflect.Field;
+import java.util.HashSet;
+
import junit.framework.TestCase;
import com.vaadin.data.Container;
assertTrue(Container.Hierarchical.class.isAssignableFrom(tree4
.getContainerDataSource().getClass()));
}
+
+ public void testRemoveExpandedItems() throws Exception {
+ tree.expandItem("parent");
+ tree.expandItem("child");
+
+ Field expandedField = tree.getClass()
+ .getDeclaredField("expanded");
+ Field expandedItemIdField = tree.getClass().getDeclaredField(
+ "expandedItemId");
+
+ expandedField.setAccessible(true);
+ expandedItemIdField.setAccessible(true);
+
+ HashSet<Object> expanded = (HashSet<Object>) expandedField.get(tree);
+ Object expandedItemId = expandedItemIdField.get(tree);
+
+ assertEquals(2, expanded.size());
+ assertTrue("Contains parent", expanded.contains("parent"));
+ assertTrue("Contains child", expanded.contains("child"));
+ assertEquals("child", expandedItemId);
+
+ tree.removeItem("parent");
+
+ expanded = (HashSet<Object>) expandedField.get(tree);
+ expandedItemId = expandedItemIdField.get(tree);
+
+ assertEquals(1, expanded.size());
+ assertTrue("Contains child", expanded.contains("child"));
+ assertEquals("child", expandedItemId);
+
+ tree.removeItem("child");
+
+ expanded = (HashSet<Object>) expandedField.get(tree);
+ expandedItemId = expandedItemIdField.get(tree);
+
+ assertEquals(0, expanded.size());
+ assertNull(expandedItemId);
+ }
}