Slight revision to my code:  Removing the "selected" style directly
from the MenuItem was causing too many bad side-effects.  What really
needs to happen, is a call to "void selectItem(MenuItem item) { ... }"
on MenuBar.  Unfortunately, that method's visibility doesn't allow
such a call.  Unless, of course, you're willing to bend a few
rules.  ;)

Menu bar = new MenuBar() {
        @Override
        public void onBrowserEvent(Event event) {
                super.onBrowserEvent(event);
                if (DOM.eventGetType(event) == Event.ONMOUSEOUT) {
                        Element barEle = getElement();
                        Element outEle = DOM.eventGetFromElement(event);
                        Element toEle = DOM.eventGetToElement(event);
                        if (DOM.isOrHasChild(barEle, outEle) && 
!DOM.isOrHasChild(barEle,
toEle)) {
                                //      Focus has shifted off of the MenuBar.  
But is it now on a sub-
menu?
                                boolean onSubMenu = false;
                                for (MenuItem mi : this.getItems()) {
                                        MenuBar subMenu = mi.getSubMenu();
                                        Widget parent = subMenu == null ? null 
: subMenu.getParent();
                                        Element parEle = parent == null ? null 
: parent.getElement();
                                        if ((parEle != null) && 
(DOM.isOrHasChild(parEle, toEle))) {
                                                onSubMenu = true;
                                                break;
                                        }
                                }
                                if (!onSubMenu) {
                                        MenuItem mi = getSelectedItem();
                                        if (mi != null) {
                                                unselectItem();
                                        }
                                }
                        }
                }
        }
        @Override
        public void onPopupClosed(PopupPanel sender, boolean autoClosed) {
                MenuItem mi = getSelectedItem();
                if (mi != null) {
                        //      Is the closed Popup the currently selected menu 
item?
                        if (mi.getSubMenu() == sender.getWidget()) {
                                unselectItem();
                        }
                }
                super.onPopupClosed(sender, autoClosed);
        }
        private native String unselectItem() /*-{
           [email protected]::selectItem(Lcom/
google/gwt/user/client/ui/MenuItem;)(null);
        }-*/;
};

Hope this helps someone else,

john...

PS.  The only scenario I have found where the above does not work is
when the user repeatedly clicks a menu item to open it's sub-menu over
and over again.


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Google Web Toolkit" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/Google-Web-Toolkit?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to