>
> Q1) I presume inside my dispatch() function I am probably supposed to call
> appropriate functions that I create in my OnChangePlanHander class?
>
> Yes. In the most simple case your handler will only have one method and the
dispatch(Handler handler) method will only contain something like:
handler.yourhandlermethod(this)
> Q2) What do I return for OnChangePlan.getAssociatedType()? Do I need to
> create a subclass of GwtEvent.Type<OnChangePlanHandler> too? When would I
> ever create an instance of this subclass? Will I ever create more than one
> instance of this subclass? (i.e. where should I be storing this instance?)
> It looks like I might have just one such instance declared statically
> somewhere to return it from OnChangePlan.getAssociatedType(). Right?
>
I always create a static variable and then return that variable in
getAssociatedType(). You can directly create an instance of it (new
Type<YourHandler>).
>
> Q3) The javadocs for SimpleEventBus.addHandler() says it should *rarely*
> be called directly. (??) The alternative code snippet offered in the
> javadocs lacks enough context for me; I don't yet understand the entire
> scheme of how these classes are intended to work together.
>
So here is a very simple example for a LoginEvent that does carry an
username and a password:
public class LoginEvent extends GwtEvent<Handler> {
public static interface Handler extends EventHandler {
public void onLogin(LoginEvent event);
}
public static Type<Handler> TYPE = new Type<Handler>();
public static HandlerRegistration register(final EventBus eventBus,
finalHandler handler) {
return eventBus.addHandler(LoginEvent.TYPE, handler);
}
private final String username;
private final String password;
public LoginEvent() {
this(null, null);
}
public LoginEvent(final String username, final String password) {
this.username = username;
this.password = password;
}
public String getUserName() {
return this.username;
}
public String getPassword() {
return this.password;
}
@Override
public Type<Handler> getAssociatedType() {
return LoginEvent.TYPE;
}
@Override
protected void dispatch(final Handler handler) {
handler.onLogin(this);
}
}
In your code you can now listen to LoginEvents via
LoginEvent.register(eventBus,
new LoginEvent.Handler() { ... implement onLogin here ...}). Well and
sending a LoginEvent is as easy as eventbus.fireEvent(new
LoginEvent(username, password)).
In this example you will receive all LoginEvents regardless who has fired
them. If you only want to listen to events from a specific source you can
also add a LoginEvent.registerToSource(EvenBus eventbus, Object source, Handler
handler) method to the event and use the eventbus.addHandlerToSource()method.
Now if you want to listen for LoginEvents that have been fired by an
instance "loginsource" you can call LoginEvent.registerToSource(eventbus,
loginsource, new LoginEvent.Handler() { ... }) and in your loginsource
instance you call eventbus.fireEventFromSource(new LoginEvent(user, pass),
this).
Hope that helps.
-- J.
--
You received this message because you are subscribed to the Google Groups
"Google Web Toolkit" group.
To view this discussion on the web visit
https://groups.google.com/d/msg/google-web-toolkit/-/azNTSDIwQWRpU29K.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/google-web-toolkit?hl=en.