Example usage for com.vaadin.client.ui VMenuBar getSelected

List of usage examples for com.vaadin.client.ui VMenuBar getSelected

Introduction

In this page you can find the example usage for com.vaadin.client.ui VMenuBar getSelected.

Prototype

public CustomMenuItem getSelected() 

Source Link

Document

Returns the currently selected item of this menu, or null if nothing is selected.

Usage

From source file:com.haulmont.cuba.web.widgets.client.addons.contextmenu.MyVMenuBar.java

License:Apache License

@Override
public boolean handleNavigation(int keycode, boolean ctrl, boolean shift) {

    // If tab or shift+tab close menus
    if (keycode == KeyCodes.KEY_TAB) {
        setSelected(null);//w w  w  . j  av a 2 s.co m
        hideChildren();
        menuVisible = false;
        return false;
    }

    if (ctrl || shift || !enabled) {
        // Do not handle tab key, nor ctrl keys
        return false;
    }

    if (keycode == getNavigationLeftKey()) {
        if (getSelected() == null) {
            // If nothing is selected then select the last item
            setSelected(items.get(items.size() - 1));
            if (!getSelected().isSelectable()) {
                handleNavigation(keycode, ctrl, shift);
            }
        } else if (visibleChildMenu == null && getParentMenu() == null) {
            // If this is the root menu then move to the left
            int idx = items.indexOf(getSelected());
            if (idx > 0) {
                setSelected(items.get(idx - 1));
            } else {
                setSelected(items.get(items.size() - 1));
            }

            if (!getSelected().isSelectable()) {
                handleNavigation(keycode, ctrl, shift);
            }
        } else if (visibleChildMenu != null) {
            // Redirect all navigation to the submenu
            visibleChildMenu.handleNavigation(keycode, ctrl, shift);

        } else if (getParentMenu().getParentMenu() == null) {

            // FIXME: this line added
            if (isContextMenu) {
                return true;
            }

            // Inside a sub menu, whose parent is a root menu item
            VMenuBar root = getParentMenu();

            root.getSelected().getSubMenu().setSelected(null);
            // #15255 - disable animate-in/out when hide popup
            root.hideChildren(false, false);

            // Get the root menus items and select the previous one
            int idx = root.getItems().indexOf(root.getSelected());
            idx = idx > 0 ? idx : root.getItems().size();
            CustomMenuItem selected = root.getItems().get(--idx);

            while (selected.isSeparator() || !selected.isEnabled()) {
                idx = idx > 0 ? idx : root.getItems().size();
                selected = root.getItems().get(--idx);
            }

            root.setSelected(selected);
            openMenuAndFocusFirstIfPossible(selected);
        } else {
            getParentMenu().getSelected().getSubMenu().setSelected(null);
            getParentMenu().hideChildren();
        }

        return true;

    } else if (keycode == getNavigationRightKey()) {

        if (getSelected() == null) {
            // If nothing is selected then select the first item
            setSelected(items.get(0));
            if (!getSelected().isSelectable()) {
                handleNavigation(keycode, ctrl, shift);
            }
        } else if (visibleChildMenu == null && getParentMenu() == null) {
            // If this is the root menu then move to the right
            int idx = items.indexOf(getSelected());

            if (idx < items.size() - 1) {
                setSelected(items.get(idx + 1));
            } else {
                setSelected(items.get(0));
            }

            if (!getSelected().isSelectable()) {
                handleNavigation(keycode, ctrl, shift);
            }
        } else if (visibleChildMenu == null && getSelected().getSubMenu() != null) {
            // If the item has a submenu then show it and move the selection
            // there
            showChildMenu(getSelected());
            menuVisible = true;
            visibleChildMenu.handleNavigation(keycode, ctrl, shift);
        } else if (visibleChildMenu == null && !isContextMenu /* FIXME */) {

            // Get the root menu
            VMenuBar root = getParentMenu();
            while (root.getParentMenu() != null) {
                root = root.getParentMenu();
            }

            // Hide the submenu (#15255 - disable animate-in/out when hide
            // popup)
            root.hideChildren(false, false);

            // Get the root menus items and select the next one
            int idx = root.getItems().indexOf(root.getSelected());
            idx = idx < root.getItems().size() - 1 ? idx : -1;
            CustomMenuItem selected = root.getItems().get(++idx);

            while (selected.isSeparator() || !selected.isEnabled()) {
                idx = idx < root.getItems().size() - 1 ? idx : -1;
                selected = root.getItems().get(++idx);
            }

            root.setSelected(selected);
            openMenuAndFocusFirstIfPossible(selected);
        } else if (visibleChildMenu != null) {
            // Redirect all navigation to the submenu
            visibleChildMenu.handleNavigation(keycode, ctrl, shift);
        }

        return true;

    } else if (keycode == getNavigationUpKey()) {

        if (getSelected() == null) {
            // If nothing is selected then select the last item
            setSelected(items.get(items.size() - 1));
            if (!getSelected().isSelectable()) {
                handleNavigation(keycode, ctrl, shift);
            }
        } else if (visibleChildMenu != null) {
            // Redirect all navigation to the submenu
            visibleChildMenu.handleNavigation(keycode, ctrl, shift);
        } else {
            // Select the previous item if possible or loop to the last item
            int idx = items.indexOf(getSelected());
            if (idx > 0) {
                setSelected(items.get(idx - 1));
            } else {
                setSelected(items.get(items.size() - 1));
            }

            if (!getSelected().isSelectable()) {
                handleNavigation(keycode, ctrl, shift);
            }
        }

        return true;

    } else if (keycode == getNavigationDownKey()) {

        if (getSelected() == null) {
            // If nothing is selected then select the first item
            selectFirstItem();
        } else if (visibleChildMenu == null && getParentMenu() == null) {
            // If this is the root menu the show the child menu with arrow
            // down, if there is a child menu
            openMenuAndFocusFirstIfPossible(getSelected());
        } else if (visibleChildMenu != null) {
            // Redirect all navigation to the submenu
            visibleChildMenu.handleNavigation(keycode, ctrl, shift);
        } else {
            // Select the next item if possible or loop to the first item
            int idx = items.indexOf(getSelected());
            if (idx < items.size() - 1) {
                setSelected(items.get(idx + 1));
            } else {
                setSelected(items.get(0));
            }

            if (!getSelected().isSelectable()) {
                handleNavigation(keycode, ctrl, shift);
            }
        }
        return true;

    } else if (keycode == getCloseMenuKey()) {
        setSelected(null);
        hideChildren();
        menuVisible = false;

    } else if (isNavigationSelectKey(keycode)) {
        if (getSelected() == null) {
            // If nothing is selected then select the first item
            selectFirstItem();
        } else if (visibleChildMenu != null) {
            // Redirect all navigation to the submenu
            visibleChildMenu.handleNavigation(keycode, ctrl, shift);
            menuVisible = false;
        } else if (visibleChildMenu == null && getSelected().getSubMenu() != null) {
            // If the item has a sub menu then show it and move the
            // selection there
            openMenuAndFocusFirstIfPossible(getSelected());
        } else {
            final Command command = getSelected().getCommand();

            setSelected(null);
            hideParents(true);

            // #17076 keyboard selected menuitem without children: do
            // not leave menu to visible ("hover open") mode
            menuVisible = false;

            Scheduler.get().scheduleDeferred(new ScheduledCommand() {
                @Override
                public void execute() {
                    if (command != null) {
                        command.execute();
                    }
                }
            });
        }
    }

    return false;
}