com.vaadin.flow.component.

Class ComponentUtil


  • public class ComponentUtil
    extends Object

    Utility methods for Component.

    Since:

    1.0

    Author:

    Vaadin Ltd

    • Method Detail

      • findComponents

        public static void findComponents(Element element,
                                          Consumer<Component> componentConsumer)

        Finds the first component instance in each Element subtree by traversing the Element tree starting from the given element.

        Parameters:

        element - the element to start scanning from

        componentConsumer - 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 component

        component - 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 a Composite chain, i.e. the first content which is not a Composite.

        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 a Composite chain, i.e. it is a composite in the chain or the content of the innermost Composite.

        Parameters:

        composite - the first composite

        component - 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 a Composite is mapped to the element. If a Composite is mapped to the element, finds the innermost content of the Composite 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 the onAttach method and firing the AttachEvent for the given component when it has been attached to a UI.

        Parameters:

        component - the component attached to a UI

        initialAttach - 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 the DetachEvent 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 type

        Parameters:

        component - the component to add the listener

        eventType - the component event type, not null

        listener - the listener to add, not null

        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 the DomEvent annotation.

        Note that customizing the DOM event listener works only for event types which are annotated with DomEvent. Use addListener(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 the listener

        eventType - the event type for which to call the listener, must be annotated with DomEvent

        listener - the listener to call when the event occurs, not null

        domListenerConsumer - a consumer to customize the behavior of the DOM event listener, not null

        Returns:

        a handle that can be used for removing the listener

        Throws:

        IllegalArgumentException - if the event type is not annotated with DomEvent

      • 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 events

        componentEvent - 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 (if mapComponent is true).

        This is a helper method for Element#as and Component#from.

        Type Parameters:

        T - the component type

        Parameters:

        element - the element

        componentType - the component type

        mapComponent - 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

      • 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 data

        key - the key with which the instance can be retrieved, not null

        value - the data to set, or null 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 data

        type - the type of the data to set, not null

        value - the data instance to set, or null 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, or null if no data has been set for that key.

        Parameters:

        component - the component from which to get the data

        key - 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, or null if there is no such instance.

        Type Parameters:

        T - the data instance type

        Parameters:

        component - the component from which to get the data

        type - 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)