org.bushe.swing.event.annotation
Annotation Type EventTopicSubscriber


@Retention(value=RUNTIME)
@Target(value=METHOD)
public @interface EventTopicSubscriber

An Annotation for subscribing to EventService Topics.

This annotation simplifies much of the repetitive boilerplate used for subscribing to EventService Topics.

Instead of this:

 public class MyAppController implements EventTopicSubscriber {
   public MyAppController {
      EventBus.subscribe("AppClosing", this);
   }
   public void onEvent(String topic, Object data) {
      JComponent source = (JComponent)data;
      //do something
   }
 }
 
You can do this:
 public class MyAppController {  //no interface necessary
   public MyAppController {
       AnnotationProcessor.process(this);//this line can be avoided with a compile-time tool or an Aspect
   }
   @EventTopicSubscriber{topic="AppClosingEvent"}
   public void onAppClosing(String topic, Object data) {
      //do something
   }
 }
 

That's pretty good, but when the controller does more, annotations are even nicer.

 public class MyAppController implements EventTopicSubscriber {
   public MyAppController {
      EventBus.subscribe("AppStartingEvent", this);
      EventBus.subscribe("AppClosingEvent", this);
   }
   public void onEvent(String topic, Object data) {
      //wicked bad pattern, but we have to this
      //...or create multiple subscriber classes and hold instances of them fields, which is even more verbose...
      if ("AppStartingEvent".equals(topic)) {
         onAppStartingEvent((JComponent)data);
      } else ("AppClosingEvent".equals(topic)) {
         onAppClosingEvent((JComponet)data);
      }

   }

   public void onAppStartingEvent(JComponent requestor) {
      //do something
   }

   public void onAppClosingEvent(JComponent requestor) {
      //do something
   }
 }
 
Instead of all that, you can do this:
 public class MyAppController {
   public MyAppController {
       AnnotationProcessor.process(this);//this line can be avoided with a compile-time tool or an Aspect
   }
   @EventTopicSubscriber{topic="AppStartingEvent"}
   public void onAppStartingEvent(Object data) {
      //do something
   }
   @EventTopicSubscriber{topic="AppClosingEvent"}
   public void onAppClosingEvent(Foo data) {
      //do something
   }
 }
 
Brief, clear, and easy.


Required Element Summary
 String topic
          The topic to subscribe to
 
Optional Element Summary
 Class<? extends EventService> autoCreateEventServiceClass
          Whether or not to autocreate the event service if it doesn't exist on subscription, default is true.
 String eventServiceName
          The event service to subscribe to, default to the EventServiceLocator.SERVICE_NAME_EVENT_BUS.
 int priority
          Determines the order in which this subscriber is called, default is FIFO.
 ReferenceStrength referenceStrength
          Whether to subscribe weakly or strongly.
 

Element Detail

topic

public abstract String topic
The topic to subscribe to

referenceStrength

public abstract ReferenceStrength referenceStrength
Whether to subscribe weakly or strongly.

Default:
org.bushe.swing.event.annotation.ReferenceStrength.WEAK

eventServiceName

public abstract String eventServiceName
The event service to subscribe to, default to the EventServiceLocator.SERVICE_NAME_EVENT_BUS.

Default:
"EventBus"

priority

public abstract int priority
Determines the order in which this subscriber is called, default is FIFO.

Default:
0

autoCreateEventServiceClass

public abstract Class<? extends EventService> autoCreateEventServiceClass
Whether or not to autocreate the event service if it doesn't exist on subscription, default is true. If the service needs to be created, it must have a default constructor.

Default:
org.bushe.swing.event.ThreadSafeEventService.class


Copyright © 2011 Bushe Enterprises, Inc.. All Rights Reserved.