use of com.vaadin.flow.component.Component.MapToExistingElement 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();
}
}
Aggregations