Using Eclipse Templates to show off

Have you ever been in a software demo there the presenter was that fast with the editor that you were scared?

He might have been using personal templates. Templates allow you to save a block of coding and insert it as you desire. You find them under Window-> Preferences -> Java -> Editor -> Templates.

Try it out in the Editor for the SWT templates (within an Eclipse plugin or Eclipse RCP application). Type Label and press Cntr+Space three times until you see only the Java template suggestions.

Select the template and change the code to your liking. Do the same for GridData. You end up the following code almost without typing.

Label label1 = new Label(parent, SWT.NONE);
label1.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false,false));
label1.setText("Test");
GridData gridData2 = new GridData(SWT.FILL, SWT.FILL, true, false);
gridData2.widthHint = SWT.DEFAULT;
gridData2.heightHint = SWT.DEFAULT;
label1.setLayoutData(gridData2);

The great thing about templates is that you can define your own once. So if you have something longer which you need to type again and again you can use your own templates.

About Lars Vogel

Lars Vogel is the founder and CEO of the vogella GmbH and works as Eclipse and Android consultant, trainer and book author. He is a regular speaker at international conferences, He is the primary author of vogella.com. With more than one million visitors per month this website is one of the central sources for Java, Eclipse and Android programming information.
This entry was posted in Eclipse and tagged , , . Bookmark the permalink.

7 Responses to Using Eclipse Templates to show off

  1. Hi Lars,

    for some reasons I don’t see the SWT templates (from within PDE). What’s the trick to enable them?

    Michael

  2. Lars Vogel says:

    @Michael Do you see the templates under Window-> Preferences -> Java -> Editor -> Templates?

  3. Bob Brodt says:

    Yeah…templates are a good idea if used sparingly, otherwise they just lead to duplication and unreadable code and they make programmers fat and lazy 😉

  4. Lars Vogel says:

    @Michael also check in the preferences Java -> Editor -> Content Assist -> Advanced. SWT Template Proposals shoudl be active.

  5. dao says:

    templates are great. But how do you save them into your eclipse product so that they will be deployed as default templates?

  6. Lars Vogel says:

    @dao Not sure what you mean. The templates can be exported and imported into different workspaces. The default ones come with your Eclipse installation.

  7. I figured out my problem: the trick is to only type “Label” and not “new Label”.

    I often type “new MyClass” and then I use the “extract local variable” refactoring to create a local variable and the declaration…..

    If typing “MyClass” would propose creating “MyClass myClass=new MyClass();” then the “Label” would just be an expansion of this. But this is a slightly different usage pattern of templates.

    It’s great that I now learned that trick 🙂

Comments are closed.