com.vaadin.flow.component.
Class ComponentUtil
- java.lang.Object
-
- com.vaadin.flow.component.ComponentUtil
-
-
Method Summary
All Methods Modifier and Type Method and Description static <T extends ComponentEvent<?>>
RegistrationaddListener(Component component, Class<T> eventType, ComponentEventListener<T> listener)
Adds a listener for an event of the given type to the
component
.static <T extends ComponentEvent<?>>
RegistrationaddListener(Component component, Class<T> eventType, ComponentEventListener<T> listener, Consumer<DomListenerRegistration> domListenerConsumer)
Adds a listener for an event of the given type to the
component
, and customizes the corresponding DOM event listener with the given consumer.static <T extends Component>
TcomponentFromElement(Element element, Class<T> componentType, boolean mapComponent)
Creates a new component instance using the given element, maps the component to the element and optionally maps the element to the component (if
mapComponent
istrue
).static void
findComponents(Element element, Consumer<Component> componentConsumer)
Finds the first component instance in each
Element
subtree by traversing theElement
tree starting from the given element.static Optional<Component>
findParentComponent(Element element)
Finds the first component by traversing upwards in the element hierarchy, starting from the given element.
static <T extends Component>
voidfireEvent(T component, ComponentEvent<? extends T> componentEvent)
Dispatches the event to all listeners registered for the event type.
static <T> T
getData(Component component, Class<T> type)
Gets a data instance with the given type, or
null
if there is no such instance.static Object
getData(Component component, String key)
Gets a data instance with the given key, or
null
if no data has been set for that key.static ComponentMetaData.DependencyInfo
getDependencies(VaadinService service, Class<? extends Component> componentClass)
Gets the dependencies for the given class, defined using annotations (
HtmlImport
,JavaScript
,StyleSheet
andUses
).static Component
getInnermostComponent(Composite<?> composite)
Returns the innermost component from a
Composite
chain, i.e.static Component
getInnermostComponent(Element element)
Gets the innermost mapped component for the element.
static Component
getParentUsingComposite(Composite<?> composite, Component component)
Gets the parent of the given component, which is inside the given composite.
static Collection<ComponentMetaData.SynchronizedPropertyInfo>
getSynchronizedProperties(Class<? extends Component> componentClass)
Gets the synchronized property infos of the properties that are defined declaratively for the given class with their RPC update mode.
static Stream<String>
getSynchronizedPropertyEvents(Class<? extends Component> componentClass)
Gets the name of the synchronized property event defined declaratively for the given class.
static boolean
isCompositeContent(Composite<?> composite, Component component)
Checks if the given component is inside a
Composite
chain, i.e.static void
onComponentAttach(Component component, boolean initialAttach)
Handles triggering the
onAttach
method and firing theAttachEvent
for the given component when it has been attached to a UI.static void
onComponentDetach(Component component)
Handles triggering the
onDetach
method and firing theDetachEvent
for the given component right before it is detached from a UI.static <T> void
setData(Component component, Class<T> type, T value)
Stores an instance of a specific type for the given component.
static void
setData(Component component, String key, Object value)
Stores a arbitrary value for the given component.
-
-
-
Method Detail
-
findComponents
public static void findComponents(Element element, Consumer<Component> componentConsumer)
Finds the first component instance in each
Element
subtree by traversing theElement
tree starting from the given element.Parameters:
element
- the element to start scanning fromcomponentConsumer
- a consumer which is called for each found component
-
getParentUsingComposite
public static Component getParentUsingComposite(Composite<?> composite, Component component)
Gets the parent of the given component, which is inside the given composite.
This method is meant for internal use only.
Parameters:
composite
- the composite containing the componentcomponent
- the component to get the parent for, must be inside the composite or a nested compositeReturns:
the parent of the component, never
null
-
getInnermostComponent
public static Component getInnermostComponent(Composite<?> composite)
Returns the innermost component from a
Composite
chain, i.e. the first content which is not aComposite
.Parameters:
composite
- a composite in the chainReturns:
the innermost component
-
isCompositeContent
public static boolean isCompositeContent(Composite<?> composite, Component component)
Checks if the given component is inside a
Composite
chain, i.e. it is a composite in the chain or the content of the innermostComposite
.Parameters:
composite
- the first compositecomponent
- the component to look forReturns:
true
if the component is inside the composite chain,false
otherwise
-
findParentComponent
public static Optional<Component> findParentComponent(Element element)
Finds the first component by traversing upwards in the element hierarchy, starting from the given element.
Parameters:
element
- the element from which to begin the searchReturns:
optional of the component, empty if no component is found
-
getInnermostComponent
public static Component getInnermostComponent(Element element)
Gets the innermost mapped component for the element.
This returns
Element.getComponent()
if something else than aComposite
is mapped to the element. If aComposite
is mapped to the element, finds the innermost content of theComposite
chain.Parameters:
element
- the element which is mapped to a componentReturns:
the innermost component mapped to the element
-
onComponentAttach
public static void onComponentAttach(Component component, boolean initialAttach)
Handles triggering the
onAttach
method and firing theAttachEvent
for the given component when it has been attached to a UI.Parameters:
component
- the component attached to a UIinitialAttach
- indicates if this is the first time the component (element) has been attached
-
onComponentDetach
public static void onComponentDetach(Component component)
Handles triggering the
onDetach
method and firing theDetachEvent
for the given component right before it is detached from a UI.Parameters:
component
- the component detached from a UI
-
addListener
public static <T extends ComponentEvent<?>> Registration addListener(Component component, Class<T> eventType, ComponentEventListener<T> listener)
Adds a listener for an event of the given type to the
component
.Type Parameters:
T
- the component event typeParameters:
component
- the component to add thelistener
eventType
- the component event type, notnull
listener
- the listener to add, notnull
Returns:
a handle that can be used for removing the listener
-
addListener
public static <T extends ComponentEvent<?>> Registration addListener(Component component, Class<T> eventType, ComponentEventListener<T> listener, Consumer<DomListenerRegistration> domListenerConsumer)
Adds a listener for an event of the given type to the
component
, and customizes the corresponding DOM event listener with the given consumer. This allows overriding eg. the debounce settings defined in theDomEvent
annotation.Note that customizing the DOM event listener works only for event types which are annotated with
DomEvent
. UseaddListener(Component, Class, ComponentEventListener)
for other listeners, or if you don't need to customize the DOM listener.Type Parameters:
T
- the event typeParameters:
component
- the component to add thelistener
eventType
- the event type for which to call the listener, must be annotated withDomEvent
listener
- the listener to call when the event occurs, notnull
domListenerConsumer
- a consumer to customize the behavior of the DOM event listener, notnull
Returns:
a handle that can be used for removing the listener
Throws:
IllegalArgumentException
- if the event type is not annotated withDomEvent
-
fireEvent
public static <T extends Component> void fireEvent(T component, ComponentEvent<? extends T> componentEvent)
Dispatches the event to all listeners registered for the event type.
Type Parameters:
T
- the type of the componentParameters:
component
- the component for which to fire eventscomponentEvent
- the event to fireSee Also:
-
componentFromElement
public static <T extends Component> T componentFromElement(Element element, Class<T> componentType, boolean mapComponent)
Creates a new component instance using the given element, maps the component to the element and optionally maps the element to the component (if
mapComponent
istrue
).This is a helper method for Element#as and Component#from.
Type Parameters:
T
- the component typeParameters:
element
- the elementcomponentType
- the component typemapComponent
-true
to also map the element to the component,false
to only map the component to the elementReturns:
a new component instance of the given type
See Also:
-
getSynchronizedProperties
public static Collection<ComponentMetaData.SynchronizedPropertyInfo> getSynchronizedProperties(Class<? extends Component> componentClass)
Gets the synchronized property infos of the properties that are defined declaratively for the given class with their RPC update mode.
Parameters:
componentClass
- the component class to checkReturns:
the synchronized property infos of the properties defined declaratively for the class
-
getSynchronizedPropertyEvents
public static Stream<String> getSynchronizedPropertyEvents(Class<? extends Component> componentClass)
Gets the name of the synchronized property event defined declaratively for the given class.
Parameters:
componentClass
- the component class to checkReturns:
the synchronized property events defined declaratively for the class
-
getDependencies
public static ComponentMetaData.DependencyInfo getDependencies(VaadinService service, Class<? extends Component> componentClass)
Gets the dependencies for the given class, defined using annotations (
HtmlImport
,JavaScript
,StyleSheet
andUses
).Parameters:
service
- the service to use for resolving dependenciescomponentClass
- the component class to checkReturns:
the dependencies for the given class
-
setData
public static void setData(Component component, String key, Object value)
Stores a arbitrary value for the given component.
Parameters:
component
- the component for which to set the datakey
- the key with which the instance can be retrieved, notnull
value
- the data to set, ornull
to remove data previously set with the same keySee Also:
setData(Component, Class, Object)
,getData(Component, String)
-
setData
public static <T> void setData(Component component, Class<T> type, T value)
Stores an instance of a specific type for the given component.
Type Parameters:
T
- the data instance typeParameters:
component
- the component for which to set the datatype
- the type of the data to set, notnull
value
- the data instance to set, ornull
to remove data previously set with the same typeSee Also:
setData(Component, String, Object)
,getData(Component, Class)
-
getData
public static Object getData(Component component, String key)
Gets a data instance with the given key, or
null
if no data has been set for that key.Parameters:
component
- the component from which to get the datakey
- the data keyReturns:
the data instance, or
null
if no instance has been set using the given keySee Also:
-
getData
public static <T> T getData(Component component, Class<T> type)
Gets a data instance with the given type, or
null
if there is no such instance.Type Parameters:
T
- the data instance typeParameters:
component
- the component from which to get the datatype
- the data typeReturns:
the data instance, or
null
if no instance has been set using the given typeSee Also:
-
-