com.vaadin.flow.component.
Class ComponentUtil
Utility methods for Component
.
Since:
1.0
Author:
Vaadin Ltd
-
Method Summary
Modifier and TypeMethodDescriptionstatic <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.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.
Provides access to the entire mapping of HTML tags to component classes.
getComponentsByTag
(String tag) Retrieves the set of component classes associated with a specific HTML tag.
static <T> T
Gets a data instance with the given type, or
null
if there is no such instance.static Object
Gets a data instance with the given key, or
null
if no data has been set for that key.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 a
Composite
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.
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.
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.
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 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 void
registerComponentClass
(String tag, Class<? extends Component> componentClass) Registers a component class with a specific HTML tag.
static <T> void
Stores an instance of a specific type for the given component.
static void
Stores a arbitrary value for the given component.
-
Method Details
-
registerComponentClass
Registers a component class with a specific HTML tag. This mapping is maintained in a static map and is used primarily during development mode for various component operations like rendering and event handling.
Parameters:
tag
- The HTML tag associated with the component class.componentClass
- The component class to be registered with the given tag. -
getComponentsByTag
Retrieves the set of component classes associated with a specific HTML tag.
Note: This method retrieves data from a mapping that is only populated in development mode. In production mode, this method will always return an empty set.
Parameters:
tag
- The HTML tag for which component classes are to be retrieved.Returns:
A set of component classes associated with the specified HTML tag. Returns an empty set if no classes are associated with the tag or if running in production mode.
-
getAllTagMappings
Provides access to the entire mapping of HTML tags to component classes.
Note: This mapping is only populated in development mode. The map is returned as unmodifiable to prevent external modifications.
Returns:
An unmodifiable map of HTML tags to sets of component classes. This map is only populated in development mode and will be empty in production mode.
-
findComponents
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
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
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
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
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
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
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
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
-
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 typeReturns:
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 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 (
JavaScript
,StyleSheet
andUses
).Parameters:
service
- the service to use for resolving dependenciescomponentClass
- the component class to checkReturns:
the dependencies for the given class
-
setData
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
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:
-
getData
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
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:
-
getRouter
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 navigationReturns:
a router instance
Throws:
IllegalStateException
- if no router instance is available -
getRouteComponent
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 forReturns:
Optional containing Route component if found
-