The Source for Java Technology Collaboration
User: Password:



Evan Summers

Evan Summers's Blog

Gooey Beans Info

Posted by evanx on December 07, 2006 at 08:37 AM | Comments (0)

Let's get us some simplistic "beans binding" to support the Presentation Model pattern. We explicitly declare our properties so as to enjoy IDE auto-completion and refactoring capabilities. "Wa wa wee wa!" Cos string references are fragile with respect to renaming getters and setters, and so we avoid them, like whatsit, the plague ;)


Code Snippet

We implement a POJO Presentation Model object as follows.

public class PersonModel {    
    private String firstNames;
    private String lastName;
    private String phone;
    private String email;
    private Date birthDate;
    private BigInteger idNumber;
    private BigDecimal creditScore;
    private Integer dependents;
    private PersonTitle title;
    private PersonGender gender;
    private PersonMaritalStatus maritalStatus;
    private Boolean confirmed;
    ...    
    @IntegerRangeValidator(minimum = 0, maximum = 9, inclusive = true)
    public void setDependents(Integer dependents) {
        this.dependents = dependents;
    }
    ...
}

where we have illustrated a validation annotation. Alternatively we specify the um, properties' properties, including validators, in an explicit bean info class, as follows. This is sans magic, quite trivial and quite programmable. "It's nice, I like. High five!"

public class PersonModelInfo extends QBeanInfo<PersonModel> {
    final QProperty firstNames = createProperty("firstNames");
    final QProperty lastName = createProperty("lastName");
    final QProperty phone = createProperty("phone");
    final QProperty email = createProperty("email");
    final QProperty birthDate = createProperty("birthDate");
    final QProperty idNumber = createProperty("idNumber");
    final QProperty creditScore = createProperty("creditScore");
    final QProperty dependents = createProperty("dependents");
    final QProperty title = createProperty("title");
    final QProperty gender = createProperty("gender");
    final QProperty maritalStatus = createProperty("maritalStatus");
    final QProperty confirmed = createProperty("confirmed");
    
    public PersonModelInfo() {
        super(PersonModel.class);
        birthDate.setFormatPattern("yyyy-MM-dd");        
        birthDate.setPropertyValueType(dateType);
        birthDate.addValidator(new QFutureDateValidator(false));
        email.setPropertyValueType(emailType);
        dependents.addValidator(new QIntegerRangeValidator(0, 9, false));
        title.setPropertyValueType(enumType);
        gender.setPropertyValueType(enumType);
        maritalStatus.setPropertyValueType(enumType);
        confirmed.setPropertyValueType(booleanType);
    }   
}

We use the above info class to refer to our properties in an explicit, refactorable fashion. When we refactor our bean ie. rename properties (ie. their getter/setter methods) then we need to change the property name here, and we're good to go. Cos they're a pair like Mutt and Jeff.

QProperty stores additional information besides wrapping the underlying PropertyDescriptor, eg. formatPattern and propertyValueType relate to formatting, and also there a list of validators.


Demo

Launch   (PersonInfo, 150k/500k, unsandboxed, Java6)

You can refresh the "BeanInfo" tab (and also the "Console" tab) to see that the values entered into our form are written to our Presentation Model.

where use formatPattern to override the default formatting for that propertyValueType (eg. date, timestamp, currency) and propertyType (eg. Date, Integer et al).



Bookmark blog post: del.icio.us del.icio.us Digg Digg DZone DZone Furl Furl Reddit Reddit
Comments
Comments are listed in date ascending order (oldest first) | Post Comment





Powered by
Movable Type 3.01D
 Feed java.net RSS Feeds