Search code examples
gwtuibinder

gwt generic page level click handler


I have this situation, where I display a success/error message on a page and then I want it to disappear when the user does anything on the page (I assume that that triggers a click event, I can ignore events like going to new tab/windows etc.).

I have other "uihandlers" and "clickhandlers" on the page. So if I click empty regions on the page only the hidemessage call fires, else if I click valid 'clickable' elements my hidemessage fires first followed by the relevant handler.

Is there a way I can achieve this without adding hidemessage to all my clickhandlers on the page?

Edit: The message widget is not a PopupPanel, so setAutohide(true) won't work. But it is exactly the behavior I'm looking for. The widget is a custom widget which extends Composite implements HasWidget, HasClickHandlers


Solution

  • You can do this on your error message:

    myPopupPanel.setAutoHideEnabled(true);
    

    It does exactly whet you need. You may also consider setting auto-hide on history events (mostly back button):

    myPopupPanel.setAutoHideOnHistoryEventsEnabled(true);
    

    EDIT:

    If you are not using a PopupPanel, you can make your Widget implement EventPreview, and then:

    public boolean onEventPreview(Event event) {
    
    Element target = DOM.eventGetTarget(event);
    
    boolean widgetIsTarget = (target != null) && DOM.isOrHasChild(getElement(), target);
    
    setVisible(widgetIsTarget):