Example usage for javax.swing MenuSelectionManager defaultManager

List of usage examples for javax.swing MenuSelectionManager defaultManager

Introduction

In this page you can find the example usage for javax.swing MenuSelectionManager defaultManager.

Prototype

public static MenuSelectionManager defaultManager() 

Source Link

Document

Returns the default menu selection manager.

Usage

From source file:ToggleSample.java

private MenuElement[] getPath() {
    MenuSelectionManager menuSelectionManager = MenuSelectionManager.defaultManager();
    MenuElement oldPath[] = menuSelectionManager.getSelectedPath();
    MenuElement newPath[];//  w w  w.ja  va2s. c  o m
    int oldPathLength = oldPath.length;
    if (oldPathLength == 0)
        return new MenuElement[0];
    Component parent = getParent();
    if (oldPath[oldPathLength - 1].getComponent() == parent) {
        // Going deeper under the parent menu
        newPath = new MenuElement[oldPathLength + 1];
        System.arraycopy(oldPath, 0, newPath, 0, oldPathLength);
        newPath[oldPathLength] = this;
    } else {
        // Sibling/child menu item currently selected
        int newPathPosition;
        for (newPathPosition = oldPath.length - 1; newPathPosition >= 0; newPathPosition--) {
            if (oldPath[newPathPosition].getComponent() == parent) {
                break;
            }
        }
        newPath = new MenuElement[newPathPosition + 2];
        System.arraycopy(oldPath, 0, newPath, 0, newPathPosition + 1);
        newPath[newPathPosition + 1] = this;
    }
    return newPath;
}

From source file:forge.screens.match.CMatchUI.java

@Override
public SpellAbility getAbilityToPlay(List<SpellAbility> abilities, ITriggerEvent triggerEvent) {
    if (triggerEvent == null) {
        if (abilities.isEmpty()) {
            return null;
        }//from w  w  w  .j  a  va  2  s  .  co m
        if (abilities.size() == 1) {
            return abilities.get(0);
        }
        return GuiChoose.oneOrNone("Choose ability to play", abilities);
    }

    if (abilities.isEmpty()) {
        return null;
    }
    if (abilities.size() == 1 && !abilities.get(0).promptIfOnlyPossibleAbility()) {
        if (abilities.get(0).canPlay()) {
            return abilities.get(0); //only return ability if it's playable, otherwise return null
        }
        return null;
    }

    //show menu if mouse was trigger for ability
    final JPopupMenu menu = new JPopupMenu("Abilities");

    boolean enabled;
    boolean hasEnabled = false;
    int shortcut = KeyEvent.VK_1; //use number keys as shortcuts for abilities 1-9
    for (final SpellAbility ab : abilities) {
        enabled = ab.canPlay();
        if (enabled) {
            hasEnabled = true;
        }
        GuiUtils.addMenuItem(menu, FSkin.encodeSymbols(ab.toString(), true),
                shortcut > 0 ? KeyStroke.getKeyStroke(shortcut, 0) : null, new Runnable() {
                    @Override
                    public void run() {
                        CPrompt.SINGLETON_INSTANCE.selectAbility(ab);
                    }
                }, enabled);
        if (shortcut > 0) {
            shortcut++;
            if (shortcut > KeyEvent.VK_9) {
                shortcut = 0; //stop adding shortcuts after 9
            }
        }
    }
    if (hasEnabled) { //only show menu if at least one ability can be played
        SwingUtilities.invokeLater(new Runnable() { //use invoke later to ensure first ability selected by default
            public void run() {
                MenuSelectionManager.defaultManager()
                        .setSelectedPath(new MenuElement[] { menu, menu.getSubElements()[0] });
            }
        });
        MouseEvent mouseEvent = ((MouseTriggerEvent) triggerEvent).getMouseEvent();
        menu.show(mouseEvent.getComponent(), mouseEvent.getX(), mouseEvent.getY());
    }

    return null; //delay ability until choice made
}