views:

12704

answers:

9

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.

You create a HandlerManager and now you have to create an Event and a Handler. How would you define those classes so that you can subscribe and fire events?

Most of the Events are DOM based, while I want to create some custom events and handlers that I can fire outside of any browser-based event.

A: 

Sounds like you want PropertyChange* support. Have a look at gwtx. Google 'gwt PropertyChange' and you will get multiple blogs that explain how to use it.

digitaljoel
A: 

You might want to have a look at the ValueChangeHandler and ValueChangeEvent in GWT 1.6. Those might work for what you're trying to do.

DLH
+10  A: 

Here's a pretty comprehensive example of creating a custom event, taken verbatim from the GwtEventSystem Wiki (when the event system was still in GWT's incubator).

This is an event that is triggered when the user becomes happy.

Define a new event class. You can add arbitrary metadata in the event class. For simplicity, we will not include any here though.

public class HappyEvent extends GwtEvent {
  ...
}

Define a new handler and marker interface for the event class.

interface HappyHandler extends EventHandler {
  public void onHappiness(HappyEvent event);
}

interface HasHappyEvents {
  public HandlerRegistration addHappyHandler(HappyHandler handler);
}

Add a unique event type

class HappyEvent extends AbstractEvent{
  public static AbstractEvent.Key KEY = new AbstractEvent.Key(){...}

  public GwtEvent.Key getKey(){
    return KEY; 
  }
  ...
}

Wire up the handler's fire method

class HappyEvent extends GwtEvent {
  static Key<HappyEvent,HappyHandler> KEY = new Key<HappyEvent,HappyHandler>(){
    protected void fire(HappyHandler handler, HappyEvent event) {
       handler.onHappiness(event);
    };
   ...
}
Zak Linder
Unfortunately, this answer is pretty badly deprecated by now (which the author already anticipated). For a much more comprehensive overview of custom events in GWT, see this more recent question and answer: http://stackoverflow.com/questions/2951621/gwt-custom-events
Eric Nguyen
+15  A: 

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<CategoryChangeHandler> {

    private final List<Category> category;

    public CategoryChangeEvent(List<Category> category) {
     super();
     this.category = category;
    }

    public static final Type<CategoryChangeHandler> TYPE = new Type<CategoryChangeHandler>();

    @Override
    protected void dispatch(CategoryChangeHandler handler) {
     handler.onCategoryChange(this);
    }

    @Override
    public com.google.gwt.event.shared.GwtEvent.Type<CategoryChangeHandler> getAssociatedType() {
     return TYPE;
    }

    public List<Category> 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<Category> categories = event.getCategories();
  populateCategories(categories); 
 }

}
Nick
A: 

Nick -

The post with code example from June 21, 2009 is very helpful. However, I'm wondering if you could further elaborate on one thing... I see "eventBus.fireEvent(event)", and while I understand the eventBus concept, I'd like to know specifically what "eventBus" refers to here. Is it your own handrolled class, or is it some use of gwt's HandlerManager? The code around what the eventBus is and how your code interacts with it would be very helpful.

Thanks,

-Tom

tom d
Sorry for the late reply, yes, EventBus is just an extension of the HandlerManager class. I didn't really need to enhance anything yet, but I wanted to extend it just in case I needed to.
Nick
+4  A: 

Here is an example of this over on Alex Reid's blog, including a link to an operational code example. The example fills in some of the fuzzy bits and, along with Nick's example here, helps clarify getting started with architecting an event bus in your gwt application.

tom d
I hope the example proves useful. :)
AlexJReid
A: 

Nick, I have a question regarding to your code, when you add your widget to your EventBus, do you have to remove the handler as well?, I suppose it should generate a memory leak if not, Best and thanks

To be clear, I add a handler to the event bus and I fire events using the event bus to be handled by the registered handlers. I would assume you can leave the handler around as long as you may fire an event in which it handles. Not sure if that helps.
Nick
A: 

Create custom GWT events using the HandlerManger shouldn't be this hard, take a look at the example GwtEventBus @ NingZhang.info it is real intuitive. The key classes used are:

  • com.google.gwt.event.shared.HandlerManager
  • com.google.gwt.event.shared.GwtEvent
  • com.google.gwt.event.shared.EventHandler
Ning120
A: 

I think that the most complete and detailed example is in this article

It contains also an example project that shows exactly how to properly use define custom events and use GWT's HandlerManager class.

Shai Rubinshtein