Search in sources :

Example 1 with Item

use of org.apache.pivot.wtk.Menu.Item in project pivot by apache.

the class TerraMenuSkin method getPreferredHeight.

@Override
public int getPreferredHeight(int width) {
    int preferredHeight = 0;
    Menu menu = (Menu) getComponent();
    Menu.SectionSequence sections = menu.getSections();
    for (int i = 0, n = sections.getLength(); i < n; i++) {
        Menu.Section section = sections.get(i);
        for (Menu.Item item : section) {
            if (item.isVisible()) {
                preferredHeight += item.getPreferredHeight(width);
            }
        }
        if (i > 0) {
            preferredHeight += sectionSpacing;
        }
    }
    return preferredHeight;
}
Also used : Item(org.apache.pivot.wtk.Menu.Item) Section(org.apache.pivot.wtk.Menu.Section) Menu(org.apache.pivot.wtk.Menu)

Example 2 with Item

use of org.apache.pivot.wtk.Menu.Item in project pivot by apache.

the class TerraMenuSkin method getPreferredWidth.

@Override
public int getPreferredWidth(int height) {
    int preferredWidth = 0;
    Menu menu = (Menu) getComponent();
    Menu.SectionSequence sections = menu.getSections();
    for (int i = 0, n = sections.getLength(); i < n; i++) {
        Menu.Section section = sections.get(i);
        for (Menu.Item item : section) {
            if (item.isVisible()) {
                preferredWidth = Math.max(item.getPreferredWidth(-1), preferredWidth);
            }
        }
    }
    return preferredWidth;
}
Also used : Item(org.apache.pivot.wtk.Menu.Item) Section(org.apache.pivot.wtk.Menu.Section) Menu(org.apache.pivot.wtk.Menu)

Example 3 with Item

use of org.apache.pivot.wtk.Menu.Item in project pivot by apache.

the class TerraMenuSkin method keyTyped.

/**
 * Select the next enabled menu item where the first character of the
 * rendered text matches the typed key (case insensitive).
 */
@Override
public boolean keyTyped(Component component, char character) {
    boolean consumed = super.keyTyped(component, character);
    Menu menu = (Menu) component;
    Menu.SectionSequence sections = menu.getSections();
    int sectionCount = sections.getLength();
    Menu.Item activeItem = menu.getActiveItem();
    int sectionIndex;
    int itemIndex;
    if (activeItem == null) {
        sectionIndex = 0;
        itemIndex = 0;
    } else {
        Menu.Section section = activeItem.getSection();
        sectionIndex = sections.indexOf(section);
        itemIndex = section.indexOf(activeItem) + 1;
    }
    char characterUpper = Character.toUpperCase(character);
    while (sectionIndex < sectionCount) {
        Section section = sections.get(sectionIndex);
        int sectionLength = section.getLength();
        while (itemIndex < sectionLength) {
            Item item = section.get(itemIndex);
            if (item.isEnabled()) {
                Button.DataRenderer itemDataRenderer = item.getDataRenderer();
                String string = itemDataRenderer.toString(item.getButtonData());
                if (string != null && string.length() > 0) {
                    char first = Character.toUpperCase(string.charAt(0));
                    if (first == characterUpper) {
                        item.setActive(true);
                        consumed = true;
                        break;
                    }
                }
            }
            itemIndex++;
        }
        if (itemIndex < sectionLength) {
            break;
        }
        sectionIndex++;
        itemIndex = 0;
    }
    return consumed;
}
Also used : Item(org.apache.pivot.wtk.Menu.Item) Button(org.apache.pivot.wtk.Button) Item(org.apache.pivot.wtk.Menu.Item) Section(org.apache.pivot.wtk.Menu.Section) Menu(org.apache.pivot.wtk.Menu) Section(org.apache.pivot.wtk.Menu.Section)

Example 4 with Item

use of org.apache.pivot.wtk.Menu.Item in project pivot by apache.

the class TerraMenuSkin method keyReleased.

/**
 * {@link KeyCode#SPACE SPACE} 'presses' the active menu item if it does not
 * have a sub-menu.
 */
@Override
public boolean keyReleased(Component component, int keyCode, Keyboard.KeyLocation keyLocation) {
    boolean consumed = super.keyReleased(component, keyCode, keyLocation);
    Menu menu = (Menu) component;
    if (keyCode == Keyboard.KeyCode.SPACE) {
        Menu.Item activeItem = menu.getActiveItem();
        // Press if the item does not have a sub-menu
        if (activeItem != null && activeItem.getMenu() == null) {
            activeItem.press();
            consumed = true;
        }
    }
    return consumed;
}
Also used : Item(org.apache.pivot.wtk.Menu.Item) Menu(org.apache.pivot.wtk.Menu)

Example 5 with Item

use of org.apache.pivot.wtk.Menu.Item in project pivot by apache.

the class TerraMenuSkin method layout.

@Override
public void layout() {
    Menu menu = (Menu) getComponent();
    Menu.SectionSequence sections = menu.getSections();
    int width = getWidth();
    int itemY = 0;
    for (int i = 0, n = sections.getLength(); i < n; i++) {
        Menu.Section section = sections.get(i);
        for (Menu.Item item : section) {
            if (item.isVisible()) {
                item.setSize(width, item.getPreferredHeight(width));
                item.setLocation(0, itemY);
                itemY += item.getHeight();
            }
        }
        itemY += sectionSpacing;
    }
}
Also used : Item(org.apache.pivot.wtk.Menu.Item) Section(org.apache.pivot.wtk.Menu.Section) Menu(org.apache.pivot.wtk.Menu)

Aggregations

Menu (org.apache.pivot.wtk.Menu)8 Item (org.apache.pivot.wtk.Menu.Item)8 Section (org.apache.pivot.wtk.Menu.Section)7 Button (org.apache.pivot.wtk.Button)1 Dimensions (org.apache.pivot.wtk.Dimensions)1 Window (org.apache.pivot.wtk.Window)1