Component creation and event implementation

Hi all …

I tried to create UI component.

I just want my component be able to fire some events.

I don’t know how, and i don’t find anything in Vaadin Book.

My goal :

  • Create component “MyComp”.
  • And then add listener on “MyComp” with
myComp.addListener(new MyCompListener () {
	public void myEvent(Event event)
		{
			.......
		}
});
  • Be able to fire an event in MyComp, and catch it in parent view.

Thanks for your help …

For simple parent/child event communication, I’d probably do it the manual way: Create a listener interface (public inner interface would do just fine) with just one method that you will call whenever a certain event happens. ’

For example “DinnerListener.dinnerStarts()”. Then you let a class implement the DinnerListener interface. The object that notifies everyone that dinner is starting would have a method “addListener(DinnerListener)”, and the only thing it does is to add those DinnerListeners into a Collection the object is holding (Set would be fine). Now, whenever the object knows that dinner is starting, it will iterate through that Collection of DinnerListeners, and call the “dinnerStarts()” method on each and everyone. If you need to pass some information to the listeners, just add a parameter to the “dinnerStarts()” method, and you can pass data that way.

In case you need to send events further away than just parent/child, you might be interested in my event router:
Blackboard
. There’s some documentation and an example application that you can get inspiration of. There’s no reason you couldn’t use Blackboard for parent/child communication, but that might obscure the intent of the code, and otherwise be a bit too overkill. Using the ThreadLocal pattern with Blackboard is a good way to get the Blackboard instance globally accessible.

It is not clear to me whether you want to transmit new kinds of events from the client (browser side) to the server in your custom widgets or just create/map/fire events on the server side.

You could consider delegating most of the logic to the class com.vaadin.event.EventRouter - some of the internal components in Vaadin do so. AbstractComponent also makes it relatively easy to use it to listen to your own client side events, so that the events are only sent from the client to the server if someone is listening for the corresponding event identifiers.

The downside of this approach is losing a little type safety, but you can encapsulate those parts quite well.

See the
javadoc for the com.vaadin.event package
for a little more info.