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:

[java]
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);
}
}
[/java]

14 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

  • i’ve changed the template to this:
    firePropertyChange(“${enclosing_method_arguments}”, this.${enclosing_method_arguments}, this.${enclosing_method_arguments} = ${enclosing_method_arguments});

    so, your field and your param must have the same name.
    works for me :D

  • This is the right web site for anyone who hopes to understand this topic.
    You realize so much its almost hard to argue with you (not that I
    really will need to…HaHa). You definitely put a fresh spin
    on a topic that’s been discussed for many years. Great stuff, just great!

  • As an enthusiast of information technology encourage any lady interested
    in IT at my blog. Using of sense professional describe I test and torturing different
    type equipment – using by with mobile phones, on
    very HP servers, IMB or Cisco. Good contacts with distributors accesses
    allow me to many gear lot before the premiere.

  • Hi there! I ust wanted to ask if you ever have any trouble with hackers?
    My last blog (wordpress) was hacked and I ended up
    losing many months of had wprk due to no data backup.

    Do you have any solutions to prevnt hackers?

  • The travel – A  maker is normally lightweight and portable so it can easily
    be taken along with you on your travels, so there is no need being fussing over big
    coffee makers. A· You want to buy your Starbucks’ cravings while paying an inferior amount.
    The filter basket is simple to remove and clean and the water
    tank may be refilled easily.

  • I enjoy what you guys are usually up too. Such clever work and
    reporting! Keep up the awesome works guys I’ve incorporated you guys to my personal blogroll.

  • Greetings from Colorado! I’m bored to tears at work so I decided to check out your site on my iphone during lunch break.
    I love the knowledge you present here and can’t wait to take a look when I get home.

    I’m surprised at how fast your blog loaded on my phone ..
    I’m not even using WIFI, just 3G .. Anyways, good blog!

  • This is the perfect web site for everyone who wishes to find out about this topic.
    You know so much its almost tough to argue with you (not that I really will need to…HaHa).

    You definitely put a new spin on a topic which has been discussed for many years.
    Great stuff, just wonderful!

  • People be worried about getting caught within the spotlight without knowing the words.
    It’s a Blue-ray capable machine and every one from the amazing aspects
    that Blue-ray offers its customers is on this machine.
    When buying a property audio receiver keep planned the issues you may add in your system in the future.

  • Hey I am so thrilled I found your blog page, I really found you by mistake, while I was researching
    on Askjeeve for something else, Regardless I am
    here now and would just like to say thank you for a tremendous post
    and a all round thrilling blog (I also love the
    theme/design), I don’t have time to browse it all at the
    moment but I have saved it and also added your RSS feeds, so when I have time
    I will be back to read much more, Please do keep up
    the superb work.

  • What’s up mates, good post and nice arguments commented at this place,
    I am truly enjoying by these.

Leave a Reply