net.sf.click.util
Class ContainerUtils

java.lang.Object
  extended bynet.sf.click.util.ContainerUtils

public class ContainerUtils
extends Object

Provides Container access and copy utilities.

Author:
Bob Schellink

Constructor Summary
ContainerUtils()
           
 
Method Summary
static void copyContainerToObject(Container container, Object object)
          Populate the given object attributes from the Containers field values.
static void copyContainerToObject(Container container, Object object, List fieldList)
          Populate the given object attributes from the Containers field values.
static void copyObjectToContainer(Object object, Container container)
          Populate the given Container field values from the object attributes.
static void copyObjectToContainer(Object object, Container container, List fieldList)
          Populate the given Container field values from the object attributes.
static Control findControlByName(Container container, String name)
          Find and return the first control with a matching name in the specified container.
static Form findForm(Control control)
          Find and return the specified controls parent Form or null if no Form is present.
static List getButtons(Container container)
          Return the list of Buttons for the given Container, recursively including any Fields contained in child containers.
protected  int getDepth(Control control)
          Return how deep the control is in the container hierarchy.
static List getErrorFields(Container container)
          Return a list of container fields which are not valid, not hidden and not disabled.
static Map getFieldMap(Container container)
          Return a map of all Fields for the given Container, recursively including any Fields contained in child containers.
static List getFields(Container container)
          Return the list of Fields for the given Container, recursively including any Fields contained in child containers.
static List getFieldsAndLabels(Container container)
          Return the list of Fields for the given Container, recursively including any Fields contained in child containers.
static List getHiddenFields(Container container)
          Return the list of hidden Fields for the given Container, recursively including any Fields contained in child containers.
static List getInputFields(Container container)
          Return the list of input Fields (TextField, Select, Radio, Checkbox etc).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ContainerUtils

public ContainerUtils()
Method Detail

getButtons

public static List 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

getFields

public static List 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

getInputFields

public static List 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 any Button, FieldSet and Label fields.

Parameters:
container - the container to obtain the fields from
Returns:
the list of contained fields

getFieldsAndLabels

public static List 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 any Button and FieldSet fields.

Parameters:
container - the container to obtain the fields from
Returns:
the list of contained fields

getFieldMap

public static Map 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

getHiddenFields

public static List 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 any Button, FieldSet and Label fields.

Parameters:
container - the container to obtain the fields from
Returns:
the list of contained fields

getErrorFields

public static List 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 Button fields.

Parameters:
container - the container to obtain the invalid fields from
Returns:
list of container fields which are not valid, not hidden and not disabled

findForm

public static Form findForm(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

findControlByName

public static Control findControlByName(Container container,
                                        String name)
Find and return the first control with a matching name in the specified container.

Parameters:
container - the container that is checked for controls for matching names
name - the name of the control to find
Returns:
the control with a matching name

copyContainerToObject

public static void copyContainerToObject(Container container,
                                         Object object,
                                         List 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 from Field.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 a Map. 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 Container
object - the object to populate with field values
fieldList - 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 from
object - the object to populate with field values
See Also:
copyContainerToObject(net.sf.click.control.Container, java.lang.Object, java.util.List)

copyObjectToContainer

public static void copyObjectToContainer(Object object,
                                         Container container,
                                         List 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 from
container - the Container to populate
fieldList - 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 from
container - the Container to populate
See Also:
copyObjectToContainer(java.lang.Object, net.sf.click.control.Container, java.util.List)

getDepth

protected int getDepth(Control control)
Return how deep the control is in the container hierarchy.

Parameters:
control - the control which depth to return
Returns:
the depth of the control in the container hierarchy