Class Binder<BEAN>

java.lang.Object
com.oorian.data.binding.Binder<BEAN>
Type Parameters:
BEAN - the bean type

public class Binder<BEAN> extends Object
Connects Java bean properties to form fields with type conversion, validation, and dirty tracking.

A Binder manages a set of Binding objects, each linking one InputElement to one bean property. Use readBean(Object) to populate form fields from a bean and writeBean(Object) to write validated field values back.

Basic usage:


 Binder<Person> binder = new Binder<>(Person.class);

 binder.forField(nameField)
     .asRequired("Name is required")
     .bind(Person::getName, Person::setName);

 binder.forField(ageField)
     .withConverter(new StringToIntegerConverter("Not a number"))
     .bind(Person::getAge, Person::setAge);

 binder.forCheckbox(activeCheckbox)
     .bind(Person::isActive, Person::setActive);

 // Populate form
 binder.readBean(person);

 // Write back (validates first)
 binder.writeBean(person);
 

Auto-binding by field name:


 binder.bindInstanceFields(myFormView);
 
Since:
2.1
Version:
1.0
Author:
Marvin P. Warble Jr.
See Also:
  • Constructor Details

    • Binder

      public Binder(Class<BEAN> beanType)
      Creates a new Binder for the specified bean type.
      Parameters:
      beanType - the bean class (used for reflection-based auto-binding)
  • Method Details

    • forField

      public BindingBuilder<BEAN,String> forField(InputElement<?> field)
      Starts building a binding for a text-based input field or select.

      If the given field is a Select, the binding will automatically use Select.getSelectedValue() and Select.setSelectedValue(Object) for value access.

      Parameters:
      field - the input element to bind
      Returns:
      a binding builder with String as the initial target type
    • forCheckbox

      public BindingBuilder<BEAN,Boolean> forCheckbox(Checkbox<?> field)
      Starts building a binding for a checkbox field.

      The binding reads and writes Boolean values using Checkbox.isChecked() and Checkbox.setChecked(boolean).

      Parameters:
      field - the checkbox to bind
      Returns:
      a binding builder with Boolean as the initial target type
    • readBean

      public void readBean(BEAN bean)
      Populates all bound fields from the given bean.

      Reads each bound property from the bean, applies the converter's convertToPresentation method (if any), and sets the field's value.

      Parameters:
      bean - the bean to read property values from
    • writeBean

      public void writeBean(BEAN bean) throws BindingValidationException
      Validates all bindings and writes field values to the given bean.

      If any binding fails validation, a BindingValidationException is thrown containing all field errors. No values are written to the bean on failure.

      Parameters:
      bean - the bean to write property values to
      Throws:
      BindingValidationException - if one or more bindings fail validation
    • writeBeanIfValid

      public boolean writeBeanIfValid(BEAN bean)
      Validates all bindings and writes field values to the bean only if all are valid.
      Parameters:
      bean - the bean to write property values to
      Returns:
      true if all bindings passed validation and values were written
    • isModified

      public boolean isModified()
      Returns whether any bound field value has changed since the last readBean(Object).
      Returns:
      true if at least one field has been modified
    • isValid

      public boolean isValid()
      Validates all bindings and returns whether all pass.
      Returns:
      true if all bindings are valid
    • validate

      public Map<InputElement<?>,ValidationResult> validate()
      Validates all bindings and returns the results keyed by field.
      Returns:
      a map of field-to-validation-result entries
    • removeBinding

      public void removeBinding(InputElement<?> field)
      Removes the binding for the given field.
      Parameters:
      field - the field whose binding should be removed
    • getBindings

      public List<Binding<BEAN,?>> getBindings()
      Returns an unmodifiable list of all bindings.
      Returns:
      the list of bindings
    • bindInstanceFields

      public void bindInstanceFields(Object view)
      Automatically binds InputElement fields in the view object to bean properties by matching field names to getter/setter methods on the bean class.

      For each declared field in the view class that is an InputElement, this method looks for a matching getter (getXxx() or isXxx()) and setter (setXxx()) on the bean type. If a type converter is needed (e.g., the field holds a String but the bean property is an Integer), one is looked up automatically from the ConverterRegistry.

      Fields that have no matching bean property or no available converter are silently skipped.

      Parameters:
      view - the object containing InputElement fields to bind