Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you use GWT's SimpleEventBus or EventBus?

Tags:

gwt

gwt-mvp

I'm working on developing some simple graphing software which needs to be implemented in both swing and gwt. On the gwt side, I'm going to use gwt-g2d for the canvas. I was hoping to use an eventbus across both implementations to simplify some of the software.

My understanding is that it should be something like this:

  1. Instantiate EventBus
  2. Instantiate Parent Widget, sink the events you want for the EventBus (mouseover or rpc callback, for example)
  3. Set EventBus to listen for events from the Parent Widget.
  4. Instantiate child widgets, who should register with the EventBus the listeners it has (Do they need to register events they might fire?)
  5. When the EventBus receives an event, it should make a decision about the event and then act on that decision, whether that means ignoring it, changing the event type, or just relaying it to all applicable child widgets.

Is this generally how it should work? SimpleEventBus is still sort of new, and I can't find much on web about how to really use it.

like image 696
Brian Avatar asked Nov 06 '22 06:11

Brian


1 Answers

A SimpleEventBus provides the very basic functionality of storing a collection of handlers, and calling event.dispatch() with all of the relevant handlers when appropriate. I wouldn't say that you "set EventBus to listen for events" from the parent widget. The SimpleEventBus doesn't have a concept of a "parent." Instead, you pass around a reference to your SimpleEventBus. Then, any object can "fire an event" with the SimpleEventBus.fireEvent method. There's no hierarchy.

Similarly, any object can listen for those events with SimpleEventBus.addHandler(). This is closest to your #4 - addHandler takes a type of event and a handler, so whenever that type of event is passed to fireEvent (by something in your app), that handler will be passed to the event. Note that the Event is what calls the handler, NOT the EventBus! The EventBus just calls event.dispatch(theHandler), and the programmer of the event is responsible for calling the proper function in theHandler. Your #5 is inaccurate, at least for a SimpleEventBus - SimpleEventBus does not inspect the events at all, except to see which handlers to call. It doesn't know about any child widgets or any app logic, and never changes the type of an Event.

Your logic about seeing which region was picked would not be appropriate in an EventBus - instead, you would make a handler that does the logic and listens for ClickEvents. Then, that handler could tell the selected region that it was selected directly, or you could create your own RegionSelectionEvent that you could fire along an EventBus, and then all of the regions could be informed that a RegionSelection has occurred, and your logger could get a notification, or your server monitor could get the notification and notify the boss that someone selected a region, or whatever. If you just need to deselect one region and select another, an eventbus is overkill.

On the other hand, something like a "resize" event makes a lot of sense, since all of your widgets might need to know about it.

like image 198
Riley Lark Avatar answered Nov 15 '22 13:11

Riley Lark