Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<h1>Events in general:</h1> <p>Events are always sent to inform about something (e.g. a change of state). Let's take your example with a man and a wall. Here we can imagine that there is a game where a user can walk as a man in a labyrinth. Every time a user hits the wall it should be informed about the collision so that it can react to it (e.g. a wall can render itself as a destroyed wall). This can be achieved by sending a collision event every time the collision with a wall is detected. This event is sent by a man and every object in the system interested in the event receives it and can react to it accordingly. Objects which want to receive events must register themselves as interested with event.</p> <p>This is how events work in general in every system or framework (not only in GWT). In order to send and receive events in such systems you have to define:</p> <ol> <li>What is sent (what do events look like)</li> <li>Who receives events (event receivers)</li> <li>Who sends events (event senders)</li> </ol> <p>Then you can:</p> <ol start="4"> <li>Register event receivers which want to receive events</li> <li>Send events</li> </ol> <h1>Events in GWT:</h1> <p>Here I will show an example of using custom events in GWT. I will use an example of a system which is responsible for checking a mailbox and inform a user if there are new mails. Let's assume that in the system there are at least 2 components:</p> <ul> <li>message checker responsible for checking the mailbox and </li> <li>message displayer responsible for displaying new mails</li> </ul> <p>Message checker sends events when a new mail is received and message displayer receives these events.</p> <h2>Step 1: Define events</h2> <p>Information about a new mail will be sent as an instance of <code>MessageReceivedEvent</code> class. The class contains a new mail (for the simplicity let's assume it is just a <code>String</code>). </p> <p>Full source code of this class is presented below (the comment for it is below the source code). </p> <pre><code>public class MessageReceivedEvent extends GwtEvent&lt;MessageReceivedEventHandler&gt; { public static Type&lt;MessageReceivedEventHandler&gt; TYPE = new Type&lt;MessageReceivedEventHandler&gt;(); private final String message; public MessageReceivedEvent(String message) { this.message = message; } @Override public Type&lt;MessageReceivedEventHandler&gt; getAssociatedType() { return TYPE; } @Override protected void dispatch(MessageReceivedEventHandler handler) { handler.onMessageReceived(this); } public String getMessage() { return message; } } </code></pre> <p><code>MessageReceivedEventHandler</code> is an interface that represents event receivers. Don't bother with it at the moment, this will be discussed later.</p> <p>Every class representing a GWT event has to extend <code>GwtEvent</code> class. This class contains two abstract methods which must be implemented: <code>getAssociatedType</code> and <code>dispatch</code>. However in every event class they are usually implemented in a very similar way.</p> <p>The class stores information about a received message (see constructor). Every event receiver can get it using <code>getMessage</code> method.</p> <h2>Step 2: Define event receivers</h2> <p>Each event type in GWT is associated to an interface representing receivers of this event type. In GWT receivers are called handlers. In the example an event receiver interface for <code>MessageReceivedEvent</code> will be named <code>MessageReceivedEventHandler</code>. The source code is below:</p> <pre><code>public interface MessageReceivedEventHandler extends EventHandler { void onMessageReceived(MessageReceivedEvent event); } </code></pre> <p>Each handler has to extend <code>EventHandler</code> interface. It should also define a method which will be invoked when an event occurs (it should take at least one parameter - an event). Here the method is named <code>onMessageReceived</code>. Each receiver can react on an event by implementing this method.</p> <p>The only event receiver in the example is <code>MessageDisplayer</code> component:</p> <pre><code>public class MessageDisplayer implements MessageReceivedEventHandler { @Override public void onMessageReceived(MessageReceivedEvent event) { String newMessage = event.getMessage(); // display a new message // ... } } </code></pre> <h2>Step 3: Define event senders</h2> <p>In the example the only event sender is a component responsible for checking mails - <code>EventChecker</code>:</p> <pre><code>public class MessageChecker implements HasHandlers { private HandlerManager handlerManager; public MessageChecker() { handlerManager = new HandlerManager(this); } @Override public void fireEvent(GwtEvent&lt;?&gt; event) { handlerManager.fireEvent(event); } public HandlerRegistration addMessageReceivedEventHandler( MessageReceivedEventHandler handler) { return handlerManager.addHandler(MessageReceivedEvent.TYPE, handler); } } </code></pre> <p>Every event sender has to implement <code>HasHandlers</code> interface.</p> <p>The most important element here is a <code>HandlerManager</code> field. In GWT <code>HandlerManager</code> as the name suggest manages event handlers (event receivers). As it was said at the beginning every event receiver that wants to receive events must register itself as interested. This is what handler managers are for. They make it possible to register event handlers an they can send a particular event to every registered event handler.</p> <p>When a <code>HanlderManager</code> is created it takes one argument in its constructor. Every event has a source of origin and this parameter will be used as a source for all events send by this handler manager. In the example it is <code>this</code> as the source of events is <code>MessageChecker</code>.</p> <p>The method <code>fireEvent</code> is defined in <code>HasHandlers</code> interface and is responsible for sending events. As you can see it just uses a handler manager to send (fire) and event.</p> <p><code>addMessageReceivedEventHandler</code> is used by event receivers to register themselves as interested in receiving events. Again handler manager is used for this.</p> <h2>Step 4: Bind event receivers with event senders</h2> <p>When everything is defined event receivers must register themselves in event senders. This is usually done during creation of objects:</p> <pre><code>MessageChecker checker = new MessageChecker(); MessageDisplayer displayer = new MessageDisplayer(); checker.addMessageReceivedEventHandler(displayer); </code></pre> <p>Now all events sent by <code>checker</code> will be received by <code>displayer</code>.</p> <h2>Step 5: Send events</h2> <p>To send an event, <code>MessageChecker</code> must create an event instance and send it using <code>fireEvent</code> method. This cane be done in <code>newMailReceived</code> method:</p> <pre><code>public class MessageChecker implements HasHandlers { // ... not important stuff omitted public void newMailReceived() { String mail = ""; // get a new mail from mailbox MessageReceivedEvent event = new MessageReceivedEvent(mail); fireEvent(event); } } </code></pre> <p>I hope it is clear and will help :)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload