Wednesday, April 14, 2010

Event handing.

This was the first tough nut to crack. I am trying to learn event handling from the perspective of a widget creator, advanced widget creator. This post will be updated with all the links I find online and in the end I will write my own understanding.
Forget the links.
I am going to write what I think one should should learn handlers.
First of all.
Event: It is something that happens. In everyday words lets think about it as a being hungry. yes I feel hungry in the after noon thats an event. Well feeling hungry is physical event rather than mental because your stomach starts aching and all. and lets take another example, of feeling sad this is more on a mental feeling.
We can use the same analogy in Gwt events.
There are two kinds of events:
1. Dom events, which which actually happen like mouse move, mouse click, key up, etc..
2. Logical events, they are perceived events like close the object, before selecting some elements.

Now the key part how are events handled. every widget/object is responsible for responding and handling its own events.
As I perceived earlier their is NO super god kind of object which handles the events of all widgets.

How do you know what to do when you are hungry? Forget the biological instinct for now and lets assume that you are your mother taught you should eat when you are hungry.
you take this lesson and store it in your brain. and when ever you are hungry you execute the lesson. Gwt calls these lessons/instructions as handlers.

Same thing with widgets, every widget has a brain (No AI here in this context brain is a container for instructions on what to do when something happens.) This brain is called HandlerManager.

So HandlerManager is the brain. handlers are the instructions for responding to events.

So how do the events call the handlers when they are fired?
Who is doing the thinking here?
When we are programming we know what to do. suppose when something is closed. we will call some method called close(). We should call the close handlers for that widget/object there.
There is a method called called fire(event) on handler manager. We assume that the handlers are already added to the HandlerManager. On HandlerManager we called hey fire this event and we send in the information identifying the event which is nothing but the event's key.
Remember handler is an interface so how do we know which method to call?
in the fire method of the event we specify handler.onhungry(hungerevent)

No comments:

Post a Comment