Eclipse
Add Copyright to Java Sources
Just found a nice Eclipse plugin that adds license information to a all the files (well, you can select them) in a project. You can specify the license, enter your own, customize the output …. nice !
Just reminds me, we had a java class that did that for QAlign 2 Panta Rei source code distribution.
Yeah, there it is, May 21, 2005 written in Michas old apartment in Barcelona while working on QAlign2.
A simple AbstractBean
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.
public abstract class AbstractBean {
/**
* Create a new change support
*/
protected PropertyChangeSupport change = new EDTSafePropertyChangeSupport(this);
/**
* @param listener
* @see java.beans.PropertyChangeSupport#addPropertyChangeListener(java.beans.PropertyChangeListener)
*/
public void addPropertyChangeListener(PropertyChangeListener listener) {
change.addPropertyChangeListener(listener);
}
/**
* @param propertyName
* @param listener
* @see java.beans.PropertyChangeSupport#addPropertyChangeListener(java.lang.String, java.beans.PropertyChangeListener)
*/
public void addPropertyChangeListener(String propertyName,
PropertyChangeListener listener) {
change.addPropertyChangeListener(propertyName, listener);
}
/**
* @param propertyName
* @param oldValue
* @param newValue
* @see java.beans.PropertyChangeSupport#firePropertyChange(java.lang.String, java.lang.Object, java.lang.Object)
*/
public void firePropertyChange(String propertyName, Object oldValue,
Object newValue) {
change.firePropertyChange(propertyName, oldValue, newValue);
}
/**
* @return
* @see java.beans.PropertyChangeSupport#getPropertyChangeListeners()
*/
public PropertyChangeListener[] getPropertyChangeListeners() {
return change.getPropertyChangeListeners();
}
/**
* @param propertyName
* @return
* @see java.beans.PropertyChangeSupport#getPropertyChangeListeners(java.lang.String)
*/
public PropertyChangeListener[] getPropertyChangeListeners(
String propertyName) {
return change.getPropertyChangeListeners(propertyName);
}
/**
* @param propertyName
* @return
* @see java.beans.PropertyChangeSupport#hasListeners(java.lang.String)
*/
public boolean hasListeners(String propertyName) {
return change.hasListeners(propertyName);
}
/**
* @param listener
* @see java.beans.PropertyChangeSupport#removePropertyChangeListener(java.beans.PropertyChangeListener)
*/
public void removePropertyChangeListener(PropertyChangeListener listener) {
change.removePropertyChangeListener(listener);
}
/**
* @param propertyName
* @param listener
* @see java.beans.PropertyChangeSupport#removePropertyChangeListener(java.lang.String, java.beans.PropertyChangeListener)
*/
public void removePropertyChangeListener(String propertyName,
PropertyChangeListener listener) {
change.removePropertyChangeListener(propertyName, listener);
}
/**
* Simply ensures that events are delivered through the event dispatching thread
*
*/
class EDTSafePropertyChangeSupport extends PropertyChangeSupport{
public EDTSafePropertyChangeSupport(Object sourceBean) {
super(sourceBean);
}
@Override
public void firePropertyChange(final PropertyChangeEvent evt) {
if(SwingUtilities.isEventDispatchThread()){
super.firePropertyChange(evt);
}else{
SwingUtilities.invokeLater(new Runnable() {
public void run() {
firePropertyChange(evt);
}
});
}
}
}
}
This implementation restricts itself to the firePropertyChange(String,Object,Object) method. It also provide a custom extension of PropertyChangeSupport that ensures that events are fired in the EventDispatchingThread.
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.
Package sorter plugin
While working on EPoS, I found it very confusing that Eclipse does not sort multiple source directories alphabetically. The little PackageSorter plugin helps here and can be used in Eclipse Ganymede (>=3.4). The plugin is available on our update site:
http://java.randgestalten.de/updatesite
Add this site in the Eclipse Software Update dialog and install the Packagesorter plugin. If the plugin is successfully installed, it provides a button in to top toolbar of the java package explorer. If activated, it will sort your source directories in alphabetic order instead of using the build-path order, which seems to be the default.
As long as you only deal with one source directory, you will see no use in this plugin, but when your project consists of multiple source directories, which are all included in the projects build path, this might be helpful.
Eclipse Plugins update site
I just set up an update site for our tiny eclipse plugins. The update site URL is
http://java.randgestalten.de/updatesite
and it includes the package sorter plugin and the bounded setter generator – and might include more at some point – we’ll see.



