Tuesday, April 29th, 2008...2:58 pm
Easily Add PropertyChangeSupport to Beans in Eclipse
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:
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);
}
}







Leave a Reply