Search code examples
gwtmouseovergwt2

Propagate event from internal panel in GWT


I created component, which is Composite Panel (let's call A) which has inside several other Composite Panel (B1, B2, B3 ...) and each of internal Composite Panel (B1, B2, B3 ...) fire itself OnMouseOver and that is ok (I know how to add event to composite panel)
QUESTION:
but how to propagate this event to major composite Panel (A). I would like use panel A in some view, like bellow:

@UiField
MyCompositePanelA panelA; //which has inside B1 B2 B3 ...

...

@UiHandler(value={"panelA"})
void onOver(MouseOverEvent e) { //or some other custom event

    if (e... =  B1) // recognize - was MouseOverEvent on B1 
}

Best Regards


Solution

  • You have two options: EventBus, which is most likely an overkill in your use case, or attaching handlers to your internal panels. For example,

    (a) Add this method to your internal panel widget:

    public void addMyMouseOverHandler(MouseOverHandler mouseOverHandler) {
        myInternalPanel.addDomHandler(mouseOverHandler, MouseOverEvent.getType());
    }
    

    (b) Then, you create this handler:

    MouseOverHandler mouseOverHandler = new MouseOverHandler() {
    
        @Override
        public void onMouseOver(MouseOverEvent event) {
            // do something when this event fires
        }
    }
    

    and attach it to each internal panel that you create.

    You cannot attach this handler using Ui:Binder if the action you need is outside of the widget's scope. You need to create this handler where you have all what you need for your action to complete (like in your View), and then attach it to the widget.

    In other words, in GWT you do not wait for event to propagate to your desired level (like, your View): you specify the desired action in a handler, and then attach this handler to a widget.