Class ComponentHelper


  • public class ComponentHelper
    extends Object
    Utility methods for working with UIComponents.
    Author:
    XIMA MEDIA GmbH
    • Constructor Summary

      Constructors 
      Constructor Description
      ComponentHelper()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void addCssDevResource​(String id, String fileName, String libraryName, String devPath)  
      static void addCssResource​(String id, String fileName, String libraryName)
      Dynamically adds a CSS resource to the current page.
      static void addJsDevResource​(String id, String fileName, String libraryName, String devPath)  
      static void addJsResource​(String id, String fileName, String libraryName)
      Dynamically adds a JavaScript resource to the current page.
      static void addRenderId​(String clientId)
      Adds the given component client ID to the set of render IDs of the given faces context, e.g.
      static void addRenderId​(javax.faces.context.FacesContext context, String clientId)
      Adds the given component client ID to the set of render IDs of the given faces context, e.g.
      static void appendJsonObject​(org.primefaces.util.WidgetBuilder wb, IFastJsonConverter converter, Object data, Object defaultValue)
      Serializes the given object via JSONException and appends the result to the widget builder.
      static <T> void appendWidgetConfig​(javax.faces.context.FacesContext context, org.primefaces.util.WidgetBuilder wb, IWidgetConfigProviding<T> config)
      Serializes the given config via JSON and appends the result to the widget builder.
      static String buildAjaxRequest​(javax.faces.context.FacesContext context, javax.faces.component.UIComponent source, org.primefaces.component.api.AjaxSource ajaxSource, String event)  
      static <T> T closest​(javax.faces.component.UIComponent base, Class<T> targetClass)
      Similar to ComponentTraversalUtils.closest(Class, UIComponent) and Components.getClosestParent(UIComponent, Class), but with the difference that this method returns the given base if the base already matches the target type.
      static <T> T closest​(javax.faces.component.UIComponent base, Class<T> targetClass, Predicate<T> filter)
      Similar to ComponentTraversalUtils.closest(Class, UIComponent) and Components.getClosestParent(UIComponent, Class), but with the difference that (a) this method returns the given base if the base already matches the target type, and (b) that this method lets you specifiy a custom filter function that target must match.
      static void executeAjaxRequest​(javax.faces.context.FacesContext context, javax.faces.component.UIComponent source, CustomAjaxSource ajaxSource, String event)  
      static Class<?> getClassFromAttribute​(javax.faces.view.facelets.TagAttribute typeTagAttribute, javax.faces.view.facelets.TagAttribute loaderTagAttribute, javax.faces.view.facelets.FaceletContext ctx)  
      static String[] getIdChain​(javax.faces.component.UIComponent component)  
      static String getIdChainString​(javax.faces.component.UIComponent component)  
      static List<javax.faces.model.SelectItem> getSelectItems​(javax.faces.context.FacesContext context, javax.faces.component.UIInput component)
      Gets the select items defined for a component.
      static boolean hasInvokedSubmit​(javax.faces.component.UIComponent component)
      Similar to Components.hasInvokedSubmit(UIComponent), but with a hacky fix for composite components that implement cc:clientBehavior.
      static <TRetVal> TRetVal invokeMethod​(javax.faces.context.FacesContext context, javax.el.MethodExpression method, Class<TRetVal> expectedReturnType, Object... args)
      Invokes a JSF method expression with the given arguments, and casts the result to the expected type.
      static boolean isAnyParentScheduledToRender​(javax.faces.context.FacesContext context, javax.faces.component.UIComponent base)  
      static void removeFromScheduledToRender​(javax.faces.context.FacesContext context, String clientId)  
      static void removeSelfAndAllParentsScheduledToRender​(javax.faces.context.FacesContext context, javax.faces.component.UIComponent base, String resolvedClientId)
      Removes any parent of the given UI component (not including that given component) that is scheduled to be rendered (updated) from the components to be rendered.
      static <T extends javax.faces.event.SystemEventListener & IIdChainProviding>
      void
      removeViewListeners​(javax.faces.context.FacesContext context, Class<? extends javax.faces.event.SystemEvent> systemEventType, Class<T> listenerType, javax.faces.component.UIComponent component)  
    • Constructor Detail

      • ComponentHelper

        public ComponentHelper()
    • Method Detail

      • addCssDevResource

        public static void addCssDevResource​(String id,
                                             String fileName,
                                             String libraryName,
                                             String devPath)
      • addCssResource

        public static void addCssResource​(String id,
                                          String fileName,
                                          String libraryName)
        Dynamically adds a CSS resource to the current page. Should be called before the actual rendering phase. Usually this is called from a UIComponent during PostAddToViewEvent.
        Parameters:
        id - ID of the resource. Used to prevent it from being added multiple times.
        fileName - File name of the resource.
        libraryName - Library of the resource. May be null or empty when no library should be used.
      • addJsDevResource

        public static void addJsDevResource​(String id,
                                            String fileName,
                                            String libraryName,
                                            String devPath)
      • addJsResource

        public static void addJsResource​(String id,
                                         String fileName,
                                         String libraryName)
        Dynamically adds a JavaScript resource to the current page. Should be called before the actual rendering phase. Usually this is called from a UIComponent during PostAddToViewEvent.
        Parameters:
        id - ID of the resource. Used to prevent it from being added multiple times.
        fileName - File name of the resource.
        libraryName - Library of the resource. May be null or empty when no library should be used.
      • addRenderId

        public static void addRenderId​(javax.faces.context.FacesContext context,
                                       String clientId)
        Adds the given component client ID to the set of render IDs of the given faces context, e.g. causing those components to update during the render phase. Makes sure not to add client IDs that were already added.
        Parameters:
        context - Current faces context.
        clientId - Client ID to add.
      • addRenderId

        public static void addRenderId​(String clientId)
        Adds the given component client ID to the set of render IDs of the given faces context, e.g. causing those components to update during the render phase. Makes sure not to add client IDs that were already added.
        Parameters:
        clientId - Client ID to add.
      • appendJsonObject

        public static void appendJsonObject​(org.primefaces.util.WidgetBuilder wb,
                                            IFastJsonConverter converter,
                                            Object data,
                                            Object defaultValue)
                                     throws IOException,
                                            FastJsonException
        Serializes the given object via JSONException and appends the result to the widget builder.
        Parameters:
        wb - Widget builder to which the properties are appended.
        converter - The JSON converter to use for converting between objects and JSON.
        data - An object with data to append to the widget builder.
        defaultValue - An object with the default data. This is used to omit properties whose value is equal to the default. This reduces the amount of data that needs to be sent to the client. When null, this mechanism is disabled.
        Throws:
        IOException - When the given widget builder throws an IOException.
        FastJsonException - When the JSON could not be converted.
      • appendWidgetConfig

        public static <T> void appendWidgetConfig​(javax.faces.context.FacesContext context,
                                                  org.primefaces.util.WidgetBuilder wb,
                                                  IWidgetConfigProviding<T> config)
                                           throws IOException
        Serializes the given config via JSON and appends the result to the widget builder.
        Type Parameters:
        T - Type of the widget configuration.
        Parameters:
        context - The current faces context to be used.
        wb - Widget builder to which the properties are appended.
        config - A provider for the data to append to the widget builder.
        Throws:
        IOException - When the given widget builder throws an IOException.
      • buildAjaxRequest

        public static String buildAjaxRequest​(javax.faces.context.FacesContext context,
                                              javax.faces.component.UIComponent source,
                                              org.primefaces.component.api.AjaxSource ajaxSource,
                                              String event)
      • closest

        public static <T> T closest​(javax.faces.component.UIComponent base,
                                    Class<T> targetClass)
        Similar to ComponentTraversalUtils.closest(Class, UIComponent) and Components.getClosestParent(UIComponent, Class), but with the difference that this method returns the given base if the base already matches the target type.
        Type Parameters:
        T - Type of the target component.
        Parameters:
        base - Base component where to start.
        targetClass - Type of the target component.
        Returns:
        The first parent of the given type. If the given base is already of the given type, the base is returned.
      • closest

        public static <T> T closest​(javax.faces.component.UIComponent base,
                                    Class<T> targetClass,
                                    Predicate<T> filter)
        Similar to ComponentTraversalUtils.closest(Class, UIComponent) and Components.getClosestParent(UIComponent, Class), but with the difference that (a) this method returns the given base if the base already matches the target type, and (b) that this method lets you specifiy a custom filter function that target must match.
        Type Parameters:
        T - Type of the target component.
        Parameters:
        base - Base component where to start.
        targetClass - Type of the target component.
        filter - Filter to apply to the components.
        Returns:
        The first parent of the given type that matches the given filter. If the given base is already of the given type and matches the filter, the base is returned.
      • executeAjaxRequest

        public static void executeAjaxRequest​(javax.faces.context.FacesContext context,
                                              javax.faces.component.UIComponent source,
                                              CustomAjaxSource ajaxSource,
                                              String event)
      • getClassFromAttribute

        public static Class<?> getClassFromAttribute​(javax.faces.view.facelets.TagAttribute typeTagAttribute,
                                                     javax.faces.view.facelets.TagAttribute loaderTagAttribute,
                                                     javax.faces.view.facelets.FaceletContext ctx)
      • getIdChain

        public static String[] getIdChain​(javax.faces.component.UIComponent component)
      • getIdChainString

        public static String getIdChainString​(javax.faces.component.UIComponent component)
      • getSelectItems

        public static List<javax.faces.model.SelectItem> getSelectItems​(javax.faces.context.FacesContext context,
                                                                        javax.faces.component.UIInput component)
        Gets the select items defined for a component. Select items are defined as child tags, using <f:selectItem> and <f:selectItems>.
        Parameters:
        context - Current faces context.
        component - The component to get the select items from.
        Returns:
        A list of select items.
      • hasInvokedSubmit

        public static boolean hasInvokedSubmit​(javax.faces.component.UIComponent component)
        Similar to Components.hasInvokedSubmit(UIComponent), but with a hacky fix for composite components that implement cc:clientBehavior.
        Parameters:
        component - The component to be checked.
        Returns:
        true if the given component has invoked the form submit.
      • invokeMethod

        public static <TRetVal> TRetVal invokeMethod​(javax.faces.context.FacesContext context,
                                                     javax.el.MethodExpression method,
                                                     Class<TRetVal> expectedReturnType,
                                                     Object... args)
        Invokes a JSF method expression with the given arguments, and casts the result to the expected type. Returns null in case of an error.
        Type Parameters:
        TRetVal - Type of the expected return value of the method expression.
        Parameters:
        context - Faces context to use.
        method - Method expression to invoke.
        expectedReturnType - Expected return type of the method expression. Use Void for no return value.
        args - Optional arguments to pass to the method expression.
        Returns:
        The value returned by the method expression.
      • isAnyParentScheduledToRender

        public static boolean isAnyParentScheduledToRender​(javax.faces.context.FacesContext context,
                                                           javax.faces.component.UIComponent base)
        Parameters:
        context - Current faces context.
        base - Base component in the component tree where to start.
        Returns:
        true if any parent of the given UI component (not including that given component) is scheduled to be rendered (updated), or false otherwise.
      • removeFromScheduledToRender

        public static void removeFromScheduledToRender​(javax.faces.context.FacesContext context,
                                                       String clientId)
      • removeSelfAndAllParentsScheduledToRender

        public static void removeSelfAndAllParentsScheduledToRender​(javax.faces.context.FacesContext context,
                                                                    javax.faces.component.UIComponent base,
                                                                    String resolvedClientId)
        Removes any parent of the given UI component (not including that given component) that is scheduled to be rendered (updated) from the components to be rendered. Adds back all siblings that would have been rendered by virtue of a parent being rendered.
        Parameters:
        context - Current faces context.
        base - Base component in the component tree where to start.
        resolvedClientId - The full client ID of the component, resulting from a full tree visit (e.g. respecting ui:repeat etc.).
      • removeViewListeners

        public static <T extends javax.faces.event.SystemEventListener & IIdChainProviding> void removeViewListeners​(javax.faces.context.FacesContext context,
                                                                                                                     Class<? extends javax.faces.event.SystemEvent> systemEventType,
                                                                                                                     Class<T> listenerType,
                                                                                                                     javax.faces.component.UIComponent component)