JDT/UI and Text Team Blog

The JDT/UI and Text team presents new API and features

Text Templates 1

One of our 3.4 plan items is improve template mechanism and offer a set of SWT templates. We’ve added this in 3.4 M2.
In this blog post I’ll describe how to use these new SWT templates and how to add your own templates, because templates can save you from typing the same chunk of code over and over again.

SWT Templates

Let’s say you want to add a SWT Button to a control. Instead of writing all the required code by hand you can, if you use Eclipse 3.4M2 or later, type ‘B‘ and press Ctrl+Space three times to get direct access to the SWT templates, then select the Button template and press Enter:

SWT templates in action

Of course the same SWT template proposals are already available after the first invocation of Ctrl+Space but are near the end in the proposals list due to lower priority. Also note that SWT template proposals are only available if SWT is on the Java build path of your project.

Add Your Own Templates

You can add your own templates on the Java > Editor > Templates preference page. Platform/Text uses its own language to define templates. Lets say we want to add a new template to create a GridData object. To do so go to said preference page and click New… then set the values of the fields as follows:

simple grid data pattern

From now on your SWT proposals will contain your grid data template.

Make Your Templates Smart

Inserting the above template may result in compile errors, if for example a variable data is already defined. To make your templates smarter you can use so called template variables. Template variables start with a $ sign and resolve to some value when the template is inserted. You can also use this to define a new named variable in case the name doesn’t match with a pre-defined variable. We now use the newName variable to create a new name for the grid data variable. The newName variable will resolve to a non conflicting variable name:

GridData ${newName}= new GridData(SWT.FILL, SWT.FILL, true, true);
control.setLayoutData(data);

When inserting this pattern the variable ${newName} is replaced by a non-conflicting Java variable name. But we also want to use this name to be passed to setLayoutData. To do this we can give each template variable an id and use this id to reference the value which the variable resolves to:

GridData ${dataName:newName}= new GridData(SWT.FILL, SWT.FILL, true, true);
control.setLayoutData(${dataName});

Now ${dataName} is replaced by the value generated by our newName variable. If you try this you will realize, that newName resolves to object, which is not a very good variable name for a GridData. Instead we want the variable to resolve to something like gridData or data or something similar. To achieve this we can parameterize the newName template variable: it can take a qualified type name as parameter. The type name is used by the template variable resolver to come up with a useful variable name:

GridData ${dataName:newName(org.eclipse.swt.layout.GridData)}=  new GridData(...);
control.setLayoutData(${dataName});

Now newName will resolve to gridData and a proposal window will propose data:

Our grid data template when inserted into the editor

We’re not yet completely done with our grid data template, there is still a lot which can be improved. If you do so you may end up with the following monster:

${type:newType(org.eclipse.swt.layout.GridData)} ${dataName:newName(org.eclipse.swt.layout.GridData)} = new ${type}(SWT.${horizontal:link(FILL, BEGINNING, CENTER, END)}, SWT.${vertical:link(FILL, TOP, CENTER, BOTTOM)}, ${hex:link(true, false)}, ${vex:link(true, false)});
${control:var(org.eclipse.swt.widgets.Control)}.setLayoutData(${dataName});
${imp:import(org.eclipse.swt.SWT)}${cursor}

You can find an up-to-date list of template variables by pressing F1 on the Templates preference page or in the Edit Template dialog and then selecting Templates Concepts .

As you can see, text templates are a powerful but also quite complex mechanism. But as said: write the template once, and save time for the rest of your coding life.

What’s Next

In the next blog post I’m going to show how to contribute your own set of templates through an extension point and how to add your own template variable resolvers.

Posted November 20th, 2007 by Benno Baumgartner in category: Uncategorized
Responses are currently closed, but you can trackback from your own site.

3 Responses to “Text Templates 1”


  1. Jacek Pospychala Says:

    Wow, that’s a neat trick. My first impression after reading is, that maybe adding your own template could be even more simple if the appropriate option showed up in context menu for a selected range of text.


  2. Benno Baumgartner Says:

    Mmm, I like the idea with the context menu. A problem is that we try to minimize the amount of entries in the context menu, there are already too many. The templates view (bug 69581) will allow to create a new template by drag and drop a text selection into the view.


  3. JDT/UI and Text Team Blog » Blog Archive » Text Templates 2 Says:

    [...] « Text Templates 1 [...]

Recent Posts

Archives

Categories

Meta