Beginner - hierarchical container data binding

Hello,

I need some help with a hierarchical container. I’ve used the code from here http://vaadin.com/forum/-/message_boards/message/68224 to build a tree, using icons and captions (also, item types).

I have two types of items in my tree, and they can have the same name, also, it is possible that they might have the same id (item_type_a id might be the same with some other item_type_b id).

That’s why I’ve added the items using:

Object i = tree_container.addItem();
tree_container.getContainerProperty(i, "caption").setValue(myobject.getName());

Now, the problem is that when I remove an item from the tree (using a contextual menu), the container is updated (I get an event for ItemSetChangeEvent) but the original object is not removed from the source (a Set, member of another object).

And… of course it is not, since the container does not have a direct reference to the original object.

Can anyone point me to a solution to this? Either how to properly connect the objects source to the tree container, so they can get removed automatically, or… how can I find out what item got removed from the tree container, so I can remove it from the original list?

Thank you
Alex

If I understood you correctly, you have a Set of items that is
copied
to HierarchicalContainer bound to a Tree. Then you have implemented an action handler to remove nodes from the Tree and you would also like to remove them from the original Set?

Why not just remove the item from the Set also if you have written the remove action handler yourself?

Hello,

Well, yes, that might be the problem, that I “copy” the set in the container… because it was the only way I could do it (like I’ve said, beginner)…

I have something like

public class Project()
{
Set<Group> groups = new HashSet<Group>();
...

public Set getGroups()
{
  return this.groups.
}

}

A Group can have subgroups and Iterations…

Then, I want to build a tree out of the groups of a project:

HierarchicalContainer tree_container = new HierachicalContainer();
tree_container.addContainerProperty("icon", Resource.class, null);
tree_container.addContainerProperty("caption", String.class, null);
tree_container.addContainerProperty("type", String.class, null);
recursiveAddGroups(project_tree_container, p.getGroups(), null);// p is a Project

And the function that builds the tree container:

public void recursiveAddGroups(HierarchicalContainer project_tree_container, Set<Group> groups, Object parentId)
	{
		
		if(groups == null)
		{
			return;
		}
		
		for (Iterator<Group> g_iterator = groups.iterator(); g_iterator.hasNext();) {
	        Group g = (Group) g_iterator.next();
	        
			Object g_id = project_tree_container.addItem();
			project_tree_container.getContainerProperty(g_id, "icon").setValue(new ThemeResource("../runo/icons/16/folder.png"));
			project_tree_container.getContainerProperty(g_id, "caption").setValue(g.getName());
			project_tree_container.getContainerProperty(g_id, "type").setValue("group");
			
			//set the parent group, if there is one
			if(parentId != null)
			{
				project_tree_container.setParent(g_id, parentId);
			}
			
			//check for subgroups
			if(g.getSubgroups().size() > 0)
			{
				recursiveAddGroups(project_tree_container, g.getSubgroups(), g_id);
			}
	        
			//add iterations
	        for(Iterator<Iteration> i_iterator = g.getIterations().iterator(); i_iterator.hasNext();)
	        {
	        	Iteration i = (Iteration) i_iterator.next();
	        	
	        	Object i_id = project_tree_container.addItem();
				project_tree_container.getContainerProperty(i_id, "icon").setValue(new ThemeResource("../runo/icons/16/document.png"));
				project_tree_container.getContainerProperty(i_id, "caption").setValue(i.getName());
				project_tree_container.getContainerProperty(i_id, "type").setValue("iteration");
		        
				project_tree_container.setChildrenAllowed(i_id, false);
	        	project_tree_container.setParent(i_id, g_id);
	        }
		}
	}

And then:

project_tree.setContainerDataSource(tree_container);

When I remove an item from the tree, it gets removed from the container. But I also want it removed from the Project p .

I am probably missing something very easy - and it is true that I do not see how the tree container might know about th existence of the original groups Set… so, this is what I need help for.

Any tips are greatly appreciated :slight_smile:

Thank you,
Alex