Java Reference
In-Depth Information
Figure 3.24
Once IDEA knows what object is being delegated
to, it prompts you for the set of methods (that is,
the subset of the underlying object's API) to
delegate. These methods and their delegation
mechanism are automatically implemented in
your current class.
In our example, a
CurrencyCalendar
class has a field of type
Calendar
called
cal-
endar
. This is the underlying calendar implementation, and we want to give users
of the
CurrencyCalendar
class access to some of the
Calendar
's functionality with-
out exposing the
calendar
field directly. To do this, we create a new method that
wraps the
calendar
's
get()
method, delegating the request to
calendar
. The
resulting new method looks like this:
public int getName(int field) {
return calendar.get(field);
}
3.7.7
Enclosing a block of code
You can use the
Code | Surround With
(
Ctrl+Alt+T
) com-
mand to wrap a selected block of code with one of the Java
constructs such as an
if
,
while
, or
try/catch
block. Select the
block of code you want to surround, and then activate the
command. A menu like that in figure 3.25 will appear. Use
your mouse or the shortcut key (listed next to each entry in
the menu) to wrap your code block.
3.7.8
Customizing code generated by IDEA
You can use the Code Templates tab in the
File Templates
editor in
IDE
Settings
to customize the code generated for
you by the editor's code-generation features. A number of
preexisting entries are defined. You can only edit the exist-
ing entries; you can't remove them or create new ones.
Each template type defines its own variables, which may be
Figure 3.25
Many of the basic
control and flow
structures in Java
can be generated
by selecting a bunch
of code and invoking
the Surround With
operation.