Saturday, June 29, 2013

Using the Code Templates in JDev


Jdeveloper comes with few pre-configured code templates and has provided the capability to users to define their own templates if needed.

For Example, you can simply type in code 'sop' and press Ctrl + Enter keys and JDev will automatically generate:
System.out.println();

So basically code templates are useful whenever we need to use certain pieces of code over and over again.

To add custom code templates which you use often use the following steps:
Lets create a template for the widely used partial target feature-
AdfFacesContext.getCurrentInstance().addPartialTarget();

STEP 1:
In Jdeveloper : Tools>Preferences>CodeEditor>CodeTemplates


STEP 2:
Click on the Plus icon for adding the code template:


 
STEP 3:

Enter the preferred shortcut for the template and the context of the template , for instance JavaScript,Java Statements etc 


 
STEP 4:
In the code tab enter the code to populate along with the placeholders(enclosed between $ symbols) if any.If the code depends on any imports , we can put them in the Imports tab.



Step 5:
In the Variables tab we can define default values for the placeholders if needed and also specify if it is editable or not. Click on OK.

Now after configuring the template, we can now use them directly in the Bean code or any Java statements in Jdev. In this case if we type 'aptr' and press Ctrl +Enter Keys , the template is populated along with the specified import statements.

NOTE:
We can also Export the template and share with other users. Export creates an XML file which can be imported back anytime.
 



Check the import for logging code templates here: