New Event Handling and Composites

2009-06-12 Thread Buzzterrier

I am just not getting the new Event handling.  I have went over every
example I can find, but it is not clicking. How do you let composite
widgets subscribe to events from other composite widgets? From reading
it sounds like there is a HandlerManager that you register with by
implementing the HasxxHandlers interface, but I cannot piece together
how the Class that creates the event fires it off to the subscribers.

For example I have the following SimpleWidget that extends Composite
that contains a single button. I have another Composite widget that
wishes to be notified when that button is clicked. Can someone fill in
the blanks for me?

(Note I did not attempt to add the event handlers because I do not
want to confuse others who may have the same issue.)

public class SimpleWidget extends Composite{

private Button button;

public SimpleWidget() {
FlowPanel fp = new FlowPanel();
button = new Button("Click me");
//With listeners I would create an anonymous inner class
//that fires the click events to registered listeners.
//I don't know how to fire events for handlers
//button.addClickHandler(handler);???
fp.add(button);
initWidget(fp);
}
}

public class DoSomething extends Composite{

public DoSomething() {
FlowPanel fp = new FlowPanel();
SimpleWidget simple = new SimpleWidget();

fp.add(simple);
initWidget(fp);
}
}
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Google Web Toolkit" group.
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: New Event Handling and Composites

2009-06-12 Thread Buzzterrier

Thx Thomas I really appreciate this.

That worked, but when I handle the event in DoSomething, I need to
know what button was clicked.

e.g.

public class DoSomething extends Composite implements ClickHandler{

private SimpleWidget simpleWidget;

public DoSomething() {
FlowPanel fp = new FlowPanel();
simple = new SimpleWidget();

simple.addClickHandler(this);

fp.add(simpleWidget);
initWidget(fp);
}

public void onClick(ClickEvent event) {
if (event.getSource() == simple.getButton()) {

//do something
}else if(event.getSource() == smiple.getSomeOtherButton(){
  //do something else.
}
}
}


So in the onClick handler event.getSource() sources SimpleWidget, but
I really need to know what button was clicked.
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Google Web Toolkit" group.
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: New Event Handling and Composites

2009-06-12 Thread Buzzterrier

So tracing the fireEvent method, I found that the source does indeed
reference the button, but in HandlerManager, the source gets changed
to SimpleWidget.

Object oldSource = event.getSource(); //event.getSouce is button
event.setSource(source); //here source is SimpleWidget

Not sure why.

the full method.

 /**
   * Fires the given event to the handlers listening to the event's
type.
   *
   * Note, any subclass should be very careful about overriding this
method, as
   * adds/removes of handlers will not be safe except within this
   * implementation.
   *
   * @param event the event
   */
  public void fireEvent(GwtEvent event) {
// If it not live we should revive it.
if (!event.isLive()) {
  event.revive();
}
Object oldSource = event.getSource();
event.setSource(source);
try {
  firingDepth++;

  registry.fireEvent(event, isReverseOrder);

} finally {
  firingDepth--;
  if (firingDepth == 0) {
handleQueuedAddsAndRemoves();
  }
}
if (oldSource == null) {
  // This was my event, so I should kill it now that I'm done.
  event.kill();
} else {
  // Restoring the source for the next handler to use.
  event.setSource(oldSource);
}
  }

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Google Web Toolkit" group.
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
-~--~~~~--~~--~--~---