Class ContainerUtils
- java.lang.Object
-
- org.openidentityplatform.openam.click.util.ContainerUtils
-
public class ContainerUtils extends Object
Provides Container access and copy utilities.
-
-
Constructor Summary
Constructors Constructor Description ContainerUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static voidcopyContainerToObject(Container container, Object object)Populate the given object attributes from the Containers field values.static voidcopyContainerToObject(Container container, Object object, List<Field> fieldList)Populate the given object attributes from the Containers field values.static voidcopyObjectToContainer(Object object, Container container)Populate the given Container field values from the object attributes.static voidcopyObjectToContainer(Object object, Container container, List<Field> fieldList)Populate the given Container field values from the object attributes.static org.openidentityplatform.openam.click.ControlfindControlByName(Container container, String name)Find and return the first control with a matching name in the specified container.static FormfindForm(org.openidentityplatform.openam.click.Control control)Find and return the specified controls parent Form or null if no Form is present.static List<Button>getButtons(Container container)Return the list of Buttons for the given Container, recursively including any Fields contained in child containers.static List<Field>getErrorFields(Container container)Return a list of container fields which are not valid, not hidden and not disabled.static Map<String,Field>getFieldMap(Container container)Return a map of all Fields for the given Container, recursively including any Fields contained in child containers.static List<Field>getFields(Container container)Return the list of Fields for the given Container, recursively including any Fields contained in child containers.static List<Field>getFieldsAndLabels(Container container)Return the list of Fields for the given Container, recursively including any Fields contained in child containers.static List<Field>getHiddenFields(Container container)Return the list of hidden Fields for the given Container, recursively including any Fields contained in child containers.static List<Field>getInputFields(Container container)Return the list of input Fields (TextField, Select, Radio, Checkbox etc).static org.openidentityplatform.openam.click.Controlinsert(Container container, org.openidentityplatform.openam.click.Control control, int index, Map<String,org.openidentityplatform.openam.click.Control> controlMap)Add the given control to the container at the specified index, and return the added instance.static booleanremove(Container container, org.openidentityplatform.openam.click.Control control, Map<String,org.openidentityplatform.openam.click.Control> controlMap)Remove the given control from the container, returningtrueif the control was found in the container and removed, orfalseif the control was not found.static org.openidentityplatform.openam.click.Controlreplace(Container container, org.openidentityplatform.openam.click.Control currentControl, org.openidentityplatform.openam.click.Control newControl, int controlIndex, Map<String,org.openidentityplatform.openam.click.Control> controlMap)Deprecated.this method was used for stateful pages, which have been deprecated
-
-
-
Method Detail
-
copyContainerToObject
public static void copyContainerToObject(Container container, Object object, List<Field> fieldList)
Populate the given object attributes from the Containers field values.If a Field and object attribute matches, the object attribute is set to the Object returned from the method
Field.getValueObject(). If an object attribute is a primitive, the Object returned fromField.getValueObject()will be converted into the specific primitive e.g. Integer will become int and Boolean will become boolean.The fieldList specifies which fields to copy to the object. This allows one to include or exclude certain Container fields before populating the object.
The following example shows how to exclude disabled fields from populating a customer object:
public void onInit() { List formFields = new ArrayList(); for(Iterator it = form.getFieldList().iterator(); it.hasNext(); ) { Field field = (Field) formFields.next(); // Exclude disabled fields if (!field.isDisabled()) { formFields.add(field); } } Customer customer = new Customer(); ContainerUtils.copyContainerToObject(form, customer, formFields); }The specified Object can either be a POJO (plain old java object) or aMap. If a POJO is specified, its attributes are populated from matching container fields. If a map is specified, its key/value pairs are populated from matching container fields.- Parameters:
container- the fieldList Containerobject- the object to populate with field valuesfieldList- the list of fields to obtain values from- Throws:
IllegalArgumentException- if container, object or fieldList is null
-
copyContainerToObject
public static void copyContainerToObject(Container container, Object object)
Populate the given object attributes from the Containers field values.- Parameters:
container- the Container to obtain field values fromobject- the object to populate with field values- See Also:
copyContainerToObject(org.openidentityplatform.openam.click.control.Container, java.lang.Object, java.util.List)
-
copyObjectToContainer
public static void copyObjectToContainer(Object object, Container container, List<Field> fieldList)
Populate the given Container field values from the object attributes.If a Field and object attribute matches, the Field value is set to the object attribute using the method
Field.setValueObject(java.lang.Object). If an object attribute is a primitive it is first converted to its proper wrapper class e.g. int will become Integer and boolean will become Boolean.The fieldList specifies which fields to populate from the object. This allows one to exclude or include specific fields.
The specified Object can either be a POJO (plain old java object) or a
Map. If a POJO is specified, its attributes are copied to matching container fields. If a map is specified, its key/value pairs are copied to matching container fields.- Parameters:
object- the object to obtain attribute values fromcontainer- the Container to populatefieldList- the list of fields to populate from the object attributes
-
copyObjectToContainer
public static void copyObjectToContainer(Object object, Container container)
Populate the given Container field values from the object attributes.- Parameters:
object- the object to obtain attribute values fromcontainer- the Container to populate- See Also:
copyObjectToContainer(java.lang.Object, org.openidentityplatform.openam.click.control.Container, java.util.List)
-
findControlByName
public static org.openidentityplatform.openam.click.Control findControlByName(Container container, String name)
Find and return the first control with a matching name in the specified container.If no matching control is found in the specified container, child containers will be recursively scanned for a match.
- Parameters:
container- the container that is searched for a control with a matching namename- the name of the control to find- Returns:
- the control which name matched the given name
-
findForm
public static Form findForm(org.openidentityplatform.openam.click.Control control)
Find and return the specified controls parent Form or null if no Form is present.- Parameters:
control- the control to check for Form- Returns:
- the controls parent Form or null if no parent is a Form
-
getButtons
public static List<Button> getButtons(Container container)
Return the list of Buttons for the given Container, recursively including any Fields contained in child containers.- Parameters:
container- the container to obtain the buttons from- Returns:
- the list of contained buttons
-
getErrorFields
public static List<Field> getErrorFields(Container container)
Return a list of container fields which are not valid, not hidden and not disabled.The list of returned fields will exclude any
Buttonfields.- Parameters:
container- the container to obtain the invalid fields from- Returns:
- list of container fields which are not valid, not hidden and not disabled
-
getFieldMap
public static Map<String,Field> getFieldMap(Container container)
Return a map of all Fields for the given Container, recursively including any Fields contained in child containers.The map's key / value pair will consist of the control name and instance.
- Parameters:
container- the container to obtain the fields from- Returns:
- the map of contained fields
-
getFields
public static List<Field> getFields(Container container)
Return the list of Fields for the given Container, recursively including any Fields contained in child containers.- Parameters:
container- the container to obtain the fields from- Returns:
- the list of contained fields
-
getFieldsAndLabels
public static List<Field> getFieldsAndLabels(Container container)
Return the list of Fields for the given Container, recursively including any Fields contained in child containers. The list of returned fields will exclude anyButtonfields.- Parameters:
container- the container to obtain the fields from- Returns:
- the list of contained fields
-
getHiddenFields
public static List<Field> getHiddenFields(Container container)
Return the list of hidden Fields for the given Container, recursively including any Fields contained in child containers. The list of returned fields will exclude anyButtonandLabelfields.- Parameters:
container- the container to obtain the fields from- Returns:
- the list of contained fields
-
getInputFields
public static List<Field> getInputFields(Container container)
Return the list of input Fields (TextField, Select, Radio, Checkbox etc). for the given Container, recursively including any Fields contained in child containers. The list of returned fields will exclude anyButtonandLabelfields.- Parameters:
container- the container to obtain the fields from- Returns:
- the list of contained fields
-
insert
public static org.openidentityplatform.openam.click.Control insert(Container container, org.openidentityplatform.openam.click.Control control, int index, Map<String,org.openidentityplatform.openam.click.Control> controlMap)
Add the given control to the container at the specified index, and return the added instance.Please note: an exception is raised if the container contains a control with the same name as the given control. It is the responsibility of the caller to replace existing controls.
Also note if the specified control already has a parent assigned, it will automatically be removed from that parent and inserted as a child of the container instead.
This method is useful for developers needing to implement the
Containerinterface but cannot for one reason or another extend fromAbstractContainer. For example if the Container already extends from an existingControlsuch as aField, it won't be possible to extendAbstractContaineras well. In such scenarios instead of reimplementinginsert, one can delegate to this method.For example, a custom Container that extends
Fieldand implementsContainercould implement theinsertmethod as follows:public class MyContainer extends Field implements Container { public Control insert(Control control, int index) { return ContainerUtils.insert(this, control, index, getControlMap()); } ... }- Parameters:
container- the container to insert the given control intocontrol- the control to add to the containerindex- the index at which the control is to be insertedcontrolMap- the container's map of controls keyed on control name- Returns:
- the control that was added to the container
- Throws:
IllegalArgumentException- if the control is null or if the control and container is the same instanceIndexOutOfBoundsException- if index is out of range(index < 0 || index > container.getControls().size())
-
replace
public static org.openidentityplatform.openam.click.Control replace(Container container, org.openidentityplatform.openam.click.Control currentControl, org.openidentityplatform.openam.click.Control newControl, int controlIndex, Map<String,org.openidentityplatform.openam.click.Control> controlMap)
Deprecated.this method was used for stateful pages, which have been deprecatedReplace the current control in the container at the specified index, and return the newly added control.Please note if the new control already has a parent assigned, it will automatically be removed from that parent and inserted as a child of the container instead.
This method is useful for developers needing to implement the
Containerinterface but cannot for one reason or another extend fromAbstractContainer. For example if the Container already extends from an existingControlsuch as aField, it won't be possible to extendAbstractContaineras well. In such scenarios instead of reimplementingreplace, one can delegate to this method.For example, a custom Container that extends
Fieldand implementsContainercould implement thereplacemethod as follows:public class MyContainer extends Field implements Container { public Control replace(Control currentControl, Control newControl) { int controlIndex = getControls().indexOf(currentControl); return ContainerUtils.replace(this, currentControl, newControl, controlIndex, getControlMap()); } ... }- Parameters:
container- the container to insert the new control intocurrentControl- the control currently contained in the containernewControl- the control to replace the current control contained in the containercontrolIndex- the index of the current control in the containercontrolMap- the container's map of controls keyed on control name- Returns:
- the new control that replaced the current control
- Throws:
IllegalArgumentException- if the currentControl or newControl is nullIllegalStateException- if the controlIndex = -1
-
remove
public static boolean remove(Container container, org.openidentityplatform.openam.click.Control control, Map<String,org.openidentityplatform.openam.click.Control> controlMap)
Remove the given control from the container, returningtrueif the control was found in the container and removed, orfalseif the control was not found.This method is useful for developers needing to implement the
Containerinterface but cannot for one reason or another extend fromAbstractContainer. For example if the Container already extends from an existingControlsuch as aField, it won't be possible to extendAbstractContaineras well. In such scenarios instead of reimplementingremove, one can delegate to this method.For example, a custom Container that extends
Fieldand implementsContainercould implement theremovemethod as follows:public class MyContainer extends Field implements Container { public boolean remove (Control control) { return ContainerUtils.remove(this, control, getControlMap()); } ... }- Parameters:
container- the container to remove the given control fromcontrol- the control to remove from the containercontrolMap- the container's map of controls keyed on control name- Returns:
- true if the control was removed from the container
- Throws:
IllegalArgumentException- if the control is null
-
-