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};Mine is named prop, so when I am in a Java file I type
public ${TYPE} get${NAME}(){
return ${VAR};
}
public void set${NAME}(${TYPE} ${VAR}){
this.${VAR} = ${VAR};
}
${cursor}
propthen 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};It assumes you have an instance of java.beans.PropertyChangeSupport called pcs.
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}
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};That covers much of what one wants to do with beans.
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}
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
2 comments:
Hi Wade,
Would you be willing to contribute this to the Community Docs Wiki?
Do reply me back- nvarun AT NetBeans DOT org
Thanks,
Varun Nischal
NetBeans Community Docs Coordinator
http://nb-community-docs.blogspot.com/
Hi Wade,
You can make your template more general by declaring pcs as:
${PCS instanceof="java.beans.PropertyChangeSupport" default="pcs"}
Post a Comment