Search in sources :

Example 6 with Item

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

the class TerraMenuSkin method keyPressed.

/**
 * {@link KeyCode#UP UP} Select the previous enabled menu item.<br>
 * {@link KeyCode#DOWN DOWN} Select the next enabled menu item.<br>
 * {@link KeyCode#LEFT LEFT} Close the current sub-menu.<br>
 * {@link KeyCode#RIGHT RIGHT} Open the sub-menu of the current menu
 * item.<br> {@link KeyCode#ENTER ENTER} 'presses' the active menu item if
 * it does not have a sub-menu.
 */
@Override
public boolean keyPressed(Component component, int keyCode, Keyboard.KeyLocation keyLocation) {
    boolean consumed = super.keyPressed(component, keyCode, keyLocation);
    Menu menu = (Menu) component;
    if (keyCode == Keyboard.KeyCode.UP) {
        Menu.SectionSequence sections = menu.getSections();
        int sectionCount = sections.getLength();
        Menu.Item activeItem = menu.getActiveItem();
        int sectionIndex;
        int itemIndex;
        if (activeItem == null) {
            sectionIndex = sectionCount - 1;
            itemIndex = -1;
        } else {
            Menu.Section section = activeItem.getSection();
            sectionIndex = sections.indexOf(section);
            itemIndex = section.indexOf(activeItem) - 1;
            if (itemIndex == -1) {
                sectionIndex--;
            }
        }
        while (sectionIndex >= 0) {
            Section section = sections.get(sectionIndex);
            if (itemIndex == -1) {
                int sectionLength = section.getLength();
                itemIndex = sectionLength - 1;
            }
            while (itemIndex >= 0) {
                Item item = section.get(itemIndex);
                if (item.isEnabled()) {
                    item.setActive(true);
                    break;
                }
                itemIndex--;
            }
            if (itemIndex >= 0) {
                break;
            }
            sectionIndex--;
        }
        consumed = true;
    } else if (keyCode == Keyboard.KeyCode.DOWN) {
        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;
        }
        while (sectionIndex < sectionCount) {
            Section section = sections.get(sectionIndex);
            int sectionLength = section.getLength();
            while (itemIndex < sectionLength) {
                Item item = section.get(itemIndex);
                if (item.isEnabled()) {
                    item.setActive(true);
                    break;
                }
                itemIndex++;
            }
            if (itemIndex < sectionLength) {
                break;
            }
            sectionIndex++;
            itemIndex = 0;
        }
        consumed = true;
    } else if (keyCode == Keyboard.KeyCode.LEFT) {
        // Close the window if this is not a top-level menu
        if (menu.getItem() != null) {
            Window window = menu.getWindow();
            window.close();
            consumed = true;
        }
    } else if (keyCode == Keyboard.KeyCode.RIGHT) {
        Menu.Item activeItem = menu.getActiveItem();
        // Press if the item has a sub-menu
        if (activeItem != null && activeItem.getMenu() != null) {
            activeItem.press();
            consumed = true;
        }
    } else if (keyCode == Keyboard.KeyCode.ENTER) {
        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;
        }
    } else if (keyCode == Keyboard.KeyCode.TAB) {
        consumed = false;
    }
    return consumed;
}
Also used : Window(org.apache.pivot.wtk.Window) Item(org.apache.pivot.wtk.Menu.Item) 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 7 with Item

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

the class TerraMenuSkin method getPreferredSize.

@Override
public Dimensions getPreferredSize() {
    int preferredWidth = 0;
    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()) {
                preferredWidth = Math.max(item.getPreferredWidth(), preferredWidth);
                preferredHeight += item.getPreferredHeight();
            }
        }
        if (i > 0) {
            preferredHeight += sectionSpacing;
        }
    }
    return new Dimensions(preferredWidth, preferredHeight);
}
Also used : Item(org.apache.pivot.wtk.Menu.Item) Dimensions(org.apache.pivot.wtk.Dimensions) Section(org.apache.pivot.wtk.Menu.Section) Menu(org.apache.pivot.wtk.Menu)

Example 8 with Item

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

the class TerraMenuSkin method paint.

@Override
public void paint(Graphics2D graphics) {
    super.paint(graphics);
    Menu menu = (Menu) getComponent();
    int width = getWidth();
    int height = getHeight();
    // Paint the margin
    if (marginColor != null) {
        graphics.setColor(marginColor);
        graphics.fillRect(0, 0, margin, height);
    }
    Menu.SectionSequence sections = menu.getSections();
    for (int i = 0, n = sections.getLength(); i < n; i++) {
        Menu.Section section = sections.get(i);
        if (section.getLength() > 0) {
            Menu.Item item = section.get(section.getLength() - 1);
            int separatorY = item.getY() + item.getHeight() + sectionSpacing / 2;
            // Paint the line
            graphics.setColor(separatorColor);
            graphics.drawLine(1, separatorY, width - 2, separatorY);
        }
    }
}
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