Can someone give me an example of creating a custom set of an Event and a Handler. Say you have a Person object that you want your widgets to know if it got updated.
Thanks for all the responses. Zakness came the closest to giving me the answer I needed, however, I came up with a slightly simpler model.
My main goal was to avoid using a static variable to my main data structure. I also hit the problem of trying to figure out if that main data structure was successfully retrieved from the database at the time of trying to access it and what to do when it's not (i.e. when it's null).
After watching the Google Web Toolkit Architecture: Best Practices For Architecting Your GWT App video from Google IO, the Event Bus idea seemed perfect.
I'll post my solution here in case it helps anyone else out.
First, create the Handler class. Note the reference to the Event class already:
public interface CategoryChangeHandler extends EventHandler {
void onCategoryChange(CategoryChangeEvent event);
}
Now on to the Event class. This gave me the most trouble:
public class CategoryChangeEvent extends GwtEvent {
private final List category;
public CategoryChangeEvent(List category) {
super();
this.category = category;
}
public static final Type TYPE = new Type();
@Override
protected void dispatch(CategoryChangeHandler handler) {
handler.onCategoryChange(this);
}
@Override
public com.google.gwt.event.shared.GwtEvent.Type getAssociatedType() {
return TYPE;
}
public List getCategories(){
return category;
}
}
Now I am able to use these Handler and Event classes like so when this main data structure gets reloaded:
This code got the data structure and want to notify everyone who is listening that it got updated:
CategoryChangeEvent event = new CategoryChangeEvent(result);
eventBus.fireEvent(event);
This code is an implementation of the Event
public class PopulateCategoryHandler implements CategoryChangeHandler {
@Override
public void onCategoryChange(CategoryChangeEvent event) {
tearDownCategories();
List categories = event.getCategories();
populateCategories(categories);
}
}