generate bounded setters plugin
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 searched the net to find an Eclipse plugin that does the job of creating bound properties, but I didn’t find anything. So….I wrote my own plugin. The plugin works with Eclipse Ganymede (>=3.4) and can be installed using this update site:
http://java.randgestalten.de/updatesite
At the moment, the the plugin assumes that your bean provides a firePropertyChange(String, Object, Object) method. Thats one drawback I will try to address in the next version. For the moment, I think a good solution is to provide an AbstractBean class that you extend when you write a JavaBean from scratch ( here is an example of such class).
As long as your class provides a firePropertyChange method, you can use the plugin to generate bounded getter/setter methods. The plugin uses the default template for getter methods and provides a new template for bounded setters. You find the template in the Java->Editor->Templates preferences, where you can change it according to your needs.
Using the plugins mechanism to create getter/setter is straight forward, integrated in the “Source” menu of the context popup in the java editor as well as the package explorer and outline views. Just start writing your bean, right-click in the editor and select Source->Generate bounded getters/setters. This will open the same dialog as the default getter/setter generation except that it uses the boundedsetter template to create the setter method.
Beside the integration into the context menu, the plugin also supports quickfix and quickassist. Move your cursor to a variable definition and hit ctrl-1 (or command-1 if you are on a mac) to open the quickfix menu. The plugin integrates a bounded getter/setter generation quickfix that follows the same rules as the default generation behavior. You will see the same dialog except that you will get a bounded setter when you hit the Ok button.
So, as said before, the plugin integrates nicely into the Eclipse mechanism of generating getter/setter methods. You have a custom template just for the bounded setters and all dialogs are basically the same.
The thing that is missing currently is the automatic generation of the property change support or some delegation methods. This involves more work, because we have to check for existing methods…create property change support….insert delegation methods etc. I will try to build that in the next version, but for the moment you have to provide that by yourself. Either let your Bean extend an AbstractBean implementation that already supports property changes or integrate it directly into the object using PropertyChangeSupport. Keep in mind that plugin currently expects a firePropertyChange(String,Object,Object) method.
EDIT
The plugins source code is available here.
UPDATE
I just checked, and it seems that the Plugin is also working with Eclipse 3.5 but you have to uncheck the “Group items by category” to get the plugins listed.


[...] Just in case, here is a simple example of an AbstractBean class that can be used as a base class for JavaBeans if you want to use the Eclipse bouded setter plugin. [...]
Thanks! I was too lazy to do this on my own.
[...] I had a nice live template to create a firePropertyChange statement. This is not as nice as the bound setter plugin for eclipse as it does not create the method but the methods [...]
Is the source code available somewhere?
Yes, the source code is available here.
Is the site still up? I couldn’t install the plugin. Thanks for the effort. Now if I could just find a plug-in to generate indexed property getters and setters. PS – there’s a really nice AbstractBean class in the swingx library: https://swingx.dev.java.net/source/browse/swingx/tags/SwingX-1-0/src/java/org/jdesktop/beans/AbstractBean.java?rev=3358&view=markup
Yes, I just checked and the update site is still available hat http://java.randgestalten.de/updatesite. I just installed the plugin in Eclipse 3.5 on a mac. There is a minor problem with the categorization. You have to uncheck the box “Group Items by Category” in the install dialog. Otherwise the plugins will not be listed.
Yes, I know the AbstractBean from SwingX, but I feel like AbstractBean is something I am going to write for each and every Project
I don’t like it, but tats just because I think it is something essential that is presented by a good implementation in a base library that I use for all my Projects. SwingX comes close enough, but the AbstractBean is almost always one of the first Classes I write for UI projects and at that point I usually do not want to integrate any libraries just for that one Class.
The indexed property generation is another thing. I don’t think it would be that hard. If you are interested, take a look at the source code of the plugin.
cheers
-thasso