Using the default implementation provided by
AbstractOptionPane
reduces the preparation of an
option pane to two principal tasks: writing a
_init()
method to layout and initialize the pane,
and writing a _save()
method to commit any settings
changed by user input. If a button on the option pane should trigger
another dialog, such as a JFileChooser
or jEdit's
own enhanced VFSFileChooserDialog
, the option
pane will also have to implement the
ActionListener
interface to display additional
components.
The QuickNotepad plugin has only three options to set: the path
name of the file that will store the notepad text, the visibility of the
path name on the tool bar, and the notepad's display font. Using the
shortcut methods of the plugin API, the implementation of
_init()
looks like this:
public class QuickNotepadOptionPane extends AbstractOptionPane implements ActionListener { private JTextField pathName; private JButton pickPath; private FontSelector font; ... public void _init() { showPath = new JCheckBox(jEdit.getProperty( QuickNotepadPlugin.OPTION_PREFIX + "show-filepath.title"), jEdit.getProperty( QuickNotepadPlugin.OPTION_PREFIX + "show-filepath") .equals("true")); addComponent(showPath); pathName = new JTextField(jEdit.getProperty( QuickNotepadPlugin.OPTION_PREFIX + "filepath")); JButton pickPath = new JButton(jEdit.getProperty( QuickNotepadPlugin.OPTION_PREFIX + "choose-file")); pickPath.addActionListener(this); JPanel pathPanel = new JPanel(new BorderLayout(0, 0)); pathPanel.add(pathName, BorderLayout.CENTER); pathPanel.add(pickPath, BorderLayout.EAST); addComponent(jEdit.getProperty( QuickNotepadPlugin.OPTION_PREFIX + "file"), pathPanel); font = new FontSelector(makeFont()); addComponent(jEdit.getProperty( QuickNotepadPlugin.OPTION_PREFIX + "choose-font"), font); } ... }
Here we adopt the vertical arrangement offered by use of the
addComponent()
method with one embellishment. We
want the first “row” of the option pane to contain a text
field with the current notepad file path and a button that will trigger
a file chooser dialog when pressed. To place both of them on the same
line (along with an identifying label for the file option), we create a
JPanel
to contain both components and pass the
configured panel to addComponent()
.
The _init()
method uses properties from the
plugin's property file to provide the names of label for the components
placed in the option pane. It also uses a property whose name begins
with PROPERTY_PREFIX
as a persistent data item -
the path of the current notepad file. The elements of the notepad's font
are also extracted from properties using a static method of the option
pane class.
The _save()
method extracts data from the
user input components and assigns them to the plugin's properties. The
implementation is straightforward:
public void _save() { jEdit.setProperty(QuickNotepadPlugin.OPTION_PREFIX + "filepath", pathName.getText()); Font _font = font.getFont(); jEdit.setProperty(QuickNotepadPlugin.OPTION_PREFIX + "font", _font.getFamily()); jEdit.setProperty(QuickNotepadPlugin.OPTION_PREFIX + "fontsize", String.valueOf(_font.getSize())); jEdit.setProperty(QuickNotepadPlugin.OPTION_PREFIX + "fontstyle", String.valueOf(_font.getStyle())); jEdit.setProperty(QuickNotepadPlugin.OPTION_PREFIX + "show-filepath", String.valueOf(showPath.isSelected())); }
The class has only two other methods, one to display a file
chooser dialog in response to user action, and the other to construct a
Font
object from the plugin's font properties.
They do not require discussion here.