Tuesday, April 29th, 2008...2:58 pm

Easily Add PropertyChangeSupport to Beans in Eclipse

Jump to Comments

If you plan on using JFace Databinding in SWT you will have to implement Property Change Support on all your Java Beans, (barf). I wasn't able to find a plugin that would do this in one click to a class so I came up with two ways to do it.

Here's a code template you can use based on the cool trick I learned at Stuff That Happens

firePropertyChange("${enclosing_method_arguments}", this.${enclosing_method_arguments}, ${line_selection});

Just select the assignment part of your setter and use this template.

If you're really lazy you can use a regex find and replace (With the File Search Dialog)

Your Find string will be:
(this.+) = (\w+);

And your replace string will be:
firePropertyChange("$2", $1, $1 = $2);

Both of these solutions assume you use an Abstract BaseClass to set up your firePropertyChange stuff, then extend for each bean.

Mine looks like this:

package com.weheartcode;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;

public abstract class BaseEntity {
    private transient PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(
            this);

    /**
     * Adds the property change listener.
     *
     * @param listener the listener
     */

    public void addPropertyChangeListener(PropertyChangeListener listener) {
        propertyChangeSupport.addPropertyChangeListener(listener);
    }

    /**
     * Adds the property change listener.
     *
     * @param propertyName the property name
     * @param listener the listener
     */

    public void addPropertyChangeListener(String propertyName,
            PropertyChangeListener listener) {
        propertyChangeSupport.addPropertyChangeListener(propertyName, listener);
    }

    /**
     * Removes the property change listener.
     *
     * @param listener the listener
     */

    public void removePropertyChangeListener(PropertyChangeListener listener) {
        propertyChangeSupport.removePropertyChangeListener(listener);
    }

    /**
     * Removes the property change listener.
     *
     * @param propertyName the property name
     * @param listener the listener
     */

    public void removePropertyChangeListener(String propertyName,
            PropertyChangeListener listener) {
        propertyChangeSupport.removePropertyChangeListener(propertyName,
                listener);
    }

    /**
     * Fire property change.
     *
     * @param propertyName the property name
     * @param oldValue the old value
     * @param newValue the new value
     */

    protected void firePropertyChange(String propertyName, Object oldValue,
            Object newValue) {
        propertyChangeSupport.firePropertyChange(propertyName, oldValue,
                newValue);
    }
}

5 Comments

  • [...] In many cases you need to have bound properties while working with JavaBeans. It might be easy to add PropertyChangeSupport but this does not solve the hassle of modifying your setter methods to fire change events. Of course you can do that with a search and replace pattern, but this is not a very nice and intuitive solution. A better way to address the problem is changing the Eclipse template for setter methods. This works quit well and you can keep things short, but you will loose the ability to create unbound setter methods. Of course, you can modify the template on a per project bases, but in my case, I always end up in situations where I need both bound and unbound properties. [...]

  • I wrote an eclipse plugin to automatically generate bounded setters. For the moment you bean has to provide a firePropertyChange method and you can use the plugin to generate getters and bounded setters exactly the same way you would do it for unbounded setters. You get a a custom template that you can modify and you do not loose the ability to generate unbound setters.

    check it out

    http://java.randgestalten.de/?p=10

    cheers

  • Порно интим знакомства, интим знакомства forum, интим знакомство краснодар, секс интим знакомства, интим знакомства фото, знакомства интим yabb, интим знакомства уфа, самара знакомства интим, интим знакомства петербург, интим знакомства саратова, знакомства интим нижний, интим знакомства москва, знакомства интим threads, питер интим знакомства, интим знакомства ростов, знакомства интим showthread php, знакомства интим minibb, интим знакомства question index

  • Интим интим знакомства, интим знакомства forum, интим знакомство краснодар, секс интим знакомства, интим знакомства фото, знакомства интим yabb, интим знакомства уфа, самара знакомства интим, интим знакомства петербург, интим знакомства саратова, знакомства интим нижний, интим знакомства москва, знакомства интим threads, питер интим знакомства, интим знакомства ростов, знакомства интим showthread php, знакомства интим minibb, интим знакомства text index

  • Отдается в хорошие руки очень красивый щенок. Девочка. Возраст 2 месяца.Доверчивая, дружелюбная, ласковая и преданная.Очень умная, воспитанная, и очень добрая.Отдадим только добрым и любящим хозяевам.
    8-916-118-60-95

Leave a Reply