org.eclipse.sisu.Mediator Maven / Gradle / Ivy
/*******************************************************************************
* Copyright (c) 2010-present Sonatype, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Stuart McCulloch (Sonatype, Inc.) - initial API and implementation
*******************************************************************************/
package org.eclipse.sisu;
import java.lang.annotation.Annotation;
/**
* Watches for Qualified bean implementations of T:
*
*
*
* // 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 org.eclipse.sisu.inject.BeanLocator
*/
public interface Mediator
{
/**
* Processes the added {@link BeanEntry} and sends the necessary updates to the watcher.
*
* @param entry The added bean entry
* @param watcher The watching object
*/
void add( BeanEntry entry, W watcher )
throws Exception;
/**
* Processes the removed {@link BeanEntry} and sends the necessary updates to the watcher.
*
* @param entry The removed bean entry
* @param watcher The watching object
*/
void remove( BeanEntry entry, W watcher )
throws Exception;
}