Java Reference
In-Depth Information
IDEA
attempts to minimize this boilerplate work with the concept of
file tem-
plates.
File templates define a starting point for new files, Java or otherwise.
When you create a new file from within the
Project
window,
IDEA
looks up and
presents you with a set of templates that can be created in that context. For
example, if you try to create a new file in a package in a Java source tree,
IDEA
asks if you want to create a new Java class, Java interface, or Singleton, among
others. Choosing one of these options invokes its associated file template, which
includes a basic framework for the type of file you're creating. Using this strat-
egy, you can start with your file's basic framework already defined, rather than
with a blank document.
12.6.1
Creating a new file from a template
In the
Project
window, right-click the folder where you want to create the new file.
From the context menu, select an entry from the
New
submenu to create your
new file. You have several choices as to the type of file you're creating; but
IDEA
will suggest creating Java files only under the existing configured sourcepaths:
Class
creates a standard Java class.
■
Interface
creates a standard Java interface.
■
File
creates a non-Java file, such as a text file or
JSP
.
■
Package
or
Directory
creates a new package if you're under a source tree
or otherwise a plain old directory.
■
Singleton
creates a class that implements the Singleton pattern, ensuring
that a single instance is constructed.
■
Enumeration Class
creates a class that models enumerated types.
■
You're asked to name the file, and, in the case of non-Java files, you must provide
the file extension. For example, if you create a new class or interface, it automati-
cally gets a .java extension.
Editing the default file templates
To edit a default template for a file type, select the
File Templates
option of the
IDE
Settings
to bring up the
File Templates
dialog. This dialog gives you access to
all the templates in the system. The
Code
tab is used with
IDEA
's code-generation
features; the
J2EE
tab is used for developing web applications. For now, we'll con-
centrate on the
Te m p l a t e s
and
Includes
tabs, which are used to create new files.
An example of the file template editor is shown in figure 12.10.