2008-07-16

Make adding properties to classes in NetBeans a simpler task

NetBeans has a great feature called Code Templates. This is used to create shortcuts which may be typed in the editor then expanded into a template which the editor will ask the user to fill in the blanks. There are many useful ones which come configured upon installation within the NetBeans IDE, but one I have used for a long time, yet never written about except on the NetBeans mailing lists, is one to create properties more easily.

These properties are plain properties un-bounded and not incorporating property change events, but none the less very useful, and it isn't much work to transform into your choice of other useful property creating templates. Without further delay, my code template is:
private ${TYPE} ${VAR} = ${VAL};

public ${TYPE} get${NAME}(){
return ${VAR};
}

public void set${NAME}(${TYPE} ${VAR}){
this.${VAR} = ${VAR};
}

${cursor}
Mine is named prop, so when I am in a Java file I type
prop
then press the TAB button/key. The template is expanded and asks me to fill in the details. Below are some screen samples of this in action.

I simply press the TAB button to jump between the fields TYPE,VAR, and VAL, enter the values, and when I have them all filled in press the ENTER button and my cursor is placed at the ${cursor} position or offset in the editor.

The final result is a read-write property with the source code all laid out nicely


To expand the function of the template is easy enough. I have one which adds the logic for property change events. I have it named eprop in my IDE.
private ${TYPE} ${VAR} = ${VAL};

public ${TYPE} get${NAME}(){
return ${VAR};
}

public void set${NAME}(${TYPE} ${VAR}){
${TYPE} lold${VAR} = this.${VAR};
this.${VAR} = ${VAR};
pcs.firePropertyChange("${VAR}",lold${VAR},${VAR});
}

${cursor}
It assumes you have an instance of java.beans.PropertyChangeSupport called pcs.

I have another one named ewprop which stands for Wrap or Wrapper, and in this case another field is needed to handle wrapping primitive types to pass the call to firePropertyChange, and it also assumes a variable named pcs:
private ${TYPE} ${VAR} = ${VAL};

public ${TYPE} get${NAME}(){
return ${VAR};
}

public void set${NAME}(${TYPE} ${VAR}){
${TYPE} lold${VAR} = this.${VAR};
this.${VAR} = ${VAR};
pcs.firePropertyChange("${VAR}",new ${WTYPE}(lold${VAR}),new ${WTYPE}(${VAR}));
}

${cursor}
That covers much of what one wants to do with beans.

One thing still missing from the pre 6.0 or 5.5 days is the ability to easily manage JavaBean patterns. This involves things such as being able to rename a property and have the field and method names change at once. The ability to add JavaBean properties has been added back to the IDE at least, but these code templates I'm writing about are easier to use, or at least quicker, than the UI to add properties through the Java editor once you are used to using them, but maybe that can be remedied by adding a quick pop up menu for doing things with JavaBean patterns to the Java editor; if I have time this year that can be one of my community contributions :-D