Chapter 3 COP with Java Beans Component Oriented
Chapter 3 COP with Java. Beans Component Oriented Programming 1
Major Topics • Overview of Java. Beans technology • Discuss the component infrastructure of Java. Beans • Introducing the component model of Java. Beans • Learning the connection model of Java. Beans • Discussing the deployment model of Java. Beans • Discuss the key features and techniques of component oriented programming with Java. Beans Component Oriented Programming 2
Java. Beans • Java. Beans defines a software component model for Java, so that third party ISVs can create and ship Java components that can be composed together into applications” [Sun 1997] • “A Java. Bean is a reusable software component that can be manipulated visually in a builder tool” [Sun 1997] Component Oriented Programming 3
8 Steps to Create a Java. Bean (1) 1. Use package statement as the first line of your source code. 2. Implement the Serializable interface for persistence. 3. Compile your packaged classes using the -d option. 4. Create a manifest file to describe the contents of a JAR file. Component Oriented Programming 4
8 Steps to Create a Java. Bean (2) 5. Create the JAR file for your bean using the jar utility. 6. Check the files were archived correctly. 7. Test your Java bean wrapped in a JAR file. 8. Add the bean into the Bean. Box. Component Oriented Programming 5
Four Kinds of Properties • Simple properties – Simple properties describing a bean’s appearance and behavior • Bound properties – When their values change, provide event notification to other objects • Constrained properties – Value changes can be okayed or vetoed by other objects • Indexed properties – Multiple-value properties Component Oriented Programming 6
Simple Properties • Properties are aspects of a Bean's appearance and behavior that are changeable at design time. • Properties are private values accessed through getter and setter methods. • Property getter and setter method names follow specific rules, called design patterns. By using these design pattern-based method names, Java. Beans-enabled builder tools (and the Bean. Box) can – – – Discover a Bean's properties Determine the properties' read/write attributes Determine the properties' types Locate an appropriate property editor for each property type Display the properties' (usually in a property sheet) Alter those properties (at design time) Component Oriented Programming 7
Adding a Color Property to Simple. Bean 1. Create and initialize a private instance variable. private Color color = Color. green; 2. Write a public getter method. public Color get. Color(){ return color; } 3. Write a public setter method. public void set. Color(Color new. Color){ color = new. Color; repaint(); } 4. Override the paint() method inherited from Canvas. public void paint(Graphics g) { g. set. Color(color); g. fill. Rect(20, 5, 20, 30); } Component Oriented Programming 8
Bound Properties • Whenever a bound property changes, notification of the change is sent to interested listeners. • A bean containing a bound property must maintain a list of property change listeners and alert them when the bound property changes. • The convenience class Property. Change. Support first implements methods that add and remove Property. Change. Listener objects from a list and then fires Property. Change. Event objects at those listeners when the bound property changes. Component Oriented Programming 9
Implementing Bound Property Support 1. Import the java. beans package. (accessing to the Property. Change. Support class) 2. Instantiate a Property. Change. Support object: private Property. Change. Support changes = new Property. Change. Support(this); 3. Implement methods to maintain the property change listener list. public void add. Property. Change. Listener( Property. Change. Listener l) { changes. add. Property. Change. Listener(l); } public void remove. Property. Change. Listener( Property. Change. Listener l) { changes. remove. Property. Change. Listener(l); } 4. Modify a property's setter method to fire a property change event when the property is changed. Our. Button's set. Label method looks like this: public void set. Label(String new. Label) { String old. Label = label; label = new. Label; size. To. Fit(); changes. fire. Property. Change("label", old. Label, new. Label); } Note that set. Label stores the old label value, because both the old and new labels must be passed to fire. Property. Change. public void fire. Property. Change(String property. Name, Object old. Value, Object new. Value) Component Oriented Programming 10
Constrained Properties • A bean property is constrained when any change to that property can be vetoed. • Implementation: 3 parts: – A source bean containing one or more constrained properties – Listener objects that implement the Vetoable. Change. Listener interface. A listener object accepts or rejects proposed changes to a constrained property in the source bean – A Property. Change. Event object containing the property name and its old and new values. – Example: Jelly. Bean and Voter Component Oriented Programming 11
Indexed Properties • Indexed properties represent collections of values accessed, like an array, by index. • Methods to access the entire indexed property array – public <Property. Type>[] get<Property. Name>(); – public void set<Property. Name>(<Property. Type>[] value); • Methods to access individual values – public <Property. Type> get<Property. Name>(int index); – public void set<Property. Name>(int index, <Property. Type> value); Component Oriented Programming 12
Discovering Events • Using introspection – public void add<Event. Listener. Type>(<Event. Listener. Type> a) – public void remove<Event. Listener. Type>(<Event. Listener. Type> a) • Using Bean. Info – Example: Explicit. Button. Bean. Info. java – Note: this example works in JDK 1. 3 but not in JDK 1. 4! Component Oriented Programming 13
Viewing Events • Select a bean (e. g. Our. Button) and then pull down the Edit > Events menu • Hooking up events in the Bean. Box (e. g. Juggler animation) • Generating event adapter classes (beanboxtmpsunwbeanbox. . . ) • The Event. Monitor demo bean prints out source bean event reports at run time Component Oriented Programming 14
Introspector Class • Introspection – the capability of a builder tool to discover information about a bean • Exposing a bean’s features (P, E, M, but in BDK, you see only P) • Using the JDK core reflection API and design patterns • How could we (designers) explicitly expose a bean’s feature? Component Oriented Programming 15
Bean. Info Class • A separate, associated class that implements the Bean. Info interface. • A Bean. Info class can: – Expose only those features you want to expose. – Rely on Bean. Info to expose some Bean features while relying on low-level reflection to expose others. – Associate an icon with the target bean. – Specify a customizer class. – Segregate features into normal and expert categories. – Provide a more descriptive display name, or additional information about a bean feature. Component Oriented Programming 16
Feature Descriptors • Feature. Descriptor is the base class for the other descriptor classes. It declares the aspects common to all descriptor types. • Bean. Descriptor describes the target Bean's class type and name, and describes the target Bean's customizer class if it exists. • Property. Descriptor describes the target bean's properties. • Indexed. Property. Descriptor is a subclass of Property. Descriptor, and describes the target Bean's indexed properties. • Event. Set. Descriptor describes the events the target Bean fires. • Method. Descriptor describes the target Bean's methods. • Parameter. Descriptor describes method parameters. Component Oriented Programming 17
5 Steps to Creating a Bean. Info Class 1. Name your Bean. Info class as: Class. Name. Bean. Info 2. Subclass Simple. Bean. Info. 3. Override the appropriate methods to return the properties, methods, or events that you want exposed. 4. Optionally associate an icon with the target bean. 5. Specify the target bean class, and, if the bean has a customizer, specify it also. Component Oriented Programming 18
Customization 1. Using a property editor • Each bean property has its own property editor 2. Using customizers • • Customizers are used when property editors are not practical or applicable. Property editors are associated with individual properties, while a customizer is associated with a bean. Component Oriented Programming 19
Property Editors • Explicit association via a Bean. Info object. E. g. , Molecule demo bean: Within the Molecule. Bean. Info class, the Molecule bean's property editor is set with the following line of code: pd. set. Property. Editor. Class(Molecule. Name. Editor. class); • Explicit registration via java. Beans. Property. Editor. Manager. register. Editor. This method takes a pair of arguments: The class type, and the editor to be associated with that type. • Name search. If a class has no explicitly associated property editor, then the Property. Editor. Manager searchs for that class's property editor by: • Appending "Editor" to the fully qualified class name. For example, for the java. beans. Complex. Number class, the property editor manager would search for the java. beans. Complex. Number. Editor class. • Appending "Editor" to the class name and searching a class search path. The default class path for the Bean. Box is sun. beans. editors. Component Oriented Programming 20
Customizers • Providing complete control over how to configure or edit a bean • Extend java. awt. Component or one of its subclasses. • Implement the java. beans. Customizer interface This means implementing methods to register Property. Change. Listener objects, and firing property change events at those listeners when a change to the target Bean has occurred. • Implement a default constructor. • Associate the customizer with its target class via Bean. Info. get. Bean. Descriptor. • If a Bean that has an associated Customizer is dropped into the Bean. Box, you will notice a "Customize. . . " item on the Edit menu. Component Oriented Programming 21
Persistence • All beans must persist. • A bean persists by having its properties, fields, and state information saved and restored to and from storage. • The mechanism that makes persistence possible is called serialization. • When a bean instance is serialized, it is converted into a data stream and written to storage. Any applet, application, or tool that uses that bean can then "reconstitute" it by deserialization. Component Oriented Programming 22
Serializable Interface • The Serializable interface provides automatic serialization by using the Java Object Serialization tools. • Classes that implement Serializable must have a noargument constructor. This constructor will be called when an object is "reconstituted" from a. ser file. • You don't need to implement Serializable in your class if it is already implemented in a superclass (but you do need to make sure works correctly and as you expect with default serialization). • All fields but static and transient are serialized. Use the transient modifier to specify fields you do not want serialized, and to specify classes that are not serializable. Component Oriented Programming 23
Overview of Bean Builder • The Bean Builder is a tool which allows the visual assembly of an application by instantiating and setting the properties of components based on the Java. Beans component architecture. • The dynamic behavior of the application is specified by "wiring" relationships that represent events handlers and method calls between the objects in an application. • The state of this application is saved to and restored from an XML file. An application is constructed using the Java API without having to write a line of source code. Component Oriented Programming 24
Component Oriented Programming 25
Component Oriented Programming 26
<? xml version="1. 0" encoding="UTF-8" ? > <java version="1. 3. 0" class="java. beans. XMLDecoder"> <object class="javax. swing. JPanel"> <void method="add"> <object class="javax. swing. JButton"> <void property="label"> <string>Clear</string> </void> <void property="model"> <void property="action. Command"> <string /> </void> <void method="add. Action. Listener"> <object class="java. beans. Event. Handler" method="create"> <class>java. awt. event. Action. Listener</class> <object id="JText. Area 0" class="javax. swing. JText. Area"> <void property="border"> <object class="javax. swing. border. Line. Border"> <object class="java. awt. Color"> <int>0</int> <int>255</int> </object> <int>1</int> </object> </void> </object> Component Oriented Programming <string>set. Text</string> <string>source. action. Command</string> </object> </void> <void method="add"> <object idref="JText. Area 0" /> </void> <void property="layout"> <null /> </void> </object> </java> 27
Discussion • The Bean Builder extends the capabilities of the original Bean. Box by demonstrating new techniques for persistence, layout editing and dynamic event adapter generation. • The Java Bean was designed for the construction of graphical user interface (GUI)? Component Oriented Programming 28
Summary (1) • Java. Bean is a component model for Java • A bean has four kinds of properties – Simple, bound, constrained, and indexed • BDK is a visual design tool provided by Sun Microsystems • There are 5 steps to build a Java. Bean • Events can be discovered by introspection or Bean. Info class Component Oriented Programming 29
Summary (2) • Introspection – the capability of a builder tool to discover information about a bean • A Bean. Info class can expose only those features you want to expose • There are 5 steps to creating a Bean. Info class • Customization can be done by property editors or customizers • Persistence is done by serialization • Bean Builder provides more capability for component compositions • The component architecture of Java beans could be described using the similar diagrams used in device beans and CCM Component Oriented Programming 30
- Slides: 30