11/07/2009

Java Editor Templates

Category   
Bookmark : del.icio.us  Technorati  Digg This  Add To Furl  Add To YahooMyWeb  Add To Reddit  Add To NewsVine 

Templates are a useful feature of the Java editor in Eclipse. Templates are a simple way to define common coding patterns and then write your code in a fill-in-the-blanks style.

For information about templates the Java developers user guide is a good reference and this Eclipse Zone article is a good tutorial.

The rest of this blog will discuss a few Lotus Notes specific templates.

To install the templates download nd_java_template.xml and import it into eclipse via Window -> Preferences -> Java -> Editor -> Templates -> Import... You will now see a set of new templates, named nd_*****. Sadly, the import is not very smart. If you import the file twice you will end up with duplicate templates.

screen0.jpg

The templates included are:
Name Description
nd_job Create a NotesJob and Session
nd_thread Create a NotesThread and Session
nd_create_doc Create and save a new Document
nd_dc_loop Loop through all the Documents in a DocumentCollection
nd_dd_loop Loop through all the Databases in a DbDirectory
nd_nc_loop Loop through all the Notes in a NoteCollection
nd_ve_loop Loop through all the ViewEntries in a ViewEntryCollection
nd_read_text_file Read text from a file using a Stream
nd_write_text_file Write text to a file using a Stream


Previous versions of the xml file:  
nd_java_templates_20090711.xml

To use a template type nd in the Java editor and press <ctrl>+<space> to invoke content assist. A list of all the valid templates will be displayed together with a brief description and a sample of the code it produces.

screen1.jpg

The Java editor inserts the code template into our source and highlights the fill-in-the-blanks or variables by drawing a box round them. The first variable, viewEntry is selected and highlighted in dark blue. Other occurrences of the variable are highlighted in light blue. If we are happy with the name of the variable just press tab to move to the next one. To change the variable, over-type the suggested name with a new one. If you do this all occurrences of the variable are also changed.

screen2.jpg

When we tab to the second variable the Java editor knows that the variable must be an existing ViewEntryCollection object so it prompts us with a list of all the valid objects in the current piece of code.

screen4.jpg

Pressing the tab key again takes us to the temp variable. Press tab once more and the cursor is positioned after the while statement ready for us to start typing some code to process the ViewEntry.

screen5.jpg

If anyone has suggestions for other Notes code templates let me know and I will add them to the xml file.