Features of Java Beans Support for introspection so
Features of Java. Beans – 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
Java. Bean 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
Persistent Storage • Purpose: – To use existing data formats and plug into OLE or Open. Doc 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
Bean Naming Conventions • String ss; // get. Ss set. Ss • Properties – Public X get. Property. Name() – Public void Set. Property. Name(X x) – Note: above interacts w/ property. Name • Boolean Properties – Public boolean is. Property. Name() – Public void set. Property. Name(boolean b)
More Bean Conventions • Indexed Properties – – • • • X[] get. Property. Name() Void set. Property. Name(X[] x) X get. Property. Name(int i) Void set. Property. Name(int I, X x) Bound Properties Constrained Properties Custom Events Property Editors The Bean. Info Class
Naming rules Constructors A bean has a no argument constructor Simple Properties public T get. N() public void set. N ( T value) Boolean Properties public boolean is. N() public boolean get. N() public void set. N(boolean value) Indexed Properties public value) public T get. N(int index) T[] get. N() void set. N(int index, T void set. N(T[] values)
Coding examples for Properties public class alden 2 extends Canvas { String name = “Hello”; public alden 2() { set. Background (Color. red); set. Foreground (Color. blue); } public void set. Name (String new. Name) { name = new. Name; } public String get. Name() { return name; } public Dimension get. Minimun. Size() { return new Dimension (50, 50); } }
Bound Properties • Generates notification when a property is changed. public class propert. Demo extends Canvas { String our. String = “Hello”; private Property. Change. Support changes = new Property. Change. Support(this); . . . public void set. String (String new. String) { String old. String = our. String; our. String = new. String; changes. fire. Property. Change(“string”, old. String, new. String); }. . .
Bound Properties. . . public void add. Property. Change. Listener (Property. Change. Listener l) { pcs. add. Property. Change. Listener (l); } public void remove. Property. Change. Listener (Property. Change. Listener l) { pcs. remove. Property. Change. Listener(l); } }
Events • Two types of objects are involved: – “Source” objects. – “Listener” objects. • Based on registration. • Makes use of parametric polymorphism.
Events • Message sent from one object to another. • Sender fires event, recipient (listener) handles the event • There may be many listeners. Event source Fire event Event Object Register listener Event listener
Event source Register event listener Fire event Event Object Forward event Event Object Event Listener Event Adapter Provide interface
Bean Events • Define a new Event class which extends Event. Object. XEvent • Define a new interface for listeners to implement, this must be an extension of Event. Listener. XEvent. Listener • The Source must provide methods to allow listeners to register and unregister eg add. XListener(), remove. XListener(). • The source must provide code to generate the event and send it to all registered listeners. fire. XEvent() • The listener must implement the interface to receive the event. change. X() • The listener must register with the source to receive the event.
Events public class event. Source extends Generic. Event. Generator {. . . private Vector my. Listeners = new Vector(); . . . public synchronized void add. My. Event. Listeners ( My. Event. Listener l) { my. Listeners. add. Element(l); } public synchronized void remove. My. Event. Listeners ( My. Event. Listener l) { my. Listeners. remove. Element(l); }. . .
Events. . . private void fanout. Events() { Vector l; synchronized (this) { l = (Vector) my. Listener. clone(); } for (int i = 0; i < l. size(); i++) { My. Event. Listener mel = (My. Event. Listener) l. element. At(i); mel. handle. This. Event (this); } }
Property Editors
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
Property Sheet • A Panel where editable bean properties appear along with their edit fields
Property Editor • Needs to be associated with a datatype
Customization. . . <classname>Editor package sun. beanbox. beans; import java. beans. *; public class Nervous. Text 07 Text. Property. Editor extends Property. Editor. Support { public String[] get. Tags() { String values[] = { "Nervous Text", "Anxious Text", "Funny Text", "Wobbly Text"}; return values; } }
BDK Output
Building Property Editors
Property presentation – set. As. Text(), get. Tags(), is. Paintable, paint. Value() get. Custom. Editor()
Property. Editor values • Property value access – set. Value(), get. Java. Initialization. String() • Property change notification – add. Property. Change. Listener(), remove. Property. Change. Listener(), fire. Property. Change()
Property presentation methods • The propety. Editor interface supports different ways of presenting and editing a property. • The code in get. As. Text(), get. Tags(), and is. Paintable() control how properties are presented on the property sheet. • There are three presentation modes – As text – As an Enumerated type – As a custom component
Presenting Properties as text • Used when – Simple properties are usually presented as text • Settings: – Is. Paintable() returns FALSE – get. As. Text() returns a nonnull vlue • Use the set. As. Text(string txt) method to set the value property to a string. – Method throws the Illegal. Argrument. Except ion if argument is not formatted properly or property can not be expressed as a string.
Presenting Properties as an enumerated type • Used when – A property that takes discrete values can be expressed as an enumerated type • Settings: – Is. Paintable() returns FALSE – get. As. Text() returns a non-null value – get. Tags() returns a non-null value
Presenting Properties as a custom component • Used When – A property that can not be expressed as text can be displayed using GUI components • Settings: – Is. Paintable() returns TRUE indicating to the builder tool that the paint. Value() method is supported – get. As. Text() returns NULL – getags() returns NULL • The paint. Value() method should contain code to paint a rectangular area next to the property in the property list. – The get. Custom. Editor() method should return a component used for editing.
Property value access methods Regardless of the presentation method, a property editor should support the following two methods: • get. Value() – 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 • set. Value(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 • get. Java. Initialization. Sting() – 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.
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 property. Change event. • The Property. Editor interface has methods to implement the property editors as property event sources: – add. Property. Change. Listener() – remove. Property. Change. Listener() – fire. Property. Change()
Property Editor Workflow
Property Editor Workflow The builder tool does the following: 1. Fetches the property’s Property. Descriptor object • • • 2. Fetches the value of the property from the target bean • • 3. 4. Display name Property type getter/setter methods 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
Property Editor Workflow The builder tool does the following (cont): 5. 6. Registers with the property editor for the change event notification Checks for the property presentation mode, if required displays a GUI interface. – – 7. 8. 9. Done by checking the get. As. Text(), get. Tags(), is. Paintable(), and supports. Custom. Editor() methods. If in custom component mode, builder tools calls paint. Value() and Custom. Editor() 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 get. Value() method in property editor. Sends the value fetched in Step 7 to the target bean – Done using the setter method in the target bean
Step 6 – Text property type • If – get. As. Text() method returns a non-null value – get. Tags() return NULL – is. Paintable() 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 Set. As. Text() method Passes the changed text as the new value Property editor accepts the new value, converts it to the correct property type. – Property Editor calls Set. Value() method – Property Editor invokes the fire. Property. Change() method to tell the builder tool the value has changed. • Builder tools continues with the Step 7
Step 6 – Enumeration property type • If – get. As. Text() method returns a non-null value – get. Tags() method returns non-null value – is. Paintable() 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 • Builder tools continues with the Step 7 – When the builder tool invokes the get. Tags() 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 Set. As. Text() method – Passes the changed text as the new value – Property editor accepts the new value, converts it to the correct property type. – Property Editor calls Set. Value() method – Property Editor invokes the fire. Property. Change() method to tell the builder tool the value has changed.
Step 6 – Custom component property type • If – get. As. Text() method returns NULL value – get. Tags() method returns NULL value – is. Paintable() returns TRUE • • • Then the property editor will support a custom component. Builder tools confirms this by calling the support. Custom. Editor() What happens in Step 6 • Builder tools continues with the Step 7 – When the builder tool discovers that is. Paintable() is TRUE, then it calls the paint. Value() method. – The paint. Value() 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 get. Customer. Editor() 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 set. Value() method to internally change the property value. – The property editor then calls the fire. Property. Change() event.
Building custom property editors
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 Property. Editor. Support is required to build property editors
Property. Editor. Support class • Not all the methods in the Property. Editor interface are needed for building custom property editor • The property. Change event notification requires routine code. • The Property. Editor. Support class – Implements the Property. Editor Class – Provides the code needed for event handling. • A custom property editor will sub class the Property. Editor. Support Class and override only the required methods.
Property. Editor. Support class • This class has 2 constructors – Property. Editor. Support() – Property. Editor. Support(Object o) • The Object o is the source object in which the property. Change event occurred. • Now the firing event handling can be delegated to the Property. Editor. Support class or it subclasses.
Editors for properties presented as text
Steps for building a standard property editor • Create a class by extending the Property. Editor. Support class – Follow the “Editor” naming convention • Override the set. Value(Object value) method – Convert the input value into the equivalent string – Save it in txt. Value • Override the get. As. Text() and set. As. Text() method – get. As. Text() returns a non-null, returns the txt. Value converted by the set. Value() method • Override the get. Value() method – Returns the actual value of the property • Override the get. Java. Initialization. String() method – If the property value can be constructed easily, provide the code in string format.
Editors with custom components
Steps for building a custom property editor • Create a class by extending the Property. Editor. Support class – Follow the “Editor” naming convention • Override the is. Paintable() method – Return TRUE indicates that paint. Value() is supported • Override the paint. Value(Graphic g, Rectangle r)method – Rectangle r represents the size of the edit field – When paint. Value() 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.
Steps for building a custom property editor • Override the Support. Custom. Editor() method – Returns TRUE • Override get. Custom. Editor() • Override set. Value(Object o) • Override get. Value() • Override the get. Java. Initialization. String() method – If the property value can be constructed easily, provide the code in string format.
Registering and locating property editors
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 Property. Editor. Manager class provides the means to register and locate the correct property editor.
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 Property. Editor. Manager provides: – A static method register. Editor(Class target. Class, Class editor. Class) • target. Class - meta class for target type • editor. Class – meta class for editor class
Where do you register a property editor? • Since registering a property editor is a design time task, then the Bean. Info class should be where you register the required property editors for your bean. • Property editor are registered using the following methods: – Property. Descriptor() • The set. Property. Editor. Class(Class editor) class will register the property editor for a particular property – get. Bean. Descriptor() • The register. Editor(Class editor) will register a particular class to its respective property editor.
Locating editors • The Property. Editor. Manager has 3 way to locate and editor: 1. Using a method to register the custom build editor 2. Searching for a class whose name is created by appending the word “Editor” to the fully qualified class name – for Class = Foo -> Foo. Editor 3. Searching the packages available in the classpath using the naming convention used in technique 2
- Slides: 51