|
Put GUI Component Classes into Action
Apply a text-based resource bundle to create menu bars and toolbars. Part 2 looks at the details of using the DDActionManager and DDBasicAction classes
by Patrick G. Durand
Posted May 12, 2004
Editor's Note: This is the second of a two-part article that discusses a process for automating the creation of menu bars and toolbars in newly developed GUI interfaces. Part 1, "Whipping Up GUI Components" (Java Pro Online, May 5, 2004), looks at the resource bundle (text file) that supplies the information necessary for the Java API's ResourceBundle framework to create Swing components. Part 2 takes a look at using the framework's DDActionManager and DDBasicAction classes.
The sole constructor of the DDActionManager class takes a single argument, a URL to a resource bundle file (see Listing 1), which was introduced in Part 1 of this article ("Whipping Up GUI Components" [Java Pro Online, May 5, 2004]). During its initialization DDActionManager creates a table of actions by checking the resource file for all keys suffixed with .class to locate the actions. If no value is associated to a .class key, then DDActionManager creates an instance of DDBasicAction and sets DDBasicAction's name to the prefix of the .class key. For instance, the declaration:
FileNew.class=
results in the creation of an object from the DDBasicAction class having its name set to FileNew.
Then, to get an instance of fully operational JMenuBar and JToolBar from an initialized DDActionManager, you just need to call the createMenubar() and createToolbar() methods, respectively.
Listing 2 shows sample code to create a menu bar and toolbars given the resource bundle file from Listing 1. Figure 1 shows the application result.
Class DDBasicAction inherits from javax.swing.AbstractAction and has a default implementation of AbstractAction's actionPerformed(ActionEvent e) method:
public void actionPerformed(
ActionEvent e){
firePropertyChange(_name, null, e);
}
where _name is a DDBasicAction's class variable handling the name of the action (FileNew, for example). When the user clicks on the menu item New (or on the corresponding toolbar button), the instance FileNew of DDBasicAction fires a property change event. By default, this event is cached by the instance of DDActionManager that has created this action. Indeed, during its initialization process, DDActionManager registers itself as a property change listener of all DDBasicAction instances it creates. For that purpose, DDActionManager implements java.beans.PropertyChangeListener interface with the following default implementation of ropertyChangeListener's method propertyChange(PropertyChangeEvent evt):
public void propertyChange(
PropertyChangeEvent evt){
if (evt.getPropertyName().equals(
"globalUpdate")){
DDActionManager.this.update();
}
else{
system.out.println(
evt.getPropertyName());
}
}
Back to top
|