Adam Bien's Weblog

Monday Apr 14, 2008

Netbeans 6.1rc1 - nice, indirect support for Eclipse / Netbeans RCP DataBinding (for the model layer)

Proprietary Eclipse RCP as well as JSR-295 (Beans Binding) databinding frameworks require the realization of PropertyChangeSupport in the "model" entities. Otherwise changes in the JPA-layer wouldn't be automatically propagated to the UI.

Netbeans 6.1rc1 is able to generate the properties,  the PropertyChangeSupport (listeners) as well as the accessors for you. You only need to enter the key-combination "Alt+Insert" and choose the "Add Property" option.  In the popup select the option "Bound" - this will activate the generation of the PropertyChangeSupport.

 

For the generation of the following code (except the annotations and the class declaration) I had only to execute the wizard twice. Onces for the "name" and second time for the "description" attribute.

 

@Entity

public class Entity {

   

    @Id

    protected String name;

    protected String description;

   

    private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport();;

   

    public static final String PROP_DESCRIPTION = "description";

    public static final String PROP_NAME = "name";

   

    public String getDescription() {

        return description;

    }

 

    public void setDescription(String description) {

        String oldDescription = description;

        this.description = description;

        propertyChangeSupport.firePropertyChange(PROP_DESCRIPTION, oldDescription, description);

    }

    public String getName() {

        return name;

    }

 

    public void setName(String name) {

        String oldName = name;

        this.name = name;

        propertyChangeSupport.firePropertyChange(PROP_NAME, oldName, name);

    }

}

 

Of course Netbeans 6.1RC1 is able to generate the whole code for you from the database with UI, binding and persistence. To try this, just use in the new project wizard the option "Desktop Application -> Database Application" and choose an existing table from the database.


NEW workshop: Microservices with Java EE 7 and Java 8, January 26th, 2015, Airport Munich

A book about rethinking Java EE Patterns

Comments:

Hello Adam,

This is a very cool feature!! I really like it, Thanks for the introduction. :-)))

There are some bugs in the code, the setter should be:

public void setName(String name) {
String oldName = getName();
this.name = name; propertyChangeSupport.firePropertyChange(PROP_NAME, oldName, name);
}

or

public void setName(String name) {
String oldName = this.name;
this.name = name; propertyChangeSupport.firePropertyChange(PROP_NAME, oldName, name);
}

because we should make sure that the old value will be saved in the local variable "oldName".

The setter of the description has the same bug.

There is another better(I think :-)) solution for such property setter:

public void setName(String name) {
propertyChangeSupport.firePropertyChange(PROPER_NAME, this.name, this.name = name);
}

best regards!/Liebe Gruesse
Jing

Posted by Jing Ge on April 22, 2008 at 11:19 AM CEST #

Source code with support for PCE is boring to write. (Yes. Beansbinding :-) There are two interesting solutions:

- http://www.damnhandy.com/javabean-aspect/
- https://izvin.bountysource.com/news/show/102

Chris

Posted by Christian Ullenboom on May 24, 2008 at 12:24 AM CEST #

Post a Comment:
  • HTML Syntax: NOT allowed
realworldpatterns.com
...the last 150 posts
...the last 10 comments
License