Search in sources :

Example 1 with Menu

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

the class Pivot765 method startup.

@Override
public void startup(final Display display, Map<String, String> properties) throws Exception {
    final MenuButton button = new MenuButton();
    button.setButtonData("Populate menu and open!");
    Window window = new Window(button);
    button.getListPopup().getWindowStateListeners().add(new WindowStateListener() {

        @Override
        public Vote previewWindowOpen(Window windowArgument) {
            Menu menu = new Menu();
            Menu.Section section = new Menu.Section();
            menu.getSections().add(section);
            section.add(new Menu.Item("A dynamically added menu item"));
            button.setMenu(menu);
            menuPopulated = true;
            return Vote.APPROVE;
        }

        @Override
        public void windowOpened(Window windowArgument) {
            if (!menuPopulated) {
                Alert.alert("Window was opened before the menu was populated." + "Either previewWindowOpen threw an exception, or it wasn't called before the Window was opened.", windowArgument);
            }
        }

        @Override
        public void windowClosed(Window windowArgument, Display displayArgument, Window owner) {
            // Remove menu for subsequent open attempt
            button.setMenu(null);
            menuPopulated = false;
        }
    });
    window.open(display);
}
Also used : Window(org.apache.pivot.wtk.Window) Vote(org.apache.pivot.util.Vote) WindowStateListener(org.apache.pivot.wtk.WindowStateListener) MenuButton(org.apache.pivot.wtk.MenuButton) Menu(org.apache.pivot.wtk.Menu) Display(org.apache.pivot.wtk.Display)

Example 2 with Menu

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

the class TerraMenuSkin method install.

@Override
public void install(Component component) {
    super.install(component);
    Menu menu = (Menu) component;
    menu.getMenuListeners().add(this);
    for (Menu.Section section : menu.getSections()) {
        section.getSectionListeners().add(this);
    }
    menu.setFocusTraversalPolicy(new IndexFocusTraversalPolicy(true));
}
Also used : Section(org.apache.pivot.wtk.Menu.Section) Menu(org.apache.pivot.wtk.Menu)

Example 3 with Menu

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

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

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

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