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