In the last week I created my first Eclipse extension point template. Once again I am amazed with the ease and power of another rarely mentioned yet powerful Eclipse PDE feature. After going through the exercise I think all extensions should have at least one template. I have an extension point that takes in a class file (which implements an interface), an XML file (WSDL to be exact) and then some other attributes. The problem with the extension point is the WSDL has custom bindings that do not come for free with the WTP WSDL editor. So in short, unless the WTP WSDL editor is extended, creating this WSDL from scratch can be cumbersome. I also think the linking of the class with whatever interface is needed should also come for free.
What is a template you ask?
Here is a screen shot of what I am talking about:
You can have as many templates as you want for any of your extension points. The ActionSets extension in that screen shot creates the extension and the java files associated with them. I actually referenced the Eclipse code in the UI package to figure out how to do this (once again, open source allowed for this). I can not say how valuable it is to be able to just look at source and figure things out. Unfortunately the quick searches I did for this subject yield poor results (of which I am sure someone will respond to this and provide better search results).
You can look at a lot of good template samples under the package org.eclipse.pde.internal.ui.wizards.templates in the org.eclipse.pde.ui plugin. The SampleAction template is what is displayed in that screen shot above.
What makes this concept so interesting is how you can put tokens in the file names and code that are wrapped in $’s and they will automatically be replaced by the fields you specify on the wizard forms. For instance, you add a text field to the wizard page and give the field the name className and wherever “$className$” is in the template directories it will be replaced with the users input.
In the case of the ActionSet extension template there was a file named $className$.java and the contents of that file start with this code:
Every place you see the $xxxx$ tokens they will be replaced with the user input and of course this includes file names. The files are also processed in a very specific manner and the JavaDoc explains it very well for the generateFiles() method in the OptionTemplateSection class. Here is the doc for that field:
Generates files as part of the template execution. The default implementation uses template location as a root of the file templates. The files found in the location are processed in the following way:
- Files and folders found in the directory bin are copied into the target project without modification.
- Files found in the directory java are copied into the Java source folder by creating the folder structure that corresponds to the package name (variable packageName ). Java files are subject to conditional generation and variable replacement.
- All other files and folders are copied directly into the target folder with the conditional generation and variable replacement for files. Variable replacement also includes file names.
I would have liked to post some sample code but I do not get paid for this so oh well.
Happy template creation!
<!– Edited by Bob@balfes.net 192.168.0.6 on Wed Aug 30 20:20:21 2006–>