use of com.vaadin.flow.di.Instantiator in project flow by vaadin.
the class ComponentUtil method componentFromElement.
/**
* Creates a new component instance using the given element, maps the
* component to the element and optionally maps the element to the component
* (if <code>mapComponent</code> is <code>true</code>).
* <p>
* This is a helper method for Element#as and Component#from.
*
* @see Component#from(Element, Class)
* @see Element#as(Class)
*
* @param <T>
* the component type
* @param element
* the element
* @param componentType
* the component type
* @param mapComponent
* <code>true</code> to also map the element to the component,
* <code>false</code> to only map the component to the element
* @return a new component instance of the given type
*/
public static <T extends Component> T componentFromElement(Element element, Class<T> componentType, boolean mapComponent) {
if (element == null) {
throw new IllegalArgumentException("Element to use cannot be null");
}
if (componentType == null) {
throw new IllegalArgumentException("Component type cannot be null");
}
MapToExistingElement wrapData = new MapToExistingElement(element, mapComponent);
try {
Component.elementToMapTo.set(wrapData);
UI ui = UI.getCurrent();
if (ui == null) {
throw new IllegalStateException("UI instance is not available. " + "It looks like you are trying to execute UI code outside the UI/Servlet dispatching thread");
}
Instantiator instantiator = Instantiator.get(ui);
return instantiator.createComponent(componentType);
} finally {
// This should always be cleared, normally it's cleared in Component
// but in case of exception it might be not cleared
Component.elementToMapTo.remove();
}
}
use of com.vaadin.flow.di.Instantiator in project flow by vaadin.
the class SpringInstantiatorTest method createRouteTarget_springManagedBean_instanceIsCreated.
@Test
public void createRouteTarget_springManagedBean_instanceIsCreated() throws ServletException {
Instantiator instantiator = getInstantiator(context);
RouteTarget2 singleton = context.getBean(RouteTarget2.class);
Assert.assertEquals(singleton, instantiator.createRouteTarget(RouteTarget2.class, null));
}
use of com.vaadin.flow.di.Instantiator in project flow by vaadin.
the class SpringVaadinServletServiceTest method getInstantiator_springManagedBean_instantiatorBeanReturned.
@Test
public void getInstantiator_springManagedBean_instantiatorBeanReturned() throws ServletException {
Properties properties = new Properties();
properties.setProperty(FOO, Boolean.TRUE.toString());
VaadinService service = SpringInstantiatorTest.getService(context, properties);
Instantiator instantiator = service.getInstantiator();
Assert.assertEquals(TestInstantiator.class, instantiator.getClass());
}
use of com.vaadin.flow.di.Instantiator in project flow by vaadin.
the class SpringVaadinServletService method loadInstantiators.
@Override
protected Optional<Instantiator> loadInstantiators() throws ServiceException {
Optional<Instantiator> spiInstantiator = super.loadInstantiators();
List<Instantiator> springInstantiators = context.getBeansOfType(Instantiator.class).values().stream().filter(instantiator -> instantiator.init(this)).collect(Collectors.toList());
if (spiInstantiator.isPresent() && !springInstantiators.isEmpty()) {
throw new ServiceException("Cannot init VaadinService because there are multiple eligible " + "instantiator implementations: Java SPI registered instantiator " + spiInstantiator.get() + " and Spring instantiator beans: " + springInstantiators);
}
if (!spiInstantiator.isPresent() && springInstantiators.isEmpty()) {
Instantiator defaultInstantiator = new SpringInstantiator(this, context);
defaultInstantiator.init(this);
return Optional.of(defaultInstantiator);
}
return spiInstantiator.isPresent() ? spiInstantiator : springInstantiators.stream().findFirst();
}
Aggregations