package com.cedarsoft.spring.rcp.beans;
import com.cedarsoft.spring.rcp.events.ShowBeansEvent;
import com.cedarsoft.NotFoundException;
import org.jetbrains.annotations.NotNull;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* Registry for bean ui access.
* Registers a listener and shows the beans using the {@link BeansDisplay}s that
* have been registered.
*/
public class BeanUiAccessRegistry implements ApplicationListener {
@Override
public void onApplicationEvent( @NotNull ApplicationEvent event ) {
if ( event instanceof ShowBeansEvent ) {
ShowBeansEvent beansEvent = ( ShowBeansEvent ) event;
BeanUiAccess<?> uiAccess = findBeanUiAccess( beansEvent.getBeanType() );
uiAccess.getBeansDisplay().show( beansEvent.getContext() );
}
}
/**
* Returns the bean ui access for the given bean type
*
* @param beanType the type
* @return the bean ui access
*
* @throws NotFoundException if no bean ui access has been found for the given type
*/
@NotNull
public <T> BeanUiAccess<T> findBeanUiAccess( @NotNull Class<T> beanType ) throws NotFoundException {
for ( BeanUiAccess<?> uiAccess : beanUiAccesses ) {
if ( uiAccess.getBeanType().equals( beanType ) ) {
return ( BeanUiAccess<T> ) uiAccess;
}
}
throw new NotFoundException( "No BeanUiAccess found for " + beanType );
}
@NotNull
private final List<BeanUiAccess<?>> beanUiAccesses = new ArrayList<BeanUiAccess<?>>();
@NotNull
public List<? extends BeanUiAccess<?>> getBeanUiAccesses() {
return Collections.unmodifiableList( beanUiAccesses );
}
public void setBeanUiAccesses( @NotNull List<? extends BeanUiAccess<?>> beanUiAccesses ) {
this.beanUiAccesses.clear();
this.beanUiAccesses.addAll( beanUiAccesses );
}
}