Class ComponentHelper
java.lang.Object
de.xima.fc.gui.util.ComponentHelper
Utility methods for working with
UIComponents.- Author:
- XIMA MEDIA GmbH
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceRepresents a disabled validation for aUIInput.static interfaceRepresents a component that was dropped from the composite component stack manager.static interfaceRepresents a scoped EL variable.static interfaceRepresents scoped EL variables. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidaddCssDevResource(String id, String fileName, String libraryName, String devPath) static voidaddCssResource(String id, String fileName, String libraryName) Dynamically adds a CSS resource to the current page.static voidaddJsDevResource(String id, String fileName, String libraryName, String devPath) static voidaddJsResource(String id, String fileName, String libraryName) Dynamically adds a JavaScript resource to the current page.static voidaddRenderId(String clientId) Adds the givencomponentclient IDto the set ofrender IDsof the given faces context, e.g. causing those components to update during the render phase.static voidaddRenderId(javax.faces.context.FacesContext context, String clientId) Adds the givencomponentclient IDto the set ofrender IDsof the given faces context, e.g. causing those components to update during the render phase.addScopedVariable(javax.faces.context.FacesContext context, String varName, Object value) Makes the given value available as an EL variables, using the provided faces context.addScopedVariables(javax.faces.context.FacesContext context, Map<String, Object> variables) Makes the given variables available as EL variables, using the provided faces context.static voidappendJsonObject(org.primefaces.util.WidgetBuilder wb, IFastJsonConverter converter, Object data, Object defaultValue) Serializes the given object viaJSONExceptionand appends the result to the widget builder.static <T> voidappendWidgetConfig(javax.faces.context.FacesContext context, org.primefaces.util.WidgetBuilder wb, IWidgetConfigProviding<T> config) Serializes the given config viaJSONand appends the result to the widget builder.static StringbuildAjaxRequest(javax.faces.context.FacesContext context, javax.faces.component.UIComponent source, org.primefaces.component.api.AjaxSource ajaxSource, String event) static <T> TSimilar toComponentTraversalUtils.closest(Class, UIComponent)andComponents.getClosestParent(UIComponent, Class), but with the difference that this method returns the given base if the base already matches the target type.static <T> TSimilar toComponentTraversalUtils.closest(Class, UIComponent)andComponents.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 javax.el.ValueExpressionconstructValueExpression(Object value) Constructs a value expression for the given value.disableValidation(javax.faces.component.UIInput input) Disables validation for the given input component for the current request.dropComponentFromCompositeStackManager(javax.faces.context.FacesContext context) Drops the top component from the composite component stack manager (if present) and returns aComponentHelper.DroppedComponent.dropComponentFromCompositeStackManager(javax.faces.context.FacesContext context, Class<? extends javax.faces.component.UIComponent> target) Drops the top component from the composite component stack manager (if an instance of the given type) and returns aComponentHelper.DroppedComponent.dropComponentFromCompositeStackManager(javax.faces.context.FacesContext context, javax.faces.component.UIComponent target) Drops the top component from the composite component stack manager (if equal to the given component) and returns aComponentHelper.DroppedComponent.static voidexecuteAjaxRequest(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 StringgetIdChainString(javax.faces.component.UIComponent component) static List<javax.faces.model.SelectItem> getSelectItems(javax.faces.context.FacesContext context, javax.faces.component.UIComponent component) Gets the select items defined for a component.static booleanhasInvokedSubmit(javax.faces.component.UIComponent component) Similar toComponents.hasInvokedSubmit(UIComponent), but with a hacky fix for composite components that implementcc:clientBehavior.static <TRetVal> TRetValinvokeMethod(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 booleanisAnyParentScheduledToRender(javax.faces.context.FacesContext context, javax.faces.component.UIComponent base) static voidremoveFromScheduledToRender(javax.faces.context.FacesContext context, String clientId) static voidremoveSelfAndAllParentsScheduledToRender(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>
voidremoveViewListeners(javax.faces.context.FacesContext context, Class<? extends javax.faces.event.SystemEvent> systemEventType, Class<T> listenerType, javax.faces.component.UIComponent component)
-
Constructor Details
-
ComponentHelper
public ComponentHelper()
-
-
Method Details
-
addCssDevResource
-
addCssResource
Dynamically adds a CSS resource to the current page. Should be called before the actual rendering phase. Usually this is called from aUIComponentduringPostAddToViewEvent.- 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 benullor empty when no library should be used.
-
addJsDevResource
-
addJsResource
Dynamically adds a JavaScript resource to the current page. Should be called before the actual rendering phase. Usually this is called from aUIComponentduringPostAddToViewEvent.- 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 benullor empty when no library should be used.
-
addRenderId
Adds the givencomponentclient IDto the set ofrender IDsof 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
Adds the givencomponentclient IDto the set ofrender IDsof 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.
-
addScopedVariable
public static ComponentHelper.VariableScope addScopedVariable(javax.faces.context.FacesContext context, String varName, Object value) Makes the given value available as an EL variables, using the provided faces context. Restores the original value of the variable when the scope is closed.- Parameters:
context- Current faces context.varName- Name of the variable to be added to the EL context.value- Value to be added to the EL context.- Returns:
- A scope object that can be closed to restore the original value of the variable.
- Since:
- 8.5.4
-
addScopedVariables
public static ComponentHelper.VariablesScope addScopedVariables(javax.faces.context.FacesContext context, Map<String, Object> variables) Makes the given variables available as EL variables, using the provided faces context. Restores the original value of the variables when the scope is closed.- Parameters:
context- Current faces context.variables- Map of variable names and values to be added to the EL context.- Returns:
- A scope object that can be closed to restore the original value of the variables.
- Since:
- 8.5.4
-
appendJsonObject
public static void appendJsonObject(org.primefaces.util.WidgetBuilder wb, IFastJsonConverter converter, Object data, Object defaultValue) throws IOException, FastJsonException Serializes the given object viaJSONExceptionand 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. Whennull, this mechanism is disabled.- Throws:
IOException- When the given widget builder throws anIOException.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 viaJSONand 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 anIOException.
-
buildAjaxRequest
-
closest
Similar toComponentTraversalUtils.closest(Class, UIComponent)andComponents.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 toComponentTraversalUtils.closest(Class, UIComponent)andComponents.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.
-
constructValueExpression
Constructs a value expression for the given value. The expression is a literal expression, so it will always evaluate to the given value.- Parameters:
value- Value for which to construct the value expression.- Returns:
- A value expression that always evaluates to the given value.
-
disableValidation
public static ComponentHelper.DisabledValidation disableValidation(javax.faces.component.UIInput input) Disables validation for the given input component for the current request. Validation is re-enabled once the returned auto-closeable object is closed.- Parameters:
input- Input component for which validation should be disabled.- Returns:
- An auto-closeable object that re-enables validation for the given input once closed.
-
dropComponentFromCompositeStackManager
public static ComponentHelper.DroppedComponent dropComponentFromCompositeStackManager(javax.faces.context.FacesContext context) Drops the top component from the composite component stack manager (if present) and returns aComponentHelper.DroppedComponent. When the returnedComponentHelper.DroppedComponentis closed, the component is pushed back to the stack manager.- Parameters:
context- Current faces context.- Returns:
- Am auto-closable that pushes the dropped component back to the stack manager once closed.
-
dropComponentFromCompositeStackManager
public static ComponentHelper.DroppedComponent dropComponentFromCompositeStackManager(javax.faces.context.FacesContext context, javax.faces.component.UIComponent target) Drops the top component from the composite component stack manager (if equal to the given component) and returns aComponentHelper.DroppedComponent. When the returnedComponentHelper.DroppedComponentis closed, the component is pushed back to the stack manager.- Parameters:
context- Current faces context.- Returns:
- Am auto-closable that pushes the dropped component back to the stack manager once closed.
-
dropComponentFromCompositeStackManager
public static ComponentHelper.DroppedComponent dropComponentFromCompositeStackManager(javax.faces.context.FacesContext context, Class<? extends javax.faces.component.UIComponent> target) Drops the top component from the composite component stack manager (if an instance of the given type) and returns aComponentHelper.DroppedComponent. When the returnedComponentHelper.DroppedComponentImplis closed, the component is pushed back to the stack manager.- Parameters:
context- Current faces context.- Returns:
- Am auto-closable that pushes the dropped component back to the stack manager once closed.
-
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
-
getIdChainString
-
getSelectItems
public static List<javax.faces.model.SelectItem> getSelectItems(javax.faces.context.FacesContext context, javax.faces.component.UIComponent 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 toComponents.hasInvokedSubmit(UIComponent), but with a hacky fix for composite components that implementcc:clientBehavior.- Parameters:
component- The component to be checked.- Returns:
trueif 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. Returnsnullin 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. UseVoidfor 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:
trueif any parent of the given UI component (not including that given component) is scheduled to be rendered (updated), orfalseotherwise.
-
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)
-