|
Whipping Up GUI Components (Continued)
Factory Output
DDMenu is a framework that is capable of creating the complete GUI components for menus and toolbars from a resource bundle file. The framework is centered on two classes: DDActionManager and DDBasicAction. DDActionManager plays the role of a factory; using the description file it creates the set of Actions, JMenuItems, Menus, a JMenuBar, and a JToolBar. All of the actions are created as instances of the DDBasicAction class.
DDActionManager contains a generic action events relay. During creation of DDBasicAction objects (one such object is instantiated for each action described in the description file), DDActionManager registers itself as an ActionListener, which allows DDActionManager to capture all action events. By default, DDActionManager does nothing when such an event occurs, since it is the responsibility of the application. Therefore, to handle specific actions, we need to extend DDActionManager by providing the action's core code, which we'll discuss shortly.
The file containing the description of menus and toolbars and their actions is a text file organized s a series of key-value pairs. The ResourceBundle framework of the Java API (java.util.ResourceBundle) can easily handle this file (see see Listing 1).
Lines 1 and 2 give the description of the menu bar and the toolbar, respectively. MenuBar's value is a list of menu names, separated by a comma. Those menus are then described on lines 4–9. In our example, line 1 says that the menu bar is constituted of the menus File and Edit. In turn, the File menu contains the actions FileNew, FileClose, and FileExit, as detailed on line 4 (key File.menu). The - (hyphen) between FileClose and FileExit is used to add a separator. Within the menu bar, the File menu has the name File (key File.text), and its mnemonic is F (key File.mnemonic); the Edit menu has the name Edit (key Edit.text), and its mnemonic is E (key Edit.mnemonic). We'll discuss the meaning of class keys shortly.
ToolBar's value is a list of action names, separated by a comma. As in menus, a - (hyphen) is used to add a separator between toolbar buttons. Each action is described in details beginning on line 10. In the Listing 1 example, the description of action FileNew includes its label (key FileNew.label), tooltip (key FileNew.tip), icon (key FileNew.img), keyboard shortcut (key FileNew.accel), and mnemonic (key FileNew.mnemonic). The same syntax is used to describe the FileClose, FileExit, EditCopy, and EditPaste actions.
Line 3 (see see Listing 1) provides the path to locate the images associated with the actions. A path is a fully qualified package name starting with a / and where the . is replaced by /. ImageLocator's value may contain a list of paths, each separated by a comma.
The bold words shown in the resource bundle example (see see Listing 1) are reserved keywords. All other words, that is, the names of menus, actions, and paths are set by the developer. Note that the keyboard shortcuts (.accel keys) have to be valid strings for method etKeyStroke(String) of class javax.swing.KeyStroke—refer to the Java API for more details (see Resources).
In the concluding part of this discussion, we'll look at putting the DDActionManager and DDBasicAction classes in action.
About the Author
Patrick G. Durand, Ph.D., is an engineer at Irisa-Inria in Rennes, France. Contact Patrick at .
Back to top
|