Author: [email protected]
Date: Wed Apr 29 13:23:39 2009
New Revision: 5308
Modified:
trunk/user/src/com/google/gwt/user/client/ui/MenuBar.java
trunk/user/src/com/google/gwt/user/client/ui/MenuItem.java
Log:
Exposing MenuBar.moveSelectionUp/Down (formerly just moveUp/Down), and
making MenuBar.selectFirstItemIfNoneSelected robust for empty menus, rather
than just assuming they aren't. Also made MenuItem.setStyleSelected
protected for out-of-package test access, and
Review by: jlabanca
Modified: trunk/user/src/com/google/gwt/user/client/ui/MenuBar.java
==============================================================================
--- trunk/user/src/com/google/gwt/user/client/ui/MenuBar.java (original)
+++ trunk/user/src/com/google/gwt/user/client/ui/MenuBar.java Wed Apr 29
13:23:39 2009
@@ -429,6 +429,53 @@
return isAnimationEnabled;
}
+ /**
+ * Moves the menu selection down to the next item. If there is no
selection,
+ * selects the first item. If there are no items at all, does nothing.
+ */
+ public void moveSelectionDown() {
+ if (selectFirstItemIfNoneSelected()) {
+ return;
+ }
+
+ if (vertical) {
+ selectNextItem();
+ } else {
+ if (selectedItem.getSubMenu() != null
+ && !selectedItem.getSubMenu().getItems().isEmpty()
+ && (shownChildMenu == null || shownChildMenu.getSelectedItem()
== null)) {
+ if (shownChildMenu == null) {
+ doItemAction(selectedItem, false);
+ }
+ selectedItem.getSubMenu().focus();
+ } else if (parentMenu != null) {
+ if (parentMenu.vertical) {
+ parentMenu.selectNextItem();
+ } else {
+ parentMenu.moveSelectionDown();
+ }
+ }
+ }
+ }
+
+ /**
+ * Moves the menu selection up to the previous item. If there is no
selection,
+ * selects the first item. If there are no items at all, does nothing.
+ */
+ public void moveSelectionUp() {
+ if (selectFirstItemIfNoneSelected()) {
+ return;
+ }
+
+ if ((shownChildMenu == null) && vertical) {
+ selectPrevItem();
+ } else if ((parentMenu != null) && parentMenu.vertical) {
+ parentMenu.selectPrevItem();
+ } else {
+ close();
+ }
+ }
+
@Override
public void onBrowserEvent(Event event) {
MenuItem item = findItem(DOM.eventGetTarget(event));
@@ -481,11 +528,11 @@
eatEvent(event);
break;
case KeyCodes.KEY_UP:
- moveUp();
+ moveSelectionUp();
eatEvent(event);
break;
case KeyCodes.KEY_DOWN:
- moveDown();
+ moveSelectionDown();
eatEvent(event);
break;
case KeyCodes.KEY_ESCAPE:
@@ -891,31 +938,6 @@
DOM.setElementAttribute(getElement(), "hideFocus", "true");
}
- private void moveDown() {
- if (selectFirstItemIfNoneSelected()) {
- return;
- }
-
- if (vertical) {
- selectNextItem();
- } else {
- if (selectedItem.getSubMenu() != null
- && !selectedItem.getSubMenu().getItems().isEmpty()
- && (shownChildMenu == null || shownChildMenu.getSelectedItem()
== null)) {
- if (shownChildMenu == null) {
- doItemAction(selectedItem, false);
- }
- selectedItem.getSubMenu().focus();
- } else if (parentMenu != null) {
- if (parentMenu.vertical) {
- parentMenu.selectNextItem();
- } else {
- parentMenu.moveDown();
- }
- }
- }
- }
-
private void moveToNextItem() {
if (selectFirstItemIfNoneSelected()) {
return;
@@ -957,20 +979,6 @@
}
}
- private void moveUp() {
- if (selectFirstItemIfNoneSelected()) {
- return;
- }
-
- if ((shownChildMenu == null) && vertical) {
- selectPrevItem();
- } else if ((parentMenu != null) && parentMenu.vertical) {
- parentMenu.selectPrevItem();
- } else {
- close();
- }
- }
-
/*
* This method is called when a menu bar is hidden, so that it can hide
any
* child popups that are currently being shown.
@@ -1097,19 +1105,19 @@
}
/**
- * Selects the first item in the menu if no items are currently
selected. This
- * method assumes that the menu has at least 1 item.
+ * Selects the first item in the menu if no items are currently selected.
+ * Has no effect if there are no items.
*
- * @return true if no item was previously selected and the first item in
the
- * list was selected, false otherwise
+ * @return true if no item was previously selected, false otherwise
*/
private boolean selectFirstItemIfNoneSelected() {
if (selectedItem == null) {
- MenuItem nextItem = items.get(0);
- selectItem(nextItem);
+ if (items.size() > 0) {
+ MenuItem nextItem = items.get(0);
+ selectItem(nextItem);
+ }
return true;
}
-
return false;
}
Modified: trunk/user/src/com/google/gwt/user/client/ui/MenuItem.java
==============================================================================
--- trunk/user/src/com/google/gwt/user/client/ui/MenuItem.java (original)
+++ trunk/user/src/com/google/gwt/user/client/ui/MenuItem.java Wed Apr 29
13:23:39 2009
@@ -181,15 +181,15 @@
}
}
- void setParentMenu(MenuBar parentMenu) {
- this.parentMenu = parentMenu;
- }
-
- void setSelectionStyle(boolean selected) {
+ protected void setSelectionStyle(boolean selected) {
if (selected) {
addStyleDependentName(DEPENDENT_STYLENAME_SELECTED_ITEM);
} else {
removeStyleDependentName(DEPENDENT_STYLENAME_SELECTED_ITEM);
}
+ }
+
+ void setParentMenu(MenuBar parentMenu) {
+ this.parentMenu = parentMenu;
}
}
--~--~---------~--~----~------------~-------~--~----~
http://groups.google.com/group/Google-Web-Toolkit-Contributors
-~----------~----~----~----~------~----~------~--~---