Java Beans - PowerPoint PPT Presentation

1 / 48
About This Presentation
Title:

Java Beans

Description:

'A Java bean is a reusable software component that can be ... Scroll Bars. Dialogs. VBX, OLE. Visually Manipulated, Builder Tools. Property Sheet. Method Tracer ... – PowerPoint PPT presentation

Number of Views:247
Avg rating:3.0/5.0
Slides: 49
Provided by: pllabCs
Category:
Tags: beans | java | scrollbars

less

Transcript and Presenter's Notes

Title: Java Beans


1
Java Beans
2
Content
  • What is a Java Bean?
  • BDK
  • What makes Bean possible?
  • Support for Java Bean
  • References

3
What are Java Beans?
  • Software Component
  • Specified interfaces.
  • Independent deployment.
  • Analogy
  • Electronic components.
  • Automotive components.

4
What are Java Beans? ...
  • Definition
  • A Java bean is a reusable software component
    that can be visually manipulated in builder
    tools.
  • Java Bean Tutorials
  • The JavaBeans Specs are available for d/l
    athttp//java.sun.com/products/javabeans/docs/

5
What is JavaBeans?
  • A Java Bean is a reusable software component that
    can be manipulated visually in a builder tool
  • JavaBeans is a portable, platform-independent
    component model written in the Java programming
    language, developed in collaboration with
    industry leaders.
  • It enables developers to write reusable
    components once and run them anywhere --
    benefiting from the platform-independent power of
    Java technology.
  • The goal of JavaBeans is to create a system
    whereby application developers can take a set of
    beans from a stock library and wire them together
    to make a full application

6
Software Components
A software component is a unit of composition
with contractually specified interfaces and
explicit context dependencies only. A software
component can be deployed independently and is
subject to composition by third
parties. Szyperski ECOOP96
7
Composition not Inheritance
In OO languages, new objects are created from old
using the inheritance mechanism. Pluggable
components are connected together by composition
rather than inheritance. Most of the flexibility
of inheritance can be gained from various
compositional tactics.
8
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

9
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

10
Software Components
  • Buttons
  • Text Fields
  • List Boxes
  • Scroll Bars
  • Dialogs
  • VBX, OLE

11
Visually Manipulated, Builder Tools
BeanBox
ToolBox
Property Sheet
Method Tracer
12
BDK...
13
Selecting the events
14
Attaching Events
15
Generating Adapter...
16
Making an Applet is a click away!
17
Applet Done!
18
What makes this possible?
  • Properties
  • Events
  • Persistence
  • Introspection
  • Customization

19
Properties
  • Attributes.
  • Can be read/write, read-only or write-only.
  • Several types of properties
  • Simple
  • Indexed
  • Bound
  • Constrained

20
Design Pattern 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)
21
Coding examples for Properties
  • public class alden2 extends Canvas
  • String ourString Hello
  • public alden2()
  • setBackground (Color.red)
  • setForeground (Color.blue)
  • public void setString (String newString)
    ourString newString
  • public String getString() return ourString
  • public Dimension getMinimunSize()
  • return new Dimension (50, 50)

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

23
Bound Properties...
  • ...
  • public void addPropertyChangeListener
    (PropertyChangeListener l)
  • changes.addPropertyChangeListener (l)
  • public void removePropertyChangeListener
    (PropertyChangeListener l)
  • changes.removePropertyChangeListener(l)

24
Constrained Property
  • The concept of a Watcher object.
  • subscribe to the VetoableChange
  • Can veto a property change using a
    PropertyVetoException.

25
Constrained Property...
  • public class JellyBean extends Canvas
  • private PropertyChangeSupport changes new
    PropertyChangeSupport (this)
  • private VetoableChangeSupport vetos new
    VetoableChangeSupport (this)
  • ...
  • public void setColor (int newColor) throws
    PropertyVetoException
  • int oldColor currentColor
  • vetos.fireVetoableChange(setColor,
    newInteger(oldColor),
  • newInteger(newColor))
  • currentColor newColor

26
Constrained Property...
  • changes.firePropertyChange(setColor, new
    Integer(oldColor),
  • new Integer(newColor))
  • public void addVetoableChangeListener
    (VetoableChangeListener l)
  • vetos.addVetoableChangeListener(l)
  • public void removeVetoableChangeListener(Vetoable
    ChangeListener l)
  • vetos.removeVetoableChangeListener(l)

27
Events
  • Two types of objects are involved
  • Source objects.
  • Listener objects.
  • Based on registration.
  • Makes use of parametric polymorphism.

28
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
29
Event source
Register event listener
Fire event
Event Adapter
Event Object
Forward event
Event Object
Provide interface
Event Listener
30
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.

31
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)
  • ...

32
Events...
  • private void fanoutEvents()
  • Vector l
  • synchronized (this)
  • l (Vector) myListener.clone()
  • for (int i 0 i lt l.size() i)
  • MyEventListener mel (MyEventListener)
    l.elementAt(i)
  • mel.handleThisEvent (this)

33
Persistence
  • Allows the graphical builder to recall the state
    of a bean.
  • public class Button implements java.io.Serializabl
    e ...
  • Selected property fields can bypass the
    serialization using keywords transient or static.

34
Persistence...
  • writeObject and readObject
  • private void writeObject (java.io.ObjectOutputStre
    am s)
  • throws java.io.IOException
  • private void readObject (java.io.ObjectInputStream
    s)
  • throws java.io.IOException,
    java.lang.ClassNotFoundException
  • Allows the customization of objects.
  • Appearance and behavior can be stored and
    recalled.
  • Dont store references to other beans.

35
Introspection
  • A mechanism that allows the builder tool to
    analyze a bean.
  • Two ways to analyze a bean
  • low-level reflection APIs.
  • vendor provided explicit information
    (Customization).
  • Application builder will provide default BeanInfo
    class ltclassnamegtBeanInfo.

36
BDK Example
  • package acme.beans
  • import java.awt.
  • import java.io.Serializable
  • public class Acme04Bean extends Canvas implements
    Serializable
  • public Acme04Bean()
  • resize(60,40)
  • this.label"Bean"
  • setFont(new Font("Dialog", Font.PLAIN,
    12))

37
Introspection...
  • public void paint(Graphics g)
  • g.setColor(beanColor)
  • g.setColor(Color.blue)
  • int width size().width
  • int height size().height
  • FontMetrics fm g.getFontMetrics()
  • g.drawString(label, (width -
    fm.stringWidth(label)) / 2,
  • (height
    fm.getMaxAscent() - fm.getMaxDescent()) / 2)
  • public Color getColor()
  • return beanColor

38
Introspection...
  • public void setColor(Color newColor)
  • beanColor newColor
  • repaint()
  • public String getLabel()
  • return label
  • public void setLabel(String newLabel)
  • String oldLabel label
  • label newLabel
  • private Color beanColor Color.cyan
  • private String label

39
Introspection...
40
Customization
  • Similar to Introspection.
  • Develop your own ltclassnamegtBeanInfo class which
    extends SimpleBeanInfo.
  • Develop your own ltclassnamegtEditor class which
    extends PropertyEditorSupport to custom build
    your property editor.

41
Customization ...ltclassnamegtBeanInfo
  • package sun.beanbox.beans
  • import java.beans.
  • public class NervousText07BeanInfo extends
    SimpleBeanInfo
  • private final static Class beanClass
  • NervousText07.class
  • public BeanDescriptor getBeanDescriptor()
  • BeanDescriptor bd new BeanDescriptor(beanCla
    ss)
  • bd.setDisplayName("Uneasy Text 07")
  • return bd

42
Customization...
  • public PropertyDescriptor getPropertyDescriptors
    ()
  • try
  • PropertyDescriptor textPD
  • new PropertyDescriptor("text",
    beanClass)
  • PropertyDescriptor rv textPD
  • textPD.setPropertyEditorClass(NervousText07T
    extPropertyEditor.class)
  • return rv
  • catch (IntrospectionException e)
  • throw new Error(e.toString())

43
Customization...ltclassnamegtEditor
  • 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

44
BDK Output
45
Conclusion
  • Easy to use.
  • Difficult to build.
  • Like all OO design, needs careful planning.
  • Similar to the String library in C.
  • Wide selection of JavaBeans in the future?

46
Support
  • BDK - Sun
  • NetBeans www.netbeans.org
  • Jbuilder - Inprise
  • Super Mojo - Penumbra Software
  • Visual Age for Java - IBM
  • Visual Cafe - Symantec Corporation
  • JDeveloper Suite - Oracle

47
References
  • http//java.sun.com/products/javabeans/
  • Java Developer Connection Tutorials
  • Java Beans, Part 1 to 4
  • http//developer.java.sun.com/developer/onlineTrai
    ning/

48
References...
  • Trail JavaBeans
  • http//java.sun.com/docs/books/tutorial/javabeans/
    index.html
  • JavaBeans 1.01 Specification
  • http//java.sun.com/beans/docs/spec.html
Write a Comment
User Comments (0)
About PowerShow.com