Previous |
Hello4 expands Hello3 by adding a new menu to the default set provided by the framework. The Color menu has lets the user select one of three colors to use for displaying the string:
The color change operation is performed by a command, ColorCommand. The current display color is stored in the component's model.
You add the new menu by subclassing GUIHandler and overriding two methods in Hello4GUIHandler:
handleCreateMenus is called by the framework during initialization of the component. You can implement handleCreateMenus to build additional menus and add them to the default menu bar for the component.
actionPerformed is called by the framework when the user selects a menu item. If you add menus to a component, you implement this method to perform the correct action for each menu item.
Note: The wizard automatically generates Hello4GUIHandler for you if you check "Custom menus" on the wizard's front page.
To make your component localizable, use the resources file to specify the strings to be used when building the menu interface. Your code uses keywords to get appropriate strings from the resource file. The strings in the resource file can later be translated to produce different national language versions.
Generating Hello4 code with the wizard
Hello4Model.java
Hello4View.java
Hello4GUIHandler.java
Hello4.java
Hello4Resources.java
ColorCommand.java
StringCommand.java
Hello4BeanInfo.java
You build and run Hello4 using wizard-generated batch files, as described in Step 1.
To generate the code for Hello4, use the wizard to create a new project named Hello4. Follow the instructions in Step 3 to set up the model, view, and StringCommand classes. You also need to do a few additional things within the wizard:
You're ready now to generate the code from the code generation screen. The wizard generates Hello4Model, Hello4View, Hello4, Hello4GUIHandler, Hello4Resources, ColorCommand, and StringCommand, plus the WizGen.java and batch files. Press the Generate All Files button to generate the files.
The source code shown here differentiates between code generated by the wizard, and code you add or modify. Wizard-generated code is shown in green.
See WebRunner\BeanTools\samples\hello4 for full source code files for each class. The source code shown here does not show the comments generated by the wizard. Do NOT remove these comments from the source code files or you won't be able to reopen the project with the wizard.
The only way Hello4Model differs from the model in the Hello2 and Hello3 components in that it has an additional data property field defining the color that will be used to display the text string. You add code to the model to initialize this and to issue notification when it changes, just as you did for the string data field added to Hello2Model.
Import statements | import java.awt.event.*; import java.util.*; import COM.ibm.desktop.*; import java.awt.Color; public class Hello4Model extends Model implements WizGen { |
Set defaults for both the string and color data | public Hello4Model() { string = "Hello World!"; color = Color.black; } |
Specify the file extension | public String getFileExtension() { return "hl4"; } |
Methods for getting the model's data | public Color getColor() { return color; }public String getString() { return string; } |
Methods for setting the model's data both issue change notification | public void setColor(Color color) { this.color = color; setModelChanged(true); notifyOfModelChange(null); }public void setString(String string) { this.string = string; setModelChanged(true); notifyOfModelChange(null); } |
Color and string data managed by the model | private Color color; private String string; } |
Hello4View differs from Hello3View in two ways:
Construct the UI and set up key event handling | public class Hello4View extends ModelView implements WizGen, KeyListener { public Hello4View() { textField = new TextField(30); add(textField); textField.addKeyListener(this); } |
Establish the connection to the model during initialization | public void initialize() { super.initialize(); model = (Hello4Model)getModel(); } |
Update local reference to model | public void setModel(IModel model) { super.setModel(model); this.model = (Hello4Model)model; } |
Reset the foreground color, using the data from the model | public void handleModelChange(ModelChangeEvent event) { super.handleModelChange(event); setForeground(model.getColor()); repaint(); }public void handleModelSelectionChange(ModelChangeEvent event) { repaint(); } |
Draw the string, using the current foreground color | public void paint(Graphics g) { g.drawString(model.getString(), 10, 50); }public Dimension getPreferredSize() { return new Dimension(400, 300); } |
Key event handling methods | protected void processMouseEvent(MouseEvent e) { getComponentController().processMouseEvent(e); }public void keyTyped(KeyEvent evt) { }public void keyPressed(KeyEvent evt) { }public void keyReleased(KeyEvent evt) { if (evt.getKeyChar() == '\n') { getComponentController().addAndDo(new StringCommand ((Hello4Model)getModel(), textField.getText())); } } |
Data fields | private TextField textField; private Hello4Model model; } |
The GUI handler, Hello4GUIHandler, is where you implement the methods that build and manage any extensions to the component's default UI provided by the framework. To build and activate the new Color menu for Hello4 components, you need to:
Import statements | import java.awt.*; import java.awt.event.*; import java.util.*; import COM.ibm.desktop.*; |
Construct a GUI handler | public class Hello4GUIHandler extends GUIHandler implements WizGen { public Hello4GUIHandler(ComponentController controller) { super(controller); } |
Handle each action appropriately, sending actions that were not generated from the Color menu to the parent | public void actionPerformed(ActionEvent evt) { if (evt.getSource() == redMenuItem) addAndDo(new ColorCommand((Hello4Model)getModel(), Color.red)); else if (evt.getSource() == greenMenuItem) addAndDo(new ColorCommand((Hello4Model)getModel(), Color.green)); else if (evt.getSource() == blueMenuItem) addAndDo(new ColorCommand((Hello4Model)getModel(), Color.blue)); else super.actionPerformed(evt); } |
Build the set of items for the new menu | protected void handleCreateMenus(MenuBar menubar) { ResourceBundle resources = getComponentController().getResourceBundle(); redMenuItem = new MenuItem(resources.getString("Red")); greenMenuItem = new MenuItem(resources.getString("Green")); blueMenuItem = new MenuItem(resources.getString("Blue")); |
Set up delegation of action events to the controller | redMenuItem.addActionListener(this); greenMenuItem.addActionListener(this); blueMenuItem.addActionListener(this); |
Build the menu and add it to the default menu | Menu colorMenu = new Menu(resources.getString("Color"), true); colorMenu.add(redMenuItem); colorMenu.add(greenMenuItem); colorMenu.add(blueMenuItem); menubar.add(colorMenu); } |
Data fields defining the new menu items | private MenuItem redMenuItem; private MenuItem greenMenuItem; private MenuItem blueMenuItem; } |
In addition to creating the model, the view, and the resources, Hello4 also creates the Hello4GUIHandler object to add custom menus to the default menu bar.
Import statements | import java.awt.event.*; import java.util.*; import COM.ibm.desktop.*; import java.awt.*; |
Create the Hello4GUIHandler object | public class Hello4 extends ComponentController implements WizGen, KeyListener { public Hello4() { try { setResourceBundle(ResourceBundle.getBundle("hello4.Hello4Resources")); } catch (MissingResourceException e) { new ExceptionDialog(null, "Can't find resource file", e); } setModel(new Hello4Model()); setGUIHandler(new Hello4GUIHandler(this)); setView(new Hello4View()); }public static void main(String args[]) { setApplicationMain(true); new Hello4(); } |
Methods that delegate key events to the component view | public void keyTyped(KeyEvent evt) { ((KeyListener)getView()).keyTyped(evt); }public void keyPressed(KeyEvent evt) { ((KeyListener)getView()).keyPressed(evt); }public void keyReleased(KeyEvent evt) { ((KeyListener)getView()).keyReleased(evt); } |
Methods for accessing the component model's data | public Color getColor() { return ((Hello4Model)getModel()).getColor(); }public String getString() { return ((Hello4Model)getModel()).getString(); }public void setColor(Color color) { ((Hello4Model)getModel()).setColor(color); }public void setString(String string) { ((Hello4Model)getModel()).setString(string); } } |
In addition to the types of resources you saw for the Hello1 through Hello3 components, Hello4Resources provides resources used to build the new menu. You create a list of resources, each one consisting of two strings. The first string is the keyword used to access the resource from the controller or other object. The second string is the localizable resource that is referenced by the keyword.
Name and file extension resources | public class Hello4Resources extends ListResourceBundle { public Object[][] getContents() { return contents; }static final Object[][] contents = { {"Application", "Hello4"}, |
Resources used to set command labels and build the Color menu | {"String Change", "String Change"}, {"Color Change", "Color Change"}, {"Color", "Color"} {"Red", "Red"}, {"Green", "Green"}, {"Blue", "Blue"}, }; }; |
ColorCommand is very similar to StringCommand, implemented in Step 3. It provides:
Like Hello4Model and Hello4, ColorCommand.java also needs to import java.awt to get access to the Color class.
Import statements | import java.awt.event.*; import java.beans.*; import COM.ibm.desktop.*; import java.awt.Color; |
Default constructor | public class ColorCommand extends Command implements WizGen { |
Build a command with the new color for the specified model, getting the command label from the resources file | public ColorCommand(Hello4Model model, Color color) { ResourceBundle resources = ResourceBundle.getBundle("hello4.Hello4Resources"); setLabel(resources.getString("Color Change")); this.model = model; this.color = color; } |
Command execution methods | protected void handleDo() { oldColor = model.getColor(); handleRedo(); }protected void handleUndo() { model.setColor(oldColor); }protected void handleRedo() { model.setColor(color); } |
Data used for undo and redo | private Hello4Model model; private Color color; private Color oldColor; } |
StringCommand is identical to the StringCommand implemented for Hello3, except that it accesses Hello4Model instead of Hello3Model.
StringCommand construction and command execution methods | public
class StringCommand extends Command implements WizGen {public StringCommand(Hello4Model model, String string) { ResourceBundle resources = ResourceBundle.getBundle("hello4.Hello4Resources"); setLabel(resources.getString("String Change")); this.model = model; this.string = string; }protected void handleDo() { oldString = model.getString(); handleRedo(); }protected void handleUndo() { model.setString(oldString); }protected void handleRedo() { model.setString(string); }private Hello4Model model; private String string; private String oldString; } |
This file is generated to allow users to change the property, method and event descriptors which are displayed in a visual builder. By default everything is displayed to the developer inside a builder or tester like Bean Tester.
Previous |
Copyright ©
Taligent, Inc. 1996 - 1997.
Copyright © IBM Corporation 1996 - 1997.
All Rights Reserved.