Hi,
I am currently evaluating the Vaadin Tree Grid.
I would like it to behave as a file explorer of an OS like windows:
- All nodes should be selectable
- Images of the Folders should change when they are opened
My Code so far:
private TreeGrid<TreeItem> createTree() {
TreeGrid<TreeItem> grid = new TreeGrid<>();
grid.addComponentHierarchyColumn((ValueProvider<TreeItem, Component>)
treeItem -> createNameComponent(grid, treeItem));
grid.addSelectionListener(event -> showDetails(new SqlSelectAction()));
grid.setHeightFull();
grid.setMaxWidth("300px");
grid.addThemeVariants(GridVariant.LUMO_NO_ROW_BORDERS);
// GridVariant.LUMO_ROW_STRIPES; alternate rows
HierarchicalDataProvider<TreeItem, Void> dataProvider =
new AbstractBackEndHierarchicalDataProvider<TreeItem, Void>() {
@Override
public int getChildCount(HierarchicalQuery<TreeItem, Void> query) {
return treeService.getChildCount(query.getParent());
}
@Override
public boolean hasChildren(TreeItem item) {
return treeService.hasChildren(item);
}
@Override
protected Stream<TreeItem> fetchChildrenFromBackEnd(
HierarchicalQuery<TreeItem, Void> query) {
return treeService.fetchChildren(query.getParent()).stream();
}
};
grid.setDataProvider(dataProvider);
return grid;
}
private Component createNameComponent(TreeGrid<TreeItem> grid, TreeItem treeItem) {
ListItem item = new ListItem(treeItem.getName());
item.setPadding(Vertical.XS);
item.setPrefix(treeItem.getIconComponent(grid.isExpanded(treeItem)));
item.setSpacing(Right.M);
return item;
}
This gives me the tree grid with icons.
Unfortunately, the Method createNameComponent is called only on item creation, therefore the expanded property is always false.
Is there any way to force the grid to refresh? This would also be a requirement if the name of an item would change.
Another problem is the selection of items with children.
Leaf items can be selected without any problems, others just toggle expand/collapse state on click, but do no set a selection.
Is it possible to change this behaviour?