Geertjan's Blog

  • July 1, 2005

Centralized Variables in NetBeans IDE Strings Tables

Geertjan Wielenga
Product Manager
In previous blog entries, and in the new NetBeans File Template Plug-in Tutorial, I describe how to create a branded file template. The file template is branded with the Sun Microsystems copyright text. However, I've just discovered a different way of branding a file. It's not the most efficient way of doing so, but it's fun because it's different. Maybe it will give you some ideas of doing other similar things.

There are really only two steps to this process (which is another cool thing about this approach):

  1. Define copyright variables in a Strings Table. Go to Tools > Options. In the Options dialog box, expand Editing, select Java Sources, and click Strings Table. The Java Sources - Strings Table dialog box pops up. By default, only the USER variable is defined. So now, just add a string for each line of the copyright text, and include some spacing:

    COPYRIGHT3=Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved. U.S.
    COPYRIGHT4=Government Rights - Commercial software. Government users are subject
    COPYRIGHT5=to the Sun Microsystems, Inc. standard license agreement and
    COPYRIGHT6=applicable provisions of the FAR and its supplements. Use is subject
    COPYRIGHT7=to license terms.
    COPYRIGHT9=This distribution may include materials developed by third parties.
    COPYRIGHT10=Sun, Sun Microsystems, the Sun logo, Java and J2EE are trademarks
    COPYRIGHT11=or registered trademarks of Sun Microsystems, Inc. in the U.S. and
    COPYRIGHT12=other countries.
    COPYRIGHT14=Copyright (c) 2005 Sun Microsystems, Inc. Tous droits reserves.

    My Strings Table now looks like this:

  2. Specify the copyright variables in a file template. Again in the Options dialog box, expand Source Creation and Management, expand Templates, and then find a Java template to which you want to add the copyright text. For example, expand Java Classes, select Java Class, right-click it and choose Open. The template opens in the Source Editor. Now add all the variables you defined to the template:

    \* __NAME__.java
    \* Created on __DATE__, __TIME__
    \* __COPYRIGHT1__
    \* __COPYRIGHT2__
    \* __COPYRIGHT3__
    \* __COPYRIGHT4__
    \* __COPYRIGHT5__
    \* __COPYRIGHT6__
    \* __COPYRIGHT7__
    \* __COPYRIGHT8__
    \* __COPYRIGHT9__
    \* __COPYRIGHT10__
    \* __COPYRIGHT11__
    \* __COPYRIGHT12__
    \* __COPYRIGHT13__
    \* __COPYRIGHT14__
    \* __COPYRIGHT15__
    \* __COPYRIGHT16__
    package Templates.Classes;
    \* @author __USER__
    public class Class {
    /\*\* Creates a new instance of __NAME__ \*/
    public Class() {

When you save the file, you've changed the template. So when you create a new Java class in the New File wizard, all the variables above will be replaced by the values in the Strings Table. A question you might be asking right now is: "Why don't you just put the copyright text straight into the template? What's the point of defining all those variables in the Strings Table in the first place?" Good question. The answer: What happens if the copyright text changes? If I'd defined the copyright text in the template, I'd have to change it in the template -- as well as in every other template where I defined the copyright text. By defining the copyright text in the Strings Table, I've got one centralized place where I can edit the copyright text. (At the very least, COPYRIGHT3 and COPYRIGHT14 will change once a year!) Of course, what this approach doesn't support is the ability to share the file template with others -- that's where the NetBeans plug-ins come in handy because the end-product of plug-in development is a binary file that you can easily transfer to a colleague, via e-mail, disk, or shared server.

Join the discussion

Comments ( 2 )
  • Miguel A Paraz Sunday, July 3, 2005
    Good work! It looks like you are developing the NetBeans manual in these pages.
  • Geertjan Wednesday, July 6, 2005
    Thanks Miguel. Are there any specific things you're interested in that you'd like me to blog about?
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.