Aw: SimpleEventBus example?

2011-06-07 Thread Jens


 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.TypeOnChangePlanHandler 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 
TypeYourHandler).
 


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


 public static interface Handler extends EventHandler {

public void onLogin(LoginEvent event);

}


 public static TypeHandler TYPE = new TypeHandler();


 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 TypeHandler 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 google-web-toolkit@googlegroups.com.
To unsubscribe from this group, send email to 
google-web-toolkit+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/google-web-toolkit?hl=en.



Re: Aw: SimpleEventBus example?

2011-06-07 Thread Broc Seib
Jens,

Excellent example -- thank you.

The key points I needed were:
  1) I must define my handler as an interface, not a class (I finally did 
figure that one out -- whoops!)
  2) I don't need to subclass GwtEvent.Type
  3) I can define everything inside a single Event subclass, nice clean and 
neat single source file.

Your post is the first concise example I've seen for using the 
EventBus. Your example will quickly become the first result when googling 
for 'SimpleEventBus example' (if not already). The javadocs really need to 
include this kind of example.

-broc

-- 
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/-/MDRXazRYekFOTThK.
To post to this group, send email to google-web-toolkit@googlegroups.com.
To unsubscribe from this group, send email to 
google-web-toolkit+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/google-web-toolkit?hl=en.