Tree: Update a component inside TreeData (nor add or remove)

Hello,

I’m struggling to find a solution updating a value within an object that is in a Tree.

My main Object is a ModuleComponent:

It can be a ModuleContainer or a ModuleItem. If it’s a ModuleContainer is a tree parent, if it’s a ModuleItem, an tree leaf.

Despite being an ModuleItem or a ModuleContainer, it keeps a DTO inside it with the description and checked/unchecked state. When I click on an ModuleItem, a component is attached to the screen that contains a button for checking or unchecking the state of the DTO.

Some of the items are dependant on other items, so every time I check/uncheck, I have to verify the state for enable/disabled the dependent item on the tree.

So the tree it’s a collection of ModuleComponents

[ModuleContainer]
 PREGUNTA DE INVESTIGACION
		 > [ModuleItem]
1.1 RESULTADOS CENTRADOS EN EL PACIENTE: RICHTEXTAREA
		 > [ModuleItem]
1.2 RESULTADOS RELEVANTES PARA CLÍNICOS: RICHTEXTAREA
	 > [CONTAINER]
1.3 EVIDENCIA DISPONIBLE
		 > [ModuleItem]
1.3.1 Pregunta de investigación: RICHTEXTAREA
		 > [ModuleItem]
1.3.2 Estrategia de búsqueda : RICHTEXTAREA
		 > [ModuleItem]
1.3.3 Términos de búsqueda utilizados: RICHTEXTAREA
		 > [ModuleItem]
1.3.4 Estrategia búsqueda realizada: FILEUPLOADER
		 > [ModuleItem]
1.3.5 Introducción: RICHTEXTAREA
		 > [ModuleItem]
1.3.6 Bibliografía : RICHTEXTAREA
	> [ModuleItem]
1.4 VALORACIÓN DEL IMPACTO: RICHTEXTAREA

[ModuleContainer]
2 GRUPO INVESTIGADOR
		 > [ModuleItem]
2.1 ÁREAS PRIORITARIAS: RICHTEXTAREA
		 > [ModuleItem]
2.2 UNI O MULTICÉNTRICO: RICHTEXTAREA
	 > [CONTAINER]
2.3 COMPOSICIÓN EQUIPO INVESTIGADOR
		 > [ModuleItem]
2.3.1 Investigadores/as jóvenes: RICHTEXTAREA
		 > [ModuleItem]
2.3.2 Mujeres investigadoras: RICHTEXTAREA
		 > [ModuleItem]
2.3.3 Curriculum: RICHTEXTAREA
		 > [ModuleItem]
2.3.4 Conflicto de interés: RICHTEXTAREA
	> [ModuleItem]
2.4 EXPERIENCIA DEL GRUPO: RICHTEXTAREA

For example, if I validate 1.3.6 Bibliografía , I have to verify that all the items under 1. PREGUNTA DE INVESTIGACION are checked, and if they are, 2. GRUPO INVESTIGADOR will be accesible.

I have:

@Getter TreeData<ModuleComponent> treeMenuData = new TreeData<>();
@Getter TreeDataProvider<ModuleComponent> treeDataProvider = new TreeDataProvider<>(treeMenuData);

After cheking/unchecking, I refresh the item inside the data provider:

treeDataProvider.refreshItem((ModuleComponent)forRefreshing);

and after that, I get the children to check the status of the component, but the status is always the one I added when I created the TreeData.

List<ModuleComponent> children = moduleTreeMenu.getTreeMenuData().getChildren(parent);

Inside the TreeDataProvider, the object has been updated because I can see that a style has changed, but…
how can I update a component inside a TreeData??

There’s something I’m missing or I don’t understand… so I would appreciate your help.

Thanks in advance,

Iban

Hi,
Seems that this is a missing feature that Vaadin team must add to cover all CRUD operation on TreeData model.

Workaround (horrible, but we do not have access to “itemToWrapperMap” and the private class HierarchyWrapper to write 3 lines…):

/** replace item in TreeData, return replaced item or null if not replaced */
public T replaceItem(T item) {
	if (item != null) {
		T parent = getTreeData().getParent(item);
		List<T> items = getTreeData().getChildren(parent);			
		List<T> childItems = new ArrayList<>(getTreeData().getChildren(item));

		T sibling = null;
		for (T child : items) {
			if (child.equals(item)) {
				

				// remove original item and add new one after last found sibling
				getTreeData().removeItem(child);
				
				getTreeData().addItem(parent, item);
				getTreeData().addItems(item, childItems);
				
				getTreeData().moveAfterSibling(item, sibling);
				
				
				return child;
			} else {
				sibling = child;
			}
		}
	}
	return null;
}