Making JGoodies Easier With Client-Objects

JGoodies is a selection of easy to use libraries for GUI development. One of which is JGoodies Binding. This allows you to bind your UI components to your domain objects and have changes in one reflected in the other.

You'd have to be a basket case not to automate - Image from drogpatravel on Flickr

You'd have to be a basket case not to automate - Image from drogpatravel on Flickr

The only downside to using it is the requirement that the POJOs you bind to have support for bound properties. Adding the support, even with the assistance of the JGoodies Model class, is error prone and tedious. Especially if you’re generating POJOs from a database or retrofitting the support to existing classes.

Example of a class where you manually add support:

public class Person extends Model {//Model is a JGoodies class that takes care of some bound properties boilerplate code
    private String name;
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        //Adding this code for every attribute is tedious and error prone.
        String oldValue = this.name;
        this.name = name;
        firePropertyChange("name", oldValue, name);
    }
}

To the rescue comes Client-Objects. This library allows you to dynamically add the property change support to your POJOs at runtime. All the bound property boilerplate code disappears, replaced by a single call that is made after you’ve created the object (manually or through a framewework, for example JPA). Here’s an example of hiding the code behind a utility class:

public class UIBeanEnhancer {
    public static  E enhance(E bean) {
        BeanRegistry.registerType(bean.getClass());
        return BeanEnhancer.addPropertyChangeSupport(bean);
    }
}
This entry was posted in Programming and tagged , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *