Friday, November 12, 2010

Naked Objects Templates in NetBeans

Within the Naked Objects distribution you can find some Eclipse code templates that can speed up your work. Of course also in NetBeans you can have code templates, but I didn't know (and still don't) how to transform the lowercase property in a camel-case get or set method. After envying the Eclipse users for a while I decided to give it a try myself. I was quite deluded, as neither the Eclipse templates allow you to do it. This pushed me to implement the templates myself, and obviously to share my results.

The very first template is probably nop, which stands for Naked Objects Property. This basically creates a property with its setter and (annotated with @MemberOrder) getter. The problem is that I like to keep fields separated from setters and getters, so a template was not the best solution (at least not for me). The nop templates was then replaced with the ready-made ALT+INS, Add Property...


This opens a form that lets you specify the property name, type, and tweak the rest of the generated code a little bit.

And so much for the first template, as all I still need to do is to manually add the @MemberOrder annotation on the getter (but the autocompletion feature is of great help, as a simple @Mem followed by CTRL+space is normally enough).

To create a new code template you have to click on the Tools menu, select the Options item, and when the form opens click on the Code templates tab:


A click on the New button, some clickety clackety and a click on the OK button and there you have it: a brand new template all for you. The templates I've created so far:
  • noft (Naked Objects Factory Transient)
/** Creates a new transient ${Type} */
public ${Type} new${Type}(){
${Type} ${instance} = newTransientInstance(${Type}.class);
return ${instance};
}
  • noidicon (Naked Objects Identification Icon)
/** The name of the icon that represents the object */
public String iconName(){
return "${iconName}";
}
  • noidtitle (Naked Objects Identification Title)
/** A description of the object */
public String title(){
TitleBuffer buf = new TitleBuffer();
// TODO add properties
${cursor}
return buf.toString();
}
Note the use of the reserved ${cursor} property that positions the cursor where you are supposed to add your custom code.
  • nopval (Naked Objects Property Validation)
/** Validation for property ${property} */
public String validate${Property}(${Type} ${value}){
if ((${value} == null)) {
return null;
}
// TODO implement imperative rule
${cursor}
return null;
}
Last but not least
  • nopval (Naked Objects Property Validation)
/** Find all ${TypePlural} */
@Exploration
public List<${Type}> findAll${TypePlural}(){
return allInstances(${Type}.class);
}

I'm sure I can do better than this, e.g. there should be a way to automatically fix imports without the need for me to hit SHIFT+CTRL+I - which, anyway, is not all that work, moreover I'm more than used to combine it with ALT+SHIFT+F to format code and CTRL+S to save whenever I type something.

As with all code templates, use the Tab key to switch from a variable to the next one and hit Enter when you're done.

Maybe more on this will follow.

1 comment:

Robert said...

That is very nice. Please post if you tweak it more.