Previous | Next |
The Hello2 program expands on the Hello1 program by adding:
You use the wizard to create the basic code for the component, including the model and view. You'll need to add code to the model to handle the string data correctly, and add code to the view to handle the input and display of new strings.
This component also shows a simple example of the framework's notification mechanism. The notification mechanism provides a way for the model to notify the view whenever any data is changed, so that the view can redraw the changes. In this component:
The model calls the method notifyOfModelChange whenever its encapsulated string data is changed.
This issues change notification to any registered listeners, including the view, which is registered automatically by the framework when the component is created.
The notification mechanism calls the view method handleModelChange, which handles the change as appropriatein this case, by redrawing itself.
Generating Hello2 code with the wizard
Hello2Model.java
Hello2View.java
Hello2.java
Hello2Resources.java
Hello2BeanInfo.java
You build and run Hello2 using wizard-generated batch files, as described in Step 1.
To generate the code for Hello2, use the wizard to create a new project named Hello2. This time you want to create a model as well as a view and controller. You'll also need to define the model's data variables, or properties, and set up the view to handle key events so that the user can type in strings. After filling out the project definition screen, press "Next" to go to the tabbed panels.
To add the string data property to the model:
Note: The wizard also adds getString and setString methods to the controller, Hello2. If you later remove this property from the model, you also need to remove these methods from the controller.
To set up the view to accept key events:
You're ready now to generate the code from the code generation screen. The wizard generates Hello2Model, Hello2View, Hello2, and Hello2Resources, plus the WizGen.java and batch files. Press the Generate All Files button to generate and save 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\hello2 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 actual source code files or you won't be able to reopen the project with the wizard.
Hello2Model manages the data associated with the component, in this case, the string displayed by Hello2View. The model is responsible for:
The wizard-generated code sets up the data accessors (get and set methods) for you. You need to add code that initializes the data where necessary. For example, in Hello2Model, the constructor assigns a default value to the model's text data: the string "Hello World". You could also do this by assigning a default value directly to the data field.
You also need to ensure that the data access methods issue notification where appropriate. In Hello2Model, listeners need to know when the string changes--the view, for example, needs to redraw the change. The setString method, therefore, issues change notification by:
The framework serializes or deserializes the whole model when the user saves or opens a component. If you don't want to save a data field in the model, mark it transient. If you need to override the default serialization mechanism, override the readObject and writeObject methods. For more information on serialization, visit the JavaSoft site.
Hello2Model doesn't implement these methods because it uses the default serialization mechanism to stream its String object.
Extend the default model class | public class Hello2Model extends Model implements WizGen { |
Set a default string in the constructor | public Hello2Model() { string = "Hello World"; } |
Specify the file extension | public String getFileExtension() { return "hl2"; } |
Access to the string datasetString issues notification that the string has changed | public String getString() { return string; }public void setString(String string) { this.string = string; setModelChanged(true); notifyOfModelChange(null); }private String string; } |
Hello2View provides quite a bit more functionality than the view for Hello1. Hello2View draws an interface and implements event handling so that users can interact with and modify the component data, and is tightly coupled with the component model. Hello2View contains the code that modifies the model's data, and responds to the model's change notification by redrawing the data.
Hello2View draws the interface and sets up event handling in its constructor. It adds a java.awt object, TextField, to create a simple dialog that users can type strings into. The view also sets itself up as a listener to receive key events that occur within the text field. The component controller calls the view's key event methods when a key is pressed, typed, and released. The only event the view really cares about is the KEY_RELEASED event. The view's keyReleased method queries the event to see if the key released was the enter (or return) key and, if so, resets the model to contain the string the user just entered.
The view also has methods called by the framework when the model changes--handleModelChange and handleModelSelectionChange. For this simple component, these methods just cause the view to redraw itself.
Extend the default view class and implement the interface for key event handling | public class Hello2View extends ModelView implements WizGen, KeyListener { |
Add a text field to the view for the user to enter text, and to direct key events from the text field to the view | public Hello2View() { textField = new TextField(30); add(textField); textField.addKeyListener(this); } |
Methods called when the model is changed (this component has no model selection but the default method is generated) | public void handleModelChange(ModelChangeEvent event) { super.handleModelChange(event); repaint(); }public void handleModelSelectionChange(ModelChangeEvent event) { repaint(); } |
Get the current string from the component's model and draw it at a specified location | public void paint(Graphics g) { Hello2Model model = (Hello2Model)getModel(); g.drawString(model.getString(), 10, 50); }public Dimension getPreferredSize() { return new Dimension(400, 300); } |
Standard key event handling methods | public void keyTyped(KeyEvent evt) { }public void keyPressed(KeyEvent evt) { } |
When the user hits the enter (return) key, reset the model string to the string currently in the view's text field | public void keyReleased(KeyEvent evt) { if (evt.getKeyChar() == '\n') { Hello2Model model = (Hello2Model)getModel(); model.setString(textField.getText()); } } |
Data field used in the UI | private TextField textField; } |
The controller for Hello2 components is almost identical to the Hello1 controller. It sets up a component by instantiating Hello2Model and Hello2View, and reading in the resources specified by Hello2Resources.
Hello2
also includes methods it uses in its role as component
controller. It implements KeyListener event handling methods that
direct key events to the view, and has methods that provide
access to the model's data. Component controllers needs these
methods because JavaBeans do not support upward delegation of
events, properties, and methods for internal beans. If you later
remove properties or events from other classes in the component,
you should manually remove the corresponding delegation methods
from the component controller.
Extend the default controller and implement the KeyListener interface | public class Hello2 extends ComponentController implements WizGen, KeyListener { |
Create and set the custom resources, model, and view. | public Hello2() { try { setResourceBundle(ResourceBundle.getBundle("hello2.Hello2Resources")); } catch (MissingResourceException e) { new ExceptionDialog(null, "Can't find resource file", e); } setModel(new Hello2Model()); setView(new Hello2View()); }public static void main(String args[]) { setApplicationMain(true); new Hello2(); } |
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 String getString() { return ((Hello2Model)getModel()).getString(); }public void setString(String string) { ((Hello2Model)getModel()).setString(string); } } |
Hello2Resources.java provides the string resources unique to the Hello2 component: the component name ("Hello2").
Specifies the component name for Hello2 components | public class Hello2Resources extends ListResourceBundle { public Object[][] getContents() { return contents; }static final Object[][] contents = { {"Application", "Hello2"}, }; }; |
This file is generated to allow users to change the property, method, and event descriptors that are displayed in a visual builder. By default, everything is displayed to the developer inside a builder or a tester like Bean Tester.
Previous | Next |
Copyright ©
Taligent, Inc. 1996 - 1997.
Copyright © IBM Corporation 1996 - 1997.
All Rights Reserved.