1 / 51

Features of JavaBeans

Features of JavaBeans. Support for introspection so that a builder tool can analyze how a bean works Support for customization so that when using an application builder a user can customize the appearance and behavior of a bean Support for events

marcie
Download Presentation

Features of JavaBeans

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Features of JavaBeans • Support for introspection • so that a builder tool can analyze how a bean works • Support for customization • so that when using an application builder a user can customize the appearance and behavior of a bean • Support for events • as a simple communication metaphor than can be used to connect up beans • Support for properties • both for customization and for programmatic use • Support for persistence • so that a bean can be customized in an application builder and then have its customized state saved away and reloaded later

  2. JavaBean Characteristics • a public class with 0-argument constuctor • it has getters and setters • it has events • it can customized • its state can be saved • it can be analyzed by a builder tool

  3. Persistent Storage • Purpose: • To use existing data formats and plug into OLE or OpenDoc documents (e.g., Excel doc inside a Word doc) • To be “trivial” for the common case of a tiny Bean (by saving its internal state) • Solutions • Externalization: provides a Bean with full control over the resulting data layout. • Serialization: provides an automatic way of storing out and restoring the internal state of a collection of Java objects • All bean must support either Serialization or Externalization

  4. Bean Naming Conventions • String ss; // getSs setSs • Properties • Public X getPropertyName() • Public void SetPropertyName(X x) • Note: above interacts w/ propertyName • Boolean Properties • Public boolean isPropertyName() • Public void setPropertyName(boolean b)

  5. More Bean Conventions • Indexed Properties • X[] getPropertyName() • Void setPropertyName(X[] x) • X getPropertyName(int i) • Void setPropertyName(int I,X x) • Bound Properties • Constrained Properties • Custom Events • Property Editors • The BeanInfo Class

  6. Naming rules Constructors A bean has a no argument constructor Simple Properties public T getN()public void setN ( T value) Boolean Properties public boolean isN()public boolean getN()public void setN(boolean value) Indexed Properties public T getN(int index)public T[] getN()public void setN(int index, T value)public void setN(T[] values)

  7. Coding examples for Properties public class alden2 extends Canvas { String name = “Hello”; public alden2() { setBackground (Color.red); setForeground (Color.blue); } public void setName (String newName) { name = newName; } public String getName() { return name; } public Dimension getMinimunSize() { return new Dimension (50, 50); } }

  8. Bound Properties • Generates notification when a property is changed. public class propertDemo extends Canvas { String ourString = “Hello”; private PropertyChangeSupport changes = new PropertyChangeSupport(this); ... public void setString (String newString) { String oldString = ourString; ourString = newString; changes.firePropertyChange(“string”, oldString, newString); } ...

  9. Bound Properties... ... public void addPropertyChangeListener (PropertyChangeListener l) { pcs.addPropertyChangeListener (l); } public void removePropertyChangeListener (PropertyChangeListener l) { pcs.removePropertyChangeListener(l); } }

  10. Events • Two types of objects are involved: • “Source” objects. • “Listener” objects. • Based on registration. • Makes use of parametric polymorphism.

  11. Events • Message sent from one object to another. • Sender fires event, recipient (listener) handles the event • There may be many listeners. Event source Register listener Event listener Fire event Event Object

  12. Event source Register event listener Fire event Event Adapter Event Object Forward event Event Object Provide interface Event Listener

  13. Bean Events • Define a new Event class which extends EventObject. XEvent • Define a new interface for listeners to implement, this must be an extension of EventListener. XEventListener • The Source must provide methods to allow listeners to register and unregister eg addXListener(), removeXListener(). • The source must provide code to generate the event and send it to all registered listeners. fireXEvent() • The listener must implement the interface to receive the event.changeX() • The listener must register with the source to receive the event.

  14. Events public class eventSource extends GenericEventGenerator { ... private Vector myListeners = new Vector(); ... public synchronized void addMyEventListeners ( MyEventListener l) { myListeners.addElement(l); } public synchronized void removeMyEventListeners ( MyEventListener l) { myListeners.removeElement(l); } ...

  15. Events... private void fanoutEvents() { Vector l; synchronized (this) { l = (Vector) myListener.clone(); } for (int i = 0; i < l.size(); i++) { MyEventListener mel = (MyEventListener) l.elementAt(i); mel.handleThisEvent (this); } }

  16. Property Editors

  17. What is a property Editor? • Interface that resides in • package java.beans; • It is a specification that enables editing of properties. • A means to create a Property Sheet

  18. Property Sheet • A Panel where editable bean properties appear along with their edit fields

  19. Property Editor • Needs to be associated with a datatype

  20. Customization...<classname>Editor package sun.beanbox.beans; import java.beans.*; public class NervousText07TextPropertyEditor extends PropertyEditorSupport { public String[] getTags() { String values[] = { "Nervous Text", "Anxious Text", "Funny Text", "Wobbly Text"}; return values; } }

  21. BDK Output

  22. Building Property Editors

  23. Property presentation • setAsText(), getAsText(), getTags(), isPaintable, paintValue() getCustomEditor()

  24. PropertyEditor values • Property value access • setValue(), getValue(), getJavaInitializationString() • Property change notification • addPropertyChangeListener(), removePropertyChangeListener(), firePropertyChange()

  25. Property presentation methods • The propetyEditor interface supports different ways of presenting and editing a property. • The code in getAsText(),getTags(), and isPaintable() control how properties are presented on the property sheet. • There are three presentation modes • As text • As an Enumerated type • As a custom component

  26. Used when Simple properties are usually presented as text Settings: IsPaintable() returns FALSE getAsText() returns a non-null vlue Use the setAsText(stringtxt) method to set the value property to a string. Method throws the IllegalArgrumentException if argument is not formatted properly or property can not be expressed as a string. Presenting Properties as text

  27. Used when A property that takes discrete values can be expressed as an enumerated type Settings: IsPaintable() returns FALSE getAsText() returns a non-null value getTags() returns a non-null value Presenting Properties as an enumerated type

  28. Used When A property that can not be expressed as text can be displayed using GUI components Settings: IsPaintable() returns TRUE indicating to the builder tool that the paintValue() method is supported getAsText() returns NULL getags() returns NULL The paintValue() method should contain code to paint a rectangular area next to the property in the property list. The getCustomEditor() method should return a component used for editing. Presenting Properties as a custom component

  29. getValue() The body of this method contains the code to set the property value This method is called by the builder tool when the property sheet is initialized to diplay the default values setValue(Object value) This method returns the property value modified by the editor. This method is called by builder tool whenever a property change event is fired NOTICE the parameter is an Object, so primitive datatypes must be wrapped and unwrapped using a wrapper class. Property value access methods Regardless of the presentation method, a property editor should support the following two methods:

  30. Property value access methods • getJavaInitializationSting() • Does not access the property directly • Called by the tool for automatic source code generation. • Returns code in the form of a String which you can use to construct initial value for a property. • The string value returned by this method can be passed as a method or constructor argument.

  31. Property change notification methods • When a property value is modified in the property sheet, the change has to be notified to the target bean. • This a accomplished by firing the propertyChange event. • The PropertyEditor interface has methods to implement the property editors as property event sources: • addPropertyChangeListener() • removePropertyChangeListener() • firePropertyChange()

  32. Property Editor Workflow

  33. Property Editor Workflow The builder tool does the following: • Fetches the property’s PropertyDescriptor object • Display name • Property type • getter/setter methods • Fetches the value of the property from the target bean • Invoke() method in the reflection API to invoke the property’s getter method • Value returned as Object • Locates the property’s editor • Sends the value to the property editor

  34. Property Editor Workflow The builder tool does the following (cont): • Registers with the property editor for the change event notification • Checks for the property presentation mode, if required displays a GUI interface. • Done by checking the getAsText(), getTags(), isPaintable(), and supportsCustomEditor() methods. • If in custom component mode, builder tools calls paintValue() and CustomEditor() • Receives the property change event notification when the user modifies the property. • Fetches the changed value of the property from the property editor • Done by calling the getValue() method in property editor. • Sends the value fetched in Step 7 to the target bean • Done using the setter method in the target bean

  35. Step 6 – Text property type • If • getAsText() method returns a non-null value • getTags() return NULL • isPaintable() returns NULL • Then the property appears as text and text modified by typing in the text field. • What happens in Step 6 • Property is changed • Builder Tools calls SetAsText() method • Passes the changed text as the new value • Property editor accepts the accepts the new value, converts it to the correct property type. • Property Editor calls SetValue() method • Property Editor invokes the firePropertyChange() method to tell the builder tool the value has changed. • Builder tools continues with the Step 7

  36. Step 6 – Enumeration property type • If • getAsText() method returns a non-null value • getTags() method returns non-null value • isPaintable() returns NULL • Then the property is an enumeration appears as a choice box and property is modified by selecting an item in the box. • What happens in Step 6 • When the builder tool invokes the getTags() method, the property editor returns an array of strings as the elements of the enumeration. • Builder tool uses these strings as items in the Choice component items. • When the user select an item in the list the builder Tools calls SetAsText() method • Passes the changed text as the new value • Property editor accepts the accepts the new value, converts it to the correct property type. • Property Editor calls SetValue() method • Property Editor invokes the firePropertyChange() method to tell the builder tool the value has changed. • Builder tools continues with the Step 7

  37. Step 6 – Custom component property type • If • getAsText() method returns NULL value • getTags() method returns NULL value • isPaintable() returns TRUE • Then the property the property editor will support a custom component. • Builder tools confirms this by calling the supportCustomEditor() • What happens in Step 6 • When the builder tool discovers that isPaintable() is TRUE, then it calls the paintValue() method. • The paintValue() method typically paints a rectangular area next to the property name in the property list. • When the user clicks on this area, the builder invokes the getCustomerEditor() method to get the GUI component to be displayed. • This is usually embedded in a modal dialog box. • User modifies the component property using the components in the dialog box. • When the property editor gets an indication that the property has been edited, it calls the setValue() method to internally change the property value. • The property editor then calls the firePropertyChange() event. • Builder tools continues with the Step 7

  38. Building custom property editors

  39. Building custom property editors • Builder tools provide standard property editors for primitive java data types. • If you use complex data types or user specified class as properties, then you must provide a custom property editor • The Utility class PropertyEditorSupport is required to build property editors

  40. PropertyEditorSupport class • Not all the methods in the PropertyEditor interface are needed for building custom property editor • The propertyChange event notification requires routine code. • The PropertyEditorSupport class • Implements the PropertyEditor Class • Provides the code needed for event handling. • A custom property editor will sub class the PropertyEditorSupport Class and override only the required methods.

  41. PropertyEditorSupport class • This class has 2 constructors • PropertyEditorSupport() • PropertyEditorSupport(Object o) • The Object o is the source object in which the propertyChange event occurred. • Now the firing event handling can be delegated to the PropertyEditorSupport class or it subclasses.

  42. Editors for properties presented as text

  43. Steps for building a standard property editor • Create a class by extending the PropertyEditorSupport class • Follow the “Editor” naming convention • Override the setValue(Object value) method • Convert the input value into the equivalent string • Save it in txtValue • Override the getAsText() and setAsText() method • getAsText() returns a non-null, returns the txtValue converted by the setValue() method • Override the getValue() method • Returns the actual value of the property • Override the getJavaInitializationString() method • If the property value can be constructed easily, provide the code in string format.

  44. Editors with custom components

  45. Steps for building a custom property editor • Create a class by extending the PropertyEditorSupport class • Follow the “Editor” naming convention • Override the isPaintable() method • Return TRUE indicates that paintValue() is supported • Override the paintValue(Graphic g, Rectangle r)method • Rectangle r represents the size of the edit field • When paintValue() is invoked, the Graphics object is created for the edit field and supplied to the that method. • Graphic can be used for writing text or drawing in the edit field.

  46. Steps for building a custom property editor • Override the SupportCustomEditor() method • Returns TRUE • Override getCustomEditor() • Override setValue(Object o) • Override getValue() • Override the getJavaInitializationString() method • If the property value can be constructed easily, provide the code in string format.

  47. Registering and locating property editors

  48. Registering and locating editors • Many beans can share the same property editor • The editor does not need to reside in the same directory as the bean. • It may not even reside in the same package hierarchy as the bean. • The PropertyEditorManager class provides the means to register and locate the correct property editor.

  49. Registering Editors • This is the way to let the builder tool know where the correct property editors to match to a particular property in a bean. • The PropertyEditorManager provides: • A static method registerEditor(Class targetClass, Class editorClass) • targetClass - meta class for target type • editorClass – meta class for editor class

  50. Where do you register a property editor? • Since registering a property editor is a design time task, then the BeanInfo class should be where you register the required property editors for your bean. • Property editor are registered using the following methods: • PropertyDescriptor() • The setPropertyEditorClass(Class editor) class will register the property editor for a particular property • getBeanDescriptor() • The registerEditor(Class editor) will register a particular class to its respective property editor.

More Related