Download jEdit 4.1 User's Guide

Transcript
Chapter 14. Macro Tips and Techniques
if( JOptionPane.OK_OPTION !=
JOptionPane.showConfirmDialog(view, message, title,
JOptionPane.OK_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE))
return null;
// *****remainder of macro script omitted*****
// end excerpt from Write_File_Header.bsh
This macro takes several items of user input and produces a formatted file header at the
beginning of the buffer. The full macro is included in the set of macros installed by jEdit.
There are a number of input features of this excerpt worth noting.
•
The macro uses a total of seven visible components. Two of them are created
behind the scenes by showConfirmDialog(), the rest are made by the macro. To
arrange them, the script creates an array of Object objects and assigns components
to each location in the array. This translates to a fixed, top-to-bottom arrangement in
the message box created by showConfirmDialog().
•
The macro uses JTextField objects to obtain most of the input data. The fields
nameField and authorField are created with constructors that take the initial,
default text to be displayed in the field as a parameter. When the message box is
displayed, the default text will appear and can be altered or deleted by the user.
•
The text field descField uses an empty string for its initial value. The second
parameter in its constructor sets the width of the text field component, expressed as
the number of characters of “average” width. When showConfirmDialog()
prepares the layout of the message box, it sets the width wide enough to
accommodate the designated with of descField. This technique produces a
message box and input text fields that are wide enough for your data with one line
of code.
•
The displayed message box includes a JCheckBox component that determines
whether the buffer will be saved to disk immediately after the file header is written.
To conserve space in the message box, we want to display the check box to the right
of the label Name of file:. To do that, we create a JPanel object and populate it
with the label and the checkbox in a left-to-right GridLayout. The JPanel
containing the two components is then added to the beginning of message array.
•
The two visible components created by showConfirmDialog() appear at positions
3 and 6 of the message array. Only the text is required; they are rendered as text
labels.
•
There are three invisible components created by showConfirmDialog(). Each of
them involves a call to Box.createVerticalStrut(). The Box class is a
sophisticated layout class that gives the user great flexibility in sizing and
133