Disclaimer

The views expressed on this blog are my own and do not necessarily reflect the views of Oracle.

Wednesday, September 9, 2009

Creating code templates in Jdeveloper 11g


Jdeveloper has a great feature called code templates. Jdeveloper comes with some pre-seeded code templates and provided the capability to users to define their own.

A code template is kind of short cut to generate a boiler plate code which you use heavily. For example, a for loop. You can simply type in code fori and press Ctrl + enter and it will automatically generate:

for(i=0;i<0;i++){
}

This could be a huge time saving. If you want to have look at what all code templates are available then you can go to Tools-->Preferences --> Code Editor -->Code templates. You will see a good number of typically used code templates. Some are:

To define your own code template stay in the same place and click on the Add button.
So, let say we want to define a code template for iterating using a RowSetIterator and the code could be like:

ViewObject vo = null; //actual code will get the instance from application module
RowSetIterator itr = null;
if (null != vo.findRowSetIterator("test")) {
itr = vo.findRowSetIterator("test");
}
else
{
itr = vo.createRowSetIterator("test");
itr.reset();
}

while(itr.hasNext())
{
Row row = itr.next();
//your row specific code here
}

So, to define it just type a short cut name "ri" and in description "iterate using RowSetIterator on VO"

Below in the code just paste the code given below.
ViewObject $vo$ = null; //get view object
RowSetIterator $itr$ = null;
if (null != $vo$.findRowSetIterator("test")) {
$itr$ = $vo$.findRowSetIterator("test");
}
else
{
$itr$ = $vo$.createRowSetIterator("test");
$itr$.reset();
}
while($itr$.hasNext())
{
Row $row$ = $itr$.next();
$end$
}

Where ever, you see a word enclosed in $$ that is a kind of place holder and will replace with some actual value when you will use it. There is a variables tab. As soon as you paste the above code in the code tab you will see some variables are automatically defined. In the variables tab you can change the default values as well. By default it will assign the value same as the text enclosed in $$ in your code template.

There are situations like in the above template when you may need to import some classes. To do that just go to imports tab and paste below code:
oracle.jbo.Row
oracle.jbo.RowSetIterator
oracle.jbo.ViewObject

So, above 3 classes will be automatically imported when you will type ri and press ctrl + enter along with the desired code.


1 comment:

Andrei C. said...

Thanks for sharing the information!