Hi Ernesto,

With the current API I'd do it :

@Override
public void onEvent(IEvent<?> event) {
  Object payload = event.getPayload();
  if (payload instanceof FirstEvent) {
     handle((FirstEvent) payload);
  } else if (payload instanceof AnotherEvent) {
     handle((AnotherEvent) payload);
  }
  ...
}

private void handle(FirstEvent) { ... }
private void handle(AnotherEvent) { ... }

It is fairly clean.


With IFrameworkSettings#add(IEventDispatcher) this logic can be generalized
even more.
For example:

@EventCallback  // custom annotation
private void handle(FirstEvent) {...}

i.e. use reflection to find all annotated methods which accept the type of
the event as parameter.

See org.apache.wicket.EventDispatcherTest#dispatchToAnnotatedMethod() in
wicket-core unit tests for a simple implementation.
A more robust impl should cache the results of the reflection
introspections.




On Thu, May 30, 2013 at 10:17 AM, Ernesto Reinaldo Barreiro <
[email protected]> wrote:

> Oops... sent too soon.
>
> so that you might have
>
> abstract TypeBasedEventHandler<T> implements IEventHandler<T> {
>
>     private Class<? extends T> payLoadClass;
>
>    TypeBasedEventHandler(Class<? extends T> payLoadClass) {
>       this.payLoadClass = payLoadClass;
>   }
>
>      public boolean canHandleEvent(IEvent<?> event) {
>       return true if payload is of type T
>       }
> }
>
> and then
>
> Component#addEventListener(IEventHandler<T> listener
>
> )
>
> ad component will check for "passing" handlers and deliver events tho then.
> And then use it as
>
> addListener(new TypeBasedEventHandler<>(MyPayLoad.class) {
>
>      public boolean handleEvent(IEvent<MyPayLoad> event) {
>      }
> });
>
> but this might make Component object too heavy.. Isn't it?
>
> Are there any plans to rework events in any way to make then generic
> friendly?
>
>
>
>
> On Thu, May 30, 2013 at 11:07 AM, Ernesto Reinaldo Barreiro <
> [email protected]> wrote:
>
> > Hi,
> >
> > I'm starting to use events a lot on my applications... and all the time I
> > have to do something like.
> >
> > @Override
> > public void onEvent(IEvent<?> event) {
> >  if( event.getPayload() instanceof CreateEditTaskPanel.TaskCreatedEvent){
> > CreateEditTaskPanel.TaskCreatedEvent message =
> > (CreateEditTaskPanel.TaskCreatedEvent) event.getPayload();
> >  if( tasksViewParent.isVisible() ){
> > message.getTarget().add( tasksViewParent );
> > }
> >  }
> > }
> >
> > which is not really nice. I was thinking about something like.
> >
> > interface IEventHandler<T> {
> >
> >    public boolean canHandleEvent(IEvent<?> event);
> >
> >    public boolean handleEvent(IEvent<?> event);
> >
> > }
> >
> > so that you might have
> >
> > TypeBasedEventHandler<T> implements IEventHandler<T> {
> >
> >
> >    TypeBasedEventHandler(Class<? extends T> payLoadClass)
> > }
> >
> > --
> > Regards - Ernesto Reinaldo Barreiro
> >
>
>
>
> --
> Regards - Ernesto Reinaldo Barreiro
>

Reply via email to