Chapter 3 COP with JavaBeans - PowerPoint PPT Presentation

1 / 30
About This Presentation
Title:

Chapter 3 COP with JavaBeans

Description:

Indexed properties represent collections of values accessed, like an array, by index. ... How could we (designers) explicitly expose a bean's feature? ... – PowerPoint PPT presentation

Number of Views:109
Avg rating:3.0/5.0
Slides: 31
Provided by: andy240
Category:
Tags: cop | javabeans | chapter

less

Transcript and Presenter's Notes

Title: Chapter 3 COP with JavaBeans


1
Chapter 3 COP with JavaBeans
2
Major Topics
  • Overview of JavaBeans technology
  • Discuss the component infrastructure of JavaBeans
  • Introducing the component model of JavaBeans
  • Learning the connection model of JavaBeans
  • Discussing the deployment model of JavaBeans
  • Discuss the key features and techniques of
    component oriented programming with JavaBeans

3
JavaBeans
  • JavaBeans 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 JavaBean is a reusable software component that
    can be manipulated visually in a builder tool
    Sun 1997

4
8 Steps to Create a JavaBean (1)
  • Use package statement as the first line of your
    source code.
  • Implement the Serializable interface for
    persistence.
  • Compile your packaged classes using the -d
    option.
  • Create a manifest file to describe the contents
    of a JAR file.

5
8 Steps to Create a JavaBean (2)
  • Create the JAR file for your bean using the jar
    utility.
  • Check the files were archived correctly.
  • Test your Java bean wrapped in a JAR file.
  • Add the bean into the BeanBox.

6
Four Kinds of Properties
  • Simple properties
  • Simple properties describing a beans 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

7
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,
    JavaBeans-enabled builder tools (and the BeanBox)
    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)

8
Adding a Color Property to SimpleBean
  • Create and initialize a private instance
    variable.
  • private Color color Color.green
  • Write a public getter method.
  • public Color getColor() return color
  • Write a public setter method.
  • public void setColor(Color newColor)
  • color newColor repaint()
  • Override the paint() method inherited from
    Canvas.
  • public void paint(Graphics g)
  • g.setColor(color) g.fillRect(20, 5, 20,
    30)

9
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 PropertyChangeSupport first
    implements methods that add and remove
    PropertyChangeListener objects from a list and
    then fires PropertyChangeEvent objects at those
    listeners when the bound property changes.

10
Implementing Bound Property Support
  • Import the java.beans package. (accessing to the
    PropertyChangeSupport class)
  • Instantiate a PropertyChangeSupport object
  • private PropertyChangeSupport changes new
    PropertyChangeSupport(this)
  • Implement methods to maintain the property change
    listener list.
  • public void addPropertyChangeListener(
    PropertyChangeListener l) changes.addPropertyCha
    ngeListener(l)
  • public void removePropertyChangeListener(
    PropertyChangeListener l) changes.removeProperty
    ChangeListener(l)
  • Modify a property's setter method to fire a
    property change event when the property is
    changed. OurButton's setLabel method looks like
    this
  • public void setLabel(String newLabel) String
    oldLabel label label newLabel sizeToFit()
    changes.firePropertyChange("label", oldLabel,
    newLabel)
  • Note that setLabel stores the old label value,
    because both the old and new labels must be
    passed to firePropertyChange.
  • public void firePropertyChange(String
    propertyName, Object oldValue, Object newValue)

11
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
    VetoableChangeListener interface. A listener
    object accepts or rejects proposed changes to a
    constrained property in the source bean
  • A PropertyChangeEvent object containing the
    property name and its old and new values.
  • Example JellyBean and Voter

12
Indexed Properties
  • Indexed properties represent collections of
    values accessed, like an array, by index.
  • Methods to access the entire indexed property
    array
  • public ltPropertyTypegt getltPropertyNamegt()
  • public void setltPropertyNamegt(ltPropertyTypegt
    value)
  • Methods to access individual values
  • public ltPropertyTypegt getltPropertyNamegt(int
    index)
  • public void setltPropertyNamegt(int index,
    ltPropertyTypegt value)

13
Discovering Events
  • Using introspection
  • public void addltEventListenerTypegt(ltEventListenerT
    ypegt a)
  • public void removeltEventListenerTypegt(ltEventListen
    erTypegt a)
  • Using BeanInfo
  • Example ExplicitButtonBeanInfo.java
  • Note this example works in JDK1.3 but not in
    JDK1.4!

14
Viewing Events
  • Select a bean (e.g. OurButton) and then pull down
    the Edit gt Events menu
  • Hooking up events in the BeanBox (e.g. Juggler
    animation)
  • Generating event adapter classes
    (\beanbox\tmp\sunw\beanbox\...)
  • The EventMonitor demo bean prints out source bean
    event reports at run time

15
Introspector Class
  • Introspection the capability of a builder tool
    to discover information about a bean
  • Exposing a beans 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
    beans feature?

16
BeanInfo Class
  • A separate, associated class that implements the
    BeanInfo interface.
  • A BeanInfo class can
  • Expose only those features you want to expose.
  • Rely on BeanInfo 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.

17
Feature Descriptors
  • FeatureDescriptor is the base class for the other
    descriptor classes. It declares the aspects
    common to all descriptor types.
  • BeanDescriptor describes the target Bean's class
    type and name, and describes the target Bean's
    customizer class if it exists.
  • PropertyDescriptor describes the target bean's
    properties.
  • IndexedPropertyDescriptor is a subclass of
    PropertyDescriptor, and describes the target
    Bean's indexed properties.
  • EventSetDescriptor describes the events the
    target Bean fires.
  • MethodDescriptor describes the target Bean's
    methods.
  • ParameterDescriptor describes method parameters.

18
5 Steps to Creating a BeanInfo Class
  • Name your BeanInfo class as ClassNameBeanInfo
  • Subclass SimpleBeanInfo.
  • Override the appropriate methods to return the
    properties, methods, or events that you want
    exposed.
  • Optionally associate an icon with the target
    bean.
  • Specify the target bean class, and, if the bean
    has a customizer, specify it also.

19
Customization
  • Using a property editor
  • Each bean property has its own property editor
  • 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.

20
Property Editors
  • Explicit association via a BeanInfo object. E.g.,
    Molecule demo bean Within the MoleculeBeanInfo
    class, the Molecule bean's property editor is set
    with the following line of code
    pd.setPropertyEditorClass(MoleculeNameEditor.class
    )
  • Explicit registration via java.Beans.PropertyEdito
    rManager.registerEditor. 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
    PropertyEditorManager searchs for that class's
    property editor by
  • Appending "Editor" to the fully qualified class
    name. For example, for the java.beans.ComplexNumbe
    r class, the property editor manager would search
    for the java.beans.ComplexNumberEditor class.
  • Appending "Editor" to the class name and
    searching a class search path. The default class
    path for the BeanBox is sun.beans.editors.

21
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
    PropertyChangeListener 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 BeanInfo.getBeanDescriptor.
  • If a Bean that has an associated Customizer is
    dropped into the BeanBox, you will notice a
    "Customize..." item on the Edit menu.

22
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.

23
Serializable Interface
  • The Serializable interface provides automatic
    serialization by using the Java Object
    Serialization tools.
  • Classes that implement Serializable must have a
    no-argument 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.

24
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 JavaBeans 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.

25
(No Transcript)
26
(No Transcript)
27
  • lt?xml version"1.0" encoding"UTF-8" ?gt    ltjava
    version"1.3.0" class"java.beans.XMLDecoder"gt
        ltobject class"javax.swing.JPanel"gt      
    ltvoid method"add"gt         ltobject
    class"javax.swing.JButton"gt           ltvoid
    property"label"gt             ltstringgtClearlt/stri
    nggt            lt/voidgt           ltvoid
    property"model"gt             ltvoid
    property"actionCommand"gt               ltstring
    /gt              lt/voidgt           lt/voidgt
              ltvoid method"addActionListener"gt
                ltobject class"java.beans.EventHandle
    r" method"create"gt              
    ltclassgtjava.awt.event.ActionListenerlt/classgt 
                  ltobject id"JTextArea0"
    class"javax.swing.JTextArea"gt                
    ltvoid property"border"gt                  
    ltobject class"javax.swing.border.LineBorder"gt
                        ltobject class"java.awt.Color
    "gt                       ltintgt0lt/intgt 
                          ltintgt0lt/intgt 
                          ltintgt0lt/intgt 
                          ltintgt255lt/intgt 
                        lt/objectgt                   
      ltintgt1lt/intgt                    lt/objectgt
                    lt/voidgt               lt/objectgt
  •               ltstringgtsetTextlt/stringgt 
                  ltstringgtsource.actionCommandlt/strin
    ggt              lt/objectgt           lt/voidgt
            lt/objectgt       lt/voidgt       ltvoid
    method"add"gt         ltobject idref"JTextArea0"
    /gt        lt/voidgt       ltvoid
    property"layout"gt         ltnull /gt       
    lt/voidgt     lt/objectgt   lt/javagt 

28
Discussion
  • The Bean Builder extends the capabilities of the
    original BeanBox 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)?

29
Summary (1)
  • JavaBean 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 JavaBean
  • Events can be discovered by introspection or
    BeanInfo class

30
Summary (2)
  • Introspection the capability of a builder tool
    to discover information about a bean
  • A BeanInfo class can expose only those features
    you want to expose
  • There are 5 steps to creating a BeanInfo 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
Write a Comment
User Comments (0)
About PowerShow.com