Make Adding Properties to Classes in NetBeans a Simpler Task
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 typeprop
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
- Login or register to post comments
- 398 reads
- Flag as offensive
- Email this Story
- Printer-friendly version
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)









Comments
Varun Nischal replied on Sun, 2008/07/20 - 6:36am
Just 1 question-
I guess I can also do the same by writing a XML file which conforms to a DTD for NB Code Templates, right? Then, do whatever is required to register that XML.
By the way,
Welcome to Dzone! ;)