Manipulating Menu Items

I have couple of questions about menu.

  1. Can we have more than one Manu and select programatically based on user and specific default data selected e.g. Company?
  2. How can we remove, add menu items programmatically?
1 Like

Hi,

It is not yet supported. There is similar topic with some details: https://www.cuba-platform.com/discuss/t/main-menu 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);
    }
}

:ticket: See the following issue in our bug tracker:

https://youtrack.cuba-platform.com/issue/PL-7492