Search in sources :

Example 16 with Menu

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

the class TerraMenuSkin method sectionInserted.

@Override
public void sectionInserted(Menu menu, int index) {
    Menu.Section section = menu.getSections().get(index);
    section.getSectionListeners().add(this);
    invalidateComponent();
}
Also used : Section(org.apache.pivot.wtk.Menu.Section) Menu(org.apache.pivot.wtk.Menu)

Example 17 with Menu

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

the class TerraMenuSkin method sectionsRemoved.

@Override
public void sectionsRemoved(Menu menu, int index, Sequence<Menu.Section> removed) {
    for (int i = 0, n = removed.getLength(); i < n; i++) {
        Menu.Section section = removed.get(i);
        section.getSectionListeners().remove(this);
    }
    invalidateComponent();
}
Also used : Section(org.apache.pivot.wtk.Menu.Section) Menu(org.apache.pivot.wtk.Menu)

Example 18 with Menu

use of org.apache.pivot.wtk.Menu 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 19 with Menu

use of org.apache.pivot.wtk.Menu 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 20 with Menu

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

the class TerraMenuPopupSkin method menuChanged.

@Override
public void menuChanged(MenuPopup menuPopup, Menu previousMenu) {
    if (previousMenu != null) {
        previousMenu.getMenuItemSelectionListeners().remove(menuItemSelectionListener);
    }
    Menu menu = menuPopup.getMenu();
    if (menu != null) {
        menu.getMenuItemSelectionListeners().add(menuItemSelectionListener);
    }
    panorama.setView(menu);
}
Also used : Menu(org.apache.pivot.wtk.Menu)

Aggregations

Menu (org.apache.pivot.wtk.Menu)21 Section (org.apache.pivot.wtk.Menu.Section)10 Item (org.apache.pivot.wtk.Menu.Item)8 Color (java.awt.Color)3 Display (org.apache.pivot.wtk.Display)3 Button (org.apache.pivot.wtk.Button)2 Component (org.apache.pivot.wtk.Component)2 Dimensions (org.apache.pivot.wtk.Dimensions)2 MenuPopup (org.apache.pivot.wtk.MenuPopup)2 Window (org.apache.pivot.wtk.Window)2 BasicStroke (java.awt.BasicStroke)1 Font (java.awt.Font)1 GradientPaint (java.awt.GradientPaint)1 BXMLSerializer (org.apache.pivot.beans.BXMLSerializer)1 Vote (org.apache.pivot.util.Vote)1 Action (org.apache.pivot.wtk.Action)1 Insets (org.apache.pivot.wtk.Insets)1 Keyboard (org.apache.pivot.wtk.Keyboard)1 MenuButton (org.apache.pivot.wtk.MenuButton)1 MenuHandler (org.apache.pivot.wtk.MenuHandler)1