Can CollapseRecursively collapse all but one current parent item?

Hi everyone,

Currently, I am testing out whether if it is possible to collapse all but the parent item that I am accessing. For example, if there are 5 parent items and I select a child item under parent item 2, the other parent items should collapse recursively.

I’ve attempted to add the recursive call to the existing portion of the code (in bold):

for (DashboardViewType type : DashboardViewType.values()) {
            mainEnabled = false;
            if(i <= mainACL.length()-1){
                if(mainACL.substring(i,i+1).equals("1")){
                    mainEnabled = true;
                }
                i++;
            }
            if((type.name().equalsIgnoreCase("SALES") 
                    || type.name().equalsIgnoreCase("SALES_PATTERN")
                    || type.name().equalsIgnoreCase("AUDIT")
                    || type.name().equalsIgnoreCase("TOP_SKUs")
                    || type.name().equalsIgnoreCase("CUSTOM")) && (mainEnabled == true)){
                TreeMenuItem menuItem = new TreeMenuItem(type);
                menuItem.setId("menu_"+type.name().toLowerCase());
                menuItem.setIcon(VaadinIcons.HOME_O);
                menuItem.setViewName(type.getViewName());
                menuItem.setStateful(false);
                menuItemList.add(menuItem);
                treeData.addItem(null, menuItem);
                getSubMenuItems(type).forEach((DashboardViewType subView) -> {
                    TreeMenuItem subItem = new TreeMenuItem(subView);
                    subItem.setId("subItem_"+subView.name().toLowerCase());
                    subItem.setViewName(subView.getViewName().substring(0, 1).toUpperCase()
                            + subView.getViewName().substring(1));
                    subItem.setIcon(subView.getIcon());
                    subItem.setStateful(subView.isStateful());
                    subItem.setView(subView);
                    subItem.setViewClass(subView.getViewClass());
                    treeData.addItem(menuItem, subItem);
                });
                tree.expand(menuItem);
            }
        }      
        
        
        tree.setDataProvider(new TreeDataProvider<>(treeData));
        tree.setStyleName("valo-menuitems");
        tree.setItemIconGenerator(TreeMenuItem::getIcon);
        tree.setItemCaptionGenerator(TreeMenuItem::getViewName);
        tree.addItemClickListener((Tree.ItemClick<TreeMenuItem> event) -> {
		    **parentItem = treeData.getParent(event.getItem());**
            DashboardEventBus.register(event.getItem().getView());
            UI.getCurrent().getNavigator().navigateTo(event.getItem().getViewName());
            //tree.collapseRecursively(menuItemList, 0);
        });
		
		 **tree.addExpandListener(event -> {
            for(TreeMenuItem menuItem : menuItemList){
                if(menuItem != parentItem){
                    tree.collapseRecursively(menuItemList, 0);
                }
            }
        });**

Currently, it throws an error: Invocation of method itemExpand in com.igi.sycarda.dashboard.view.DashboardMenu$$Lambda$382/224032987 failed.

So far there are not many sample usages of applications since the function was introduced in 8.4.0 not that long ago.

Thus I am unsure whether if collapseRecursively be handled to collapse per the required scenario.

Appreciate some help on this. Thanks.