Code Templates are used by the OneSourceŽ round-trip engineering engine to generate the initial source code and default property values for new modeling elements you create in Together. A simple example is the default template for a class. When you create a new Java class, the default name is "Class1", and the default code generated for it is:
public class Class1 {
}
By modifying the appropriate template, you can change the name to "New1" (or whatever you want). more importantly, you can also modify the default code, adding default attributes or operations. For example, you could change the default code for a Java class so that a default addNew() operation is always generated when you create new classes in diagrams:
public class Class1 {
public void addNew() {
}
}
This may seem trivial at first glance, but consider the implications for things like Enterprise JavaBeans (EJBs). When you use the one-click EJB feature, you get default source code for an entity or session EJB (see figure below). By modifying the respective code Templates, you can customize the default code for EJB classes, home and remote interfaces, etc. adding fields, properties, business methods... whatever you want... to the default source generated for new instances of the particular element in diagrams.
The template node for each modeling element contains two subnodes (see figure below). The first is for the default source code, the second is for default properties.
Each template contains a set of default properties whose values you can modify. For example, the default name is specified in the properties. Thus, to change the default code for an element, you open and edit the source code node; to change default values of properties you edit the properties node. (See Editing Code Templates below.)
You can browse through the available Code Templates using the
Directory tab
of the Explorer. (It doesn't matter whether a project is open or
not.) To see the main template
categories, expand the Templates
node. The categories correspond to different programming languages
and IDL variants.
Note that while all templates are available to view and edit, not all templates function in all products. For example, Java templates don't work in products that support only C++. Functionality depends upon your product license.
Each main category has subcategories for class templates, link templates, and member templates. Expand any node in the treeview to see the available templates. The figure below shows the structure of the Templates node in the Directory tab, and the default code for the selected element. This is the code generated for this element... in this case and entity EJB... whenever a new one is created in the visual model. You can edit this code (see next section) to change the default source generated for new instances of the modeling element. For example, for the EJB shown below you could add default declarations for finder or business methods.
|
|
|
Code templates in the Explorer, and the default code for an entity EJB |
Note that currently, only the templates shown are supported. Future releases will enable you to create your own custom templates and generate instances of them in projects.
You can edit the default source code specified by a template, the default values of properties, or both using the Together Editor. You can open multiple source templates or properties in the editor at once, thus facilitating clipboard operations between templates. Changes to source code or properties are automatically saved when you leave the Editor. You can manually save changes using the File menu or Main toolbar.
You will notice in the default code delivered with Together that several "blueprint macros" are used. For example, in the code for the default class, the %Name% macro is used:
public class %Name% }
{
This macro expands to the default class name specified in the properties (Class1 by default). You can use any of the available macros in the code. Blueprint macros are documented in Reference: Blueprint Macros.
To open one template for editing:
Navigate to the template's node in
the Directory tab of the Explorer and select it.
Right-click on the node and choose Edit on the speedmenu.
To open multiple templates for editing:
Navigate to a template node in the
Directory tab of the Explorer and select it.
Right-click on
the selected node and choose Edit
in New Tab on the speedmenu.
Repeat 1 and 2 for additional templates as desired.
You can configure quite a number of source code formatting options in the Source Code page of the Options dialog... indenting, comment format, treatment of spaces, etc. Once you have edited a template to its final state, you can apply the currently configured source formatting options to the code.
To apply source formatting options:
Select the source code node (not
properties node) of the template in the Explorer.
Right-click and choose Format Source from the Speedmenu.
You can configure Together to invoke any source code editor. If you prefer to edit templates using another editor you can easily do so.
To edit template in an external editor:
Check to see that you have
configured the external editor (Options dialog, Tools page).
Right-click on the template node and choose Tools | External Editor on the speedmenu.
See also:
Reference:
Code-gen Blueprint Macros
Using
the Editor
Guide
to the Options pages: Tools page
Guide
to the Options pages: Source Code page
User's
Guide:Adding custom code templates