Search code examples
javagwtuibinder

GWT prevent popup panel from opening


I have a MenuItem that has a ScheduledCommand attached. When the user clicks on the menu, a new PopupPanel appears that has autoHide enabled. Now when the user clicks on the MenuItem while the popup is open, the panel gets closed, but immediately opens again as the PopupPanel's close event fires as a click event on the menu item. Can somebody tell me how can I prevent the PopupPanel from opening in this case?

My code is something like this:

@UiField
protected MenuItem menuItem;

....

    menuItem.setScheduledCommand(new ScheduledCommand() {
        @Override
        public void execute() {
            PopupPanel window = new PopupPanel();
            window.init();
            window.addCloseHandler(new CloseHandler<PopupPanel>() {
                @Override
                public void onClose(final CloseEvent<PopupPanel> event) {
                    // TODO Maybe something here?
                }
            });                
            window.show();
        }
    });

Solution

  • OK, I managed to do this by checking whether the last hovered element on the Menubar was the menuItem that opens the window. To do this, I had to subclass the default MenuBar class and exposing the getSelectedItem() method (it was protected by default, why?)

    @UiField
    MyMenuBar myMenuBar;
    
    ....
    
    menuItem.setScheduledCommand(new ScheduledCommand() {
                @Override
                public void execute() {
                    if (!wasHoveredWhenClosed) {
                        window.init();
                        window.addCloseHandler(new CloseHandler<PopupPanel>() {
                            @Override
                            public void onClose(final CloseEvent<PopupPanel> event) {
                                wasHoveredWhenClosed = myMenuBar.getSelectedItem() != menuItem;
                            }
                        });
                        window.show();
                    } else {
                        wasHoveredWhenClosed = false;
                    }
                }
            });