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 org.eclipse.sisu.inject.BeanLocator
|
|
|
|