Main menu

I See Main Window Layout in the documentation and extended AppMainWindow with my class.

Is there a way to enable or disable the menu item?

How do you want to do it - for some user or during the user session depending on some event?
In the first case, use security permissions in menu.xml. They can also be set for a menu item in Studio.

No, during the user session depending on some event.

It cannot be done?

Hi Francesco,

For now, you cannot change state of menu items at runtime. Only workaround I can suggest you is to use Runnable for your menu item and instead of opening screen to show notification about disabled functionality.

  1. Create runnable class
 
package com.company.demo.web.menu;

import com.company.demo.web.App;
import com.haulmont.cuba.core.global.AppBeans;
import com.haulmont.cuba.core.global.UserSessionSource;
import com.haulmont.cuba.gui.WindowManager;
import com.haulmont.cuba.gui.WindowManager.OpenType;
import com.haulmont.cuba.gui.components.Frame;
import com.haulmont.cuba.gui.config.WindowConfig;

public class ScreenRunnable implements Runnable {
    @Override
    public void run() {
        // obtain infrastructure interfaces
        UserSessionSource uss = AppBeans.get(UserSessionSource.NAME);
        WindowManager wm = App.getInstance().getWindowManager();

        String login = uss.getUserSession().getCurrentOrSubstitutedUser().getLogin();
        // check some business rule
        if ("admin".equals(login)) {
            // show deny notification
            wm.showNotification("Functionality is disabled", Frame.NotificationType.WARNING);
        } else {
            // or open the screen with id "screen"
            WindowConfig wc = AppBeans.get(WindowConfig.NAME);
            wm.openWindow(wc.getWindowInfo("screen"), OpenType.THIS_TAB);
        }
    }
}
  1. Register class as screen in web-screens.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<screen-config xmlns="http://schemas.haulmont.com/cuba/screens.xsd">
    <include file="/screens.xml"/>
    <screen id="screen"
            template="com/company/demo/web/screens/screen.xml"/>

    <screen id="screen-runnable"
            class="com.company.demo.web.menu.ScreenRunnable"/>
</screen-config>
  1. Add menu item to web-menu.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<menu-config xmlns="http://schemas.haulmont.com/cuba/menu.xsd">
    <menu id="application" insertBefore="administration">
        <item id="screen-runnable"/>
    </menu>
</menu-config>

I’ve prepared sample project with this approach, so you can see it in action.

We are planning to implement API for AppMenu items in one of the next minor releases.

menu-runnable.zip (21.8K)

Hi Francesco,

Since the platform version 6.5.0, there is an ability to manage application menu programmatically. See the documentation for details.