Barry
Barry

Reputation: 1665

GWT Multiple Events Handling

I have 3 widgets on my UI side (1 ListBox, 2 TextBoxes). I would like to create an Handler which could handle value change event if any of the three widgets value changes and also if on Blur for ListBox.The skeleton of code would be something like this

registerHandler(new multiWidgetHandler());
private class multiWidgetHandler{
  //code for handling onValueChange for 3 widgets and also onBlur for listBox
}

I am not sure how to implement this cleanly. Need help. Some example code would be appreciated.

Upvotes: 2

Views: 3613

Answers (2)

Riley Lark
Riley Lark

Reputation: 20890

You can implement multiple Handler interfaces in the same handler, and then add that handler multiple times.

private class MultiWidgetHandler implements ValueChangeHandler<String>, BlurHandler, ChangeHandler
{
    protected void handleIt() { Window.alert("These events are so handled right now!"); }

    public void onBlur(BlurEvent e) { handleIt(); }
    public void onValueChange(ValueChangeEvent<String> e) { handleIt(); }
    public void onChange(ChangeEvent e) { handleIt(); }
}

...

MultiWidgetHandler handler = new MultiWidgetHandler();

listBox.addChangeHandler(handler);
listBox.addBlurHandler(handler);
textArea1.addValueChangeHandler(handler);
textArea2.addValueChangeHandler(handler);

Upvotes: 4

code-gijoe
code-gijoe

Reputation: 7244

Why are you planning such a behaviour? Usually on handler for one event. If you want to channel all 3 events to a single handler you could make your 3 different handlers call a "global" method : HandleAllEvents(). If all events are of the same type you can register the same handler 3 times.

Upvotes: 1

Related Questions