Manages a JMenuBar, providing convenient tools for building menus.
This is a general-purpose menu manager which
automatically handles radio button grouping and mnemonic assignments
within each menu, and mnemonic assignments for the set of menus themselves.
All menu items are assigned the same ActionListener.
The JMenuBarMgr class makes it straightforward to create an entire menu bar.
Create a list of JMenuItemSpec objects for each menu, each of which can be
quite short since there are default settings for several properties.
Next create a list of JMenuSpec objects from
the JMenuItemSpec objects,
and pass that in to the createMenu method.
Here's a sample fragment:
final JMenuItemSpec[] fileItems = {
new JMenuItemSpec(new JCheckBoxMenuItem("Work Offline"),
CMD_OFFLINE, KeyEvent.VK_W),
new JMenuItemSpec(), // separator
new JMenuItemSpec(new JMenuItem("Save History"),CMD_SAVE_HISTORY),
new JMenuItemSpec(), // separator
new JMenuItemSpec(new JMenuItem("Exit"),
KeyEvent.VK_X,CMD_EXIT, KeyEvent.VK_F4, ActionEvent.ALT_MASK)
}
. . .
final JMenuSpec[] menuList = {
new JMenuSpec("File", fileItems),
. . .
}
Note that the simplest item is an empty JMenuItemSpec, which is a separator.
The next simplest contains just a JMenuItem and a command.
Any such menu item will still have a mnemonic key assigned (though not an
accelerator), so that could suffice for many tasks.
Creates the menus for the JMenuBar, attaching them to the menu bar.
Each JMenuSpec specifies a list of menu items, an optional mnemonic,
and an optional MenuListener.
A JMenu is created for each by the buildMenu method
(which automatically handles radio button grouping and mnemonic assignments
at the item level), a mnemonic is assigned to the JMenu,
and the optional MenuListener is attached, if provided.
Mnemonics are assigned automatically.
Mnemonics are assigned at the menu level.
Those that are provided in the JMenuSpec list are allocated first.
The remaining menus are
scanned assigning the first unique character available from each name.
As an example, consider a set of menus named
File, Edit, Tools, Run, and Results.
One may invoke createMenus with none, some, or all, of them having
explicit mnemonic assignments.
None - The mnemonics are automatically assigned, using first
available choices: File--F, Edit--E, Tools--T, Run--R, Results--S.
The last one shows that "R" was already used, "E" was already used, so
"S" is the first available.
Some - Assume Edit is defined with a "D". The remainder will then be
auto-assigned as: File--F, Tools--T, Run--R, and Results--E.
Builds a single menu. This routine is used internally by createMenus
but is also public so that one may create a JMenu to be used as a sub-menu.
Since JMenu is a subclass of JMenuItem,
the resulting JMenu may be inserted into a JMenuItemSpec
which is passed to createMenus.
For each menu item:
A menu separator is added if the item is null.
The optional accelerator key is assigned if provided.
The global ActionListener is assigned to each item.
The item is enabled or disabled as specified in the JMenuItemSpec.
Radio button are grouped automatically.
As menu items are processed sequentially, adjacent radio buttons are grouped
until either a separator or a non-radio button is encountered.
Mnemonics are assigned automatically.
Mnemonics may or may not be provided in each JMenuItem. Those that
are provided are allocated first. The remaining menu items are
scanned assigning the first unique character available from each name.
Parameters:
menuSpec - a specification for a single menu
Returns:
a JMenu object containing the items in the menuSpec