Interface Mediator<Q extends java.lang.annotation.Annotation,​T,​W>


  • public interface Mediator<Q extends java.lang.annotation.Annotation,​T,​W>
    Mediatoratches for Mediatorualified bean implementations of Mediator:

     // add @Named for automatic registration
     public class MyMediator
         implements Mediator<Named, MyType, MyWatcher>
     {
         public void add( BeanEntry<Named, MyType> entry, MyWatcher watcher )
             throws Exception
         {
             // translate event to whatever the watcher expects
         }
     
         public void remove( BeanEntry<Named, MyType> entry, MyWatcher watcher )
             throws Exception
         {
             // translate event to whatever the watcher expects
         }
     }
     
    Mediator implementations must have a public no-arg constructor; they are neither injected nor injectable, acting instead as stateless translators.

    IMPORTANT: mediation occurs when bindings change and there is at least one live watcher. If no-one requests or injects an instance of the watcher type then the mediator will not be called.

    In the following example as soon as MyTabbedPane is injected, Sisu will use the SwingTabMediator to deliver all known JPanels annotated with @Tab to the watching MyTabbedPane. Sisu will continue to send updates, which add or remove tabs as appropriate, until the MyTabbedPane instance becomes unreachable. MyTabbedPane doesn't need to know anything about Sisu APIs and vice-versa because SwingTabMediator takes care of the necessary translation.

     @Named
     public class MyTabbedPane
         extends JTabbedPane
     {
         // watcher
     }
     
     @Qualifier
     @Retention( RetentionPolicy.RUNTIME )
     public @interface Tab
     {
         String title();
     }
     
     @Tab( title = "Summary" )
     public class SummaryTab
         extends JPanel
     {
         // qualified bean
     }
     
     @Tab( title = "Notes" )
     public class NotesTab
         extends JPanel
     {
         // qualified bean
     }
     
     @Named
     public class SwingTabMediator
         implements Mediator<Tab, JPanel, MyTabbedPane>
     {
         public void add( BeanEntry<Tab, JPanel> entry, final MyTabbedPane watcher )
             throws Exception
         {
             final Tab tab = entry.getKey();
             final JPanel panel = entry.getValue();
     
             SwingUtilities.invokeLater( new Runnable()
             {
                 public void run()
                 {
                     watcher.addTab( tab.title(), panel );
                 }
             } );
         }
     
         public void remove( BeanEntry<Tab, JPanel> entry, final MyTabbedPane watcher )
             throws Exception
         {
             final Tab tab = entry.getKey();
     
             SwingUtilities.invokeLater( new Runnable()
             {
                 public void run()
                 {
                     watcher.removeTabAt( watcher.indexOfTab( tab.title() ) );
                 }
             } );
         }
     }
     
    See Also:
    BeanLocator
    • Method Detail

      • add

        void add​(BeanEntry<Q,​T> entry,
                 W watcher)
          throws java.lang.Exception
        Processes the added BeanEntry and sends the necessary updates to the watcher.
        Parameters:
        entry - The added bean entry
        watcher - The watching object
        Throws:
        java.lang.Exception
      • remove

        void remove​(BeanEntry<Q,​T> entry,
                    W watcher)
             throws java.lang.Exception
        Processes the removed BeanEntry and sends the necessary updates to the watcher.
        Parameters:
        entry - The removed bean entry
        watcher - The watching object
        Throws:
        java.lang.Exception