Package com.vaadin.flow.component
Class ComponentUtil
- java.lang.Object
-
- com.vaadin.flow.component.ComponentUtil
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method 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 thecomponent
.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 thecomponent
, 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 (ifmapComponent
istrue
).static void
findComponents(Element element, Consumer<Component> componentConsumer)
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, ornull
if there is no such instance.static Object
getData(Component component, String key)
Gets a data instance with the given key, ornull
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 (JavaScript
,StyleSheet
andUses
).static Component
getInnermostComponent(Composite<?> composite)
Returns the innermost component from aComposite
chain, i.e.static Component
getInnermostComponent(Element element)
Gets the innermost mapped component for the element.static Collection<?>
getListeners(Component component, Class<? extends ComponentEvent> eventType)
Returns all listeners that match or extend the given event type.static Component
getParentUsingComposite(Composite<?> composite, Component component)
Gets the parent of the given component, which is inside the given composite.static Optional<Component>
getRouteComponent(Component component)
Walk up from given component until a Component with a Route annotation is found or empty if no Route is present in parents.static Router
getRouter(HasElement component)
Gets the router instance for the given component.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 <T extends ComponentEvent<?>>
booleanhasEventListener(Component component, Class<? extends T> eventType)
Check if the component has at least one registered listener of the given event type.static boolean
isCompositeContent(Composite<?> composite, Component component)
Checks if the given component is inside aComposite
chain, i.e.static void
onComponentAttach(Component component, boolean initialAttach)
Handles triggering theonAttach
method and firing theAttachEvent
for the given component when it has been attached to a UI.static void
onComponentDetach(Component component)
Handles triggering theonDetach
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 eachElement
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 composite- Returns:
- the parent of the component, never
null
-
getInnermostComponent
public static Component getInnermostComponent(Composite<?> composite)
Returns the innermost component from aComposite
chain, i.e. the first content which is not aComposite
.- Parameters:
composite
- a composite in the chain- Returns:
- the innermost component
-
isCompositeContent
public static boolean isCompositeContent(Composite<?> composite, Component component)
Checks if the given component is inside aComposite
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 for- Returns:
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 search- Returns:
- 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 component- Returns:
- the innermost component mapped to the element
-
onComponentAttach
public static void onComponentAttach(Component component, boolean initialAttach)
Handles triggering theonAttach
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 theonDetach
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 thecomponent
.- Type Parameters:
T
- the component event type- Parameters:
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 thecomponent
, 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 type- Parameters:
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
-
hasEventListener
public static <T extends ComponentEvent<?>> boolean hasEventListener(Component component, Class<? extends T> eventType)
Check if the component has at least one registered listener of the given event type.- Parameters:
component
- the component to which the listener(s) are registered.eventType
- the event type for which the listener(s) are registered.- Returns:
- a boolean indicating whether at least one listener registered to the component for the given event type.
-
getListeners
public static Collection<?> getListeners(Component component, Class<? extends ComponentEvent> eventType)
Returns all listeners that match or extend the given event type.- Parameters:
eventType
- the component event type- Returns:
- A collection with all registered listeners for a given event type. Empty if no listeners are found.
-
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 component- Parameters:
component
- the component for which to fire eventscomponentEvent
- the event to fire- See Also:
Component.fireEvent(ComponentEvent)
-
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 (ifmapComponent
istrue
).This is a helper method for Element#as and Component#from.
- Type Parameters:
T
- the component type- Parameters:
element
- the elementcomponentType
- the component typemapComponent
-true
to also map the element to the component,false
to only map the component to the element- Returns:
- a new component instance of the given type
- See Also:
Component.from(Element, Class)
,Element.as(Class)
-
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 check- Returns:
- 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 check- Returns:
- 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 (JavaScript
,StyleSheet
andUses
).- Parameters:
service
- the service to use for resolving dependenciescomponentClass
- the component class to check- Returns:
- 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 key- See 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 type- Parameters:
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 type- See 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, ornull
if no data has been set for that key.- Parameters:
component
- the component from which to get the datakey
- the data key- Returns:
- the data instance, or
null
if no instance has been set using the given key - See Also:
setData(Component, String, Object)
-
getData
public static <T> T getData(Component component, Class<T> type)
Gets a data instance with the given type, ornull
if there is no such instance.- Type Parameters:
T
- the data instance type- Parameters:
component
- the component from which to get the datatype
- the data type- Returns:
- the data instance, or
null
if no instance has been set using the given type - See Also:
setData(Component, Class, Object)
-
getRouter
public static Router getRouter(HasElement component)
Gets the router instance for the given component. Falls back to the router for the currently active VaadinService if the component is not attached.- Parameters:
component
- component for which the requested router instance serves navigation- Returns:
- a router instance
- Throws:
IllegalStateException
- if no router instance is available
-
getRouteComponent
public static Optional<Component> getRouteComponent(Component component)
Walk up from given component until a Component with a Route annotation is found or empty if no Route is present in parents.- Parameters:
component
- Component to find current route component for- Returns:
- Optional containing Route component if found
-
-