I have couple of questions about menu.
- Can we have more than one Manu and select programatically based on user and specific default data selected e.g. Company?
- How can we remove, add menu items programmatically?
I have couple of questions about menu.
Hi,
It is not yet supported. There is similar topic with some details: Main menu - CUBA.Platform We have created a YouTrack issue, see the link on the right.
Now menu can be only build using XML menu definition. It is managed by secured permissions, so if you need to hide some menus/items depending on security role then use screen permissions.
If you want to build your menu programmatically you can extend main window, unwrap Vaadin MenuBar component and use its API:
public class ExtAppMainWindow extends AppMainWindow {
@Override
public void init(Map<String, Object> params) {
super.init(params);
MenuBar menu = mainMenu.unwrap(MenuBar.class);
menu.addItem("Custom item",
selectedItem -> showNotification("Custom action")
);
}
}
Or you can create custom MenuBar from scratch:
public class ExtAppMainWindow extends AppMainWindow {
@Override
public void init(Map<String, Object> params) {
super.init(params);
// hide CUBA menu
mainMenu.setVisible(false);
AbstractOrderedLayout vTitleBar = titleBar.unwrap(AbstractOrderedLayout.class);
// add custom menu
MenuBar customMenu = new MenuBar();
customMenu.addStyleName("cuba-main-menu");
customMenu.setWidth("100%");
customMenu.addItem("First menu", (MenuBar.Command) selectedItem -> { });
customMenu.addItem("Second menu", (MenuBar.Command) selectedItem -> { });
vTitleBar.addComponent(customMenu, 1);
vTitleBar.setExpandRatio(customMenu, 1);
}
}