Java Beans
Content <ul><li>What is a Java Bean? </li></ul><ul><li>BDK </li></ul><ul><li>What makes Bean possible? </li></ul><ul><li>S...
What are Java Beans? <ul><li>Software Component </li></ul><ul><ul><li>Specified interfaces. </li></ul></ul><ul><ul><li>Ind...
What are Java Beans? ... <ul><li>Definition: </li></ul><ul><li>“ A Java bean is a reusable software component that can be ...
What is JavaBeans? <ul><li>A Java Bean is a reusable software component that can be manipulated visually in a builder tool...
Software Components “ A software component is a unit of composition with contractually specified interfaces and explicit c...
Composition not Inheritance In OO languages, new objects are created from old using the inheritance mechanism. Pluggable c...
Features of JavaBeans <ul><ul><li>Support for  introspection </li></ul></ul><ul><ul><ul><li>so that a builder tool can ana...
Persistent Storage <ul><li>Purpose: </li></ul><ul><ul><li>To use existing data formats and plug into OLE or OpenDoc docume...
Software Components <ul><li>Buttons </li></ul><ul><li>Text Fields </li></ul><ul><li>List Boxes </li></ul><ul><li>Scroll Ba...
Visually Manipulated, Builder Tools Property Sheet Method Tracer BeanBox ToolBox
BDK...
Selecting the events
Attaching Events
Generating Adapter...
Making an Applet is a click away!
Applet Done!
What makes this possible? <ul><li>Properties </li></ul><ul><li>Events </li></ul><ul><li>Persistence </li></ul><ul><li>Intr...
Properties <ul><li>Attributes. </li></ul><ul><li>Can be read/write, read-only or write-only. </li></ul><ul><li>Several typ...
Design Pattern rules Constructors  A bean has a no argument constructor Simple Properties  public T getN() public void set...
Coding examples for Properties <ul><li>public class alden2 extends Canvas { </li></ul><ul><li>String ourString = “Hello”; ...
Bound Properties <ul><li>Generates notification when a property is changed. </li></ul><ul><li>public class propertDemo ext...
Bound Properties... <ul><li>... </li></ul><ul><li>public void addPropertyChangeListener (PropertyChangeListener l) { </li>...
Constrained Property <ul><li>The concept of a Watcher object. </li></ul><ul><ul><li>subscribe to the ‘VetoableChange’ </li...
Constrained Property... <ul><li>public class JellyBean extends Canvas { </li></ul><ul><li>private PropertyChangeSupport ch...
Constrained Property... <ul><li>changes.firePropertyChange(“setColor”, new Integer(oldColor), </li></ul><ul><li>  new Inte...
Events <ul><li>Two types of objects are involved: </li></ul><ul><ul><li>“ Source” objects. </li></ul></ul><ul><ul><li>“ Li...
Events <ul><li>Message sent from one object to another. </li></ul><ul><li>Sender  fires  event, recipient (listener)  hand...
Event source Event Adapter Fire event Event  Object Register event listener Event Listener Forward event Event  Object Pro...
Bean Events <ul><li>Define a new Event class which extends EventObject.  XEvent </li></ul><ul><li>Define a new interface f...
Events <ul><li>public class eventSource extends GenericEventGenerator { </li></ul><ul><li>... </li></ul><ul><li>private Ve...
Events... <ul><li>private void fanoutEvents() { </li></ul><ul><li>Vector l; </li></ul><ul><li>synchronized (this) { </li><...
Persistence <ul><li>Allows the graphical builder to recall the state of a bean. </li></ul><ul><li>public class Button impl...
Persistence... <ul><li>writeObject and readObject </li></ul><ul><li>private void writeObject (java.io.ObjectOutputStream s...
Introspection <ul><li>A mechanism that allows the builder tool to analyze a bean. </li></ul><ul><li>Two ways to analyze a ...
BDK Example <ul><li>package acme.beans; </li></ul><ul><li>import java.awt.*; </li></ul><ul><li>import java.io.Serializable...
Introspection... <ul><li>public void paint(Graphics g) { </li></ul><ul><li>g.setColor(beanColor); </li></ul><ul><li>g.setC...
Introspection... <ul><li>public void setColor(Color newColor) { </li></ul><ul><li>beanColor = newColor; </li></ul><ul><li>...
Introspection...
Customization <ul><li>Similar to Introspection. </li></ul><ul><li>Develop your own  <classname>BeanInfo   class which exte...
Customization ... <classname>BeanInfo <ul><li>package sun.beanbox.beans; </li></ul><ul><li>import java.beans.*; </li></ul>...
Customization... <ul><li>public PropertyDescriptor[] getPropertyDescriptors() { </li></ul><ul><li>try { </li></ul><ul><li>...
Customization... <classname>Editor <ul><li>package sun.beanbox.beans; </li></ul><ul><li>import java.beans.*; </li></ul><ul...
BDK Output
Conclusion <ul><li>Easy to use. </li></ul><ul><li>Difficult to build. </li></ul><ul><li>Like all OO design, needs careful ...
Support <ul><li>BDK -  Sun </li></ul><ul><li>NetBeans – www.netbeans.org </li></ul><ul><li>Jbuilder -  Inprise </li></ul><...
References <ul><li>http://java.sun.com/products/javabeans/ </li></ul><ul><li>Java Developer Connection Tutorials </li></ul...
References... <ul><li>Trail: JavaBeans </li></ul><ul><ul><li>http://java.sun.com/docs/books/tutorial/javabeans/index.html ...
Upcoming SlideShare
Loading in …5
×

Bean Intro

1,349 views
1,040 views

Published on

Published in: Technology, Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,349
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
79
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Bean Intro

  1. 1. Java Beans
  2. 2. Content <ul><li>What is a Java Bean? </li></ul><ul><li>BDK </li></ul><ul><li>What makes Bean possible? </li></ul><ul><li>Support for Java Bean </li></ul><ul><li>References </li></ul>
  3. 3. What are Java Beans? <ul><li>Software Component </li></ul><ul><ul><li>Specified interfaces. </li></ul></ul><ul><ul><li>Independent deployment. </li></ul></ul><ul><li>Analogy </li></ul><ul><ul><li>Electronic components. </li></ul></ul><ul><ul><li>Automotive components. </li></ul></ul>
  4. 4. What are Java Beans? ... <ul><li>Definition: </li></ul><ul><li>“ A Java bean is a reusable software component that can be visually manipulated in builder tools.” </li></ul><ul><li>Java Bean Tutorials </li></ul><ul><li>The JavaBeans Specs are available for d/l at http:// java.sun.com/products/javabeans/docs / </li></ul>
  5. 5. What is JavaBeans? <ul><li>A Java Bean is a reusable software component that can be manipulated visually in a builder tool </li></ul><ul><ul><li>JavaBeans is a portable, platform-independent component model written in the Java programming language, developed in collaboration with industry leaders. </li></ul></ul><ul><ul><li>It enables developers to write reusable components once and run them anywhere -- benefiting from the platform-independent power of Java technology. </li></ul></ul><ul><ul><li>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 </li></ul></ul>
  6. 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. 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. 8. Features of JavaBeans <ul><ul><li>Support for introspection </li></ul></ul><ul><ul><ul><li>so that a builder tool can analyze how a bean works </li></ul></ul></ul><ul><ul><li>Support for customization </li></ul></ul><ul><ul><ul><li>so that when using an application builder a user can customize the appearance and behavior of a bean </li></ul></ul></ul><ul><ul><li>Support for events </li></ul></ul><ul><ul><ul><li>as a simple communication metaphor than can be used to connect up beans </li></ul></ul></ul><ul><ul><li>Support for properties </li></ul></ul><ul><ul><ul><li>both for customization and for programmatic use </li></ul></ul></ul><ul><ul><li>Support for persistence </li></ul></ul><ul><ul><ul><li>so that a bean can be customized in an application builder and then have its customized state saved away and reloaded later </li></ul></ul></ul>
  9. 9. Persistent Storage <ul><li>Purpose: </li></ul><ul><ul><li>To use existing data formats and plug into OLE or OpenDoc documents (e.g., Excel doc inside a Word doc) </li></ul></ul><ul><ul><li>To be “trivial” for the common case of a tiny Bean (by saving its internal state) </li></ul></ul><ul><li>Solutions </li></ul><ul><ul><li>Externalization : provides a Bean with full control over the resulting data layout. </li></ul></ul><ul><ul><li>Serialization : provides an automatic way of storing out and restoring the internal state of a collection of Java objects </li></ul></ul><ul><li>All bean must support either Serialization or Externalization </li></ul>
  10. 10. Software Components <ul><li>Buttons </li></ul><ul><li>Text Fields </li></ul><ul><li>List Boxes </li></ul><ul><li>Scroll Bars </li></ul><ul><li>Dialogs </li></ul><ul><li>VBX, OLE </li></ul>
  11. 11. Visually Manipulated, Builder Tools Property Sheet Method Tracer BeanBox ToolBox
  12. 12. BDK...
  13. 13. Selecting the events
  14. 14. Attaching Events
  15. 15. Generating Adapter...
  16. 16. Making an Applet is a click away!
  17. 17. Applet Done!
  18. 18. What makes this possible? <ul><li>Properties </li></ul><ul><li>Events </li></ul><ul><li>Persistence </li></ul><ul><li>Introspection </li></ul><ul><li>Customization </li></ul>
  19. 19. Properties <ul><li>Attributes. </li></ul><ul><li>Can be read/write, read-only or write-only. </li></ul><ul><li>Several types of properties: </li></ul><ul><ul><li>Simple </li></ul></ul><ul><ul><li>Indexed </li></ul></ul><ul><ul><li>Bound </li></ul></ul><ul><ul><li>Constrained </li></ul></ul>
  20. 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. 21. Coding examples for Properties <ul><li>public class alden2 extends Canvas { </li></ul><ul><li>String ourString = “Hello”; </li></ul><ul><li>public alden2() { </li></ul><ul><li>setBackground (Color.red); </li></ul><ul><li>setForeground (Color.blue); </li></ul><ul><li>} </li></ul><ul><li>public void setString (String newString) { ourString = newString; } </li></ul><ul><li>public String getString() { return ourString; } </li></ul><ul><li>public Dimension getMinimunSize() { </li></ul><ul><li>return new Dimension (50, 50); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  22. 22. Bound Properties <ul><li>Generates notification when a property is changed. </li></ul><ul><li>public class propertDemo extends Canvas { </li></ul><ul><li>String ourString = “Hello”; </li></ul><ul><li>private PropertyChangeSupport changes = new PropertyChangeSupport(this); </li></ul><ul><li>... </li></ul><ul><li>public void setString (String newString) { </li></ul><ul><li>String oldString = ourString; </li></ul><ul><li>ourString = newString; </li></ul><ul><li>changes.firePropertyChange(“string”, oldString, newString); </li></ul><ul><li>} </li></ul><ul><li>... </li></ul>
  23. 23. Bound Properties... <ul><li>... </li></ul><ul><li>public void addPropertyChangeListener (PropertyChangeListener l) { </li></ul><ul><li>changes.addPropertyChangeListener (l); </li></ul><ul><li>} </li></ul><ul><li>public void removePropertyChangeListener (PropertyChangeListener l) { </li></ul><ul><li>changes.removePropertyChangeListener(l); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  24. 24. Constrained Property <ul><li>The concept of a Watcher object. </li></ul><ul><ul><li>subscribe to the ‘VetoableChange’ </li></ul></ul><ul><li>Can veto a property change using a PropertyVetoException. </li></ul>
  25. 25. Constrained Property... <ul><li>public class JellyBean extends Canvas { </li></ul><ul><li>private PropertyChangeSupport changes = new PropertyChangeSupport (this); </li></ul><ul><li>private VetoableChangeSupport vetos = new VetoableChangeSupport (this); </li></ul><ul><li>... </li></ul><ul><li>public void setColor (int newColor) throws PropertyVetoException { </li></ul><ul><li>int oldColor = currentColor; </li></ul><ul><li>vetos.fireVetoableChange(“setColor”, newInteger(oldColor), </li></ul><ul><li> newInteger(newColor)); </li></ul><ul><li>currentColor = newColor; </li></ul>
  26. 26. Constrained Property... <ul><li>changes.firePropertyChange(“setColor”, new Integer(oldColor), </li></ul><ul><li> new Integer(newColor)); </li></ul><ul><li>} </li></ul><ul><li>public void addVetoableChangeListener (VetoableChangeListener l) { </li></ul><ul><li>vetos.addVetoableChangeListener(l); </li></ul><ul><li>} </li></ul><ul><li>public void removeVetoableChangeListener(VetoableChangeListener l) { </li></ul><ul><li>vetos.removeVetoableChangeListener(l); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  27. 27. Events <ul><li>Two types of objects are involved: </li></ul><ul><ul><li>“ Source” objects. </li></ul></ul><ul><ul><li>“ Listener” objects. </li></ul></ul><ul><li>Based on registration. </li></ul><ul><li>Makes use of parametric polymorphism. </li></ul>
  28. 28. Events <ul><li>Message sent from one object to another. </li></ul><ul><li>Sender fires event, recipient (listener) handles the event </li></ul><ul><li>There may be many listeners. </li></ul>Event source Event listener Fire event Event Object Register listener
  29. 29. Event source Event Adapter Fire event Event Object Register event listener Event Listener Forward event Event Object Provide interface
  30. 30. Bean Events <ul><li>Define a new Event class which extends EventObject. XEvent </li></ul><ul><li>Define a new interface for listeners to implement, this must be an extension of EventListener. XEventListener </li></ul><ul><li>The Source must provide methods to allow listeners to register and unregister eg addXListener(), removeXListener(). </li></ul><ul><li>The source must provide code to generate the event and send it to all registered listeners. fireXEvent() </li></ul><ul><li>The listener must implement the interface to receive the event. changeX() </li></ul><ul><li>The listener must register with the source to receive the event. </li></ul>
  31. 31. Events <ul><li>public class eventSource extends GenericEventGenerator { </li></ul><ul><li>... </li></ul><ul><li>private Vector myListeners = new Vector(); </li></ul><ul><li>... </li></ul><ul><li>public synchronized void addMyEventListeners ( MyEventListener l) { </li></ul><ul><li>myListeners.addElement(l); </li></ul><ul><li>} </li></ul><ul><li>public synchronized void removeMyEventListeners ( MyEventListener l) { </li></ul><ul><li>myListeners.removeElement(l); </li></ul><ul><li>} </li></ul><ul><li>... </li></ul>
  32. 32. Events... <ul><li>private void fanoutEvents() { </li></ul><ul><li>Vector l; </li></ul><ul><li>synchronized (this) { </li></ul><ul><li>l = (Vector) myListener.clone(); </li></ul><ul><li>} </li></ul><ul><li>for (int i = 0; i < l.size(); i++) { </li></ul><ul><li> MyEventListener mel = (MyEventListener) l.elementAt(i); </li></ul><ul><li> mel.handleThisEvent (this); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  33. 33. Persistence <ul><li>Allows the graphical builder to recall the state of a bean. </li></ul><ul><li>public class Button implements java.io.Serializable { ... } </li></ul><ul><li>Selected property fields can bypass the serialization using keywords transient or static . </li></ul>
  34. 34. Persistence... <ul><li>writeObject and readObject </li></ul><ul><li>private void writeObject (java.io.ObjectOutputStream s) </li></ul><ul><li>throws java.io.IOException{} </li></ul><ul><li>private void readObject (java.io.ObjectInputStream s) </li></ul><ul><li> throws java.io.IOException, java.lang.ClassNotFoundException {} </li></ul><ul><li>Allows the customization of objects. </li></ul><ul><li>Appearance and behavior can be stored and recalled. </li></ul><ul><li>Don’t store references to other beans. </li></ul>
  35. 35. Introspection <ul><li>A mechanism that allows the builder tool to analyze a bean. </li></ul><ul><li>Two ways to analyze a bean: </li></ul><ul><ul><li>low-level reflection APIs. </li></ul></ul><ul><ul><li>vendor provided explicit information (Customization). </li></ul></ul><ul><li>Application builder will provide default BeanInfo class <classname>BeanInfo. </li></ul>
  36. 36. BDK Example <ul><li>package acme.beans; </li></ul><ul><li>import java.awt.*; </li></ul><ul><li>import java.io.Serializable; </li></ul><ul><li>public class Acme04Bean extends Canvas implements Serializable { </li></ul><ul><li>public Acme04Bean() { </li></ul><ul><li>resize(60,40); </li></ul><ul><li>this.label=&quot;Bean&quot;; </li></ul><ul><li>setFont(new Font(&quot;Dialog&quot;, Font.PLAIN, 12)); </li></ul><ul><li>} </li></ul>
  37. 37. Introspection... <ul><li>public void paint(Graphics g) { </li></ul><ul><li>g.setColor(beanColor); </li></ul><ul><li>g.setColor(Color.blue); </li></ul><ul><li>int width = size().width; </li></ul><ul><li>int height = size().height; </li></ul><ul><li>FontMetrics fm = g.getFontMetrics(); </li></ul><ul><li>g.drawString(label, (width - fm.stringWidth(label)) / 2, </li></ul><ul><li>(height + fm.getMaxAscent() - fm.getMaxDescent()) / 2); </li></ul><ul><li>} </li></ul><ul><li>public Color getColor() { </li></ul><ul><li>return beanColor; </li></ul><ul><li>} </li></ul>
  38. 38. Introspection... <ul><li>public void setColor(Color newColor) { </li></ul><ul><li>beanColor = newColor; </li></ul><ul><li>repaint(); </li></ul><ul><li>} </li></ul><ul><li>public String getLabel() { </li></ul><ul><li>return label; </li></ul><ul><li>} </li></ul><ul><li>public void setLabel(String newLabel) { </li></ul><ul><li>String oldLabel = label; </li></ul><ul><li>label = newLabel; </li></ul><ul><li>} </li></ul><ul><li>private Color beanColor = Color.cyan; </li></ul><ul><li>private String label; </li></ul><ul><li>} </li></ul>
  39. 39. Introspection...
  40. 40. Customization <ul><li>Similar to Introspection. </li></ul><ul><li>Develop your own <classname>BeanInfo class which extends SimpleBeanInfo . </li></ul><ul><li>Develop your own <classname>Editor class which extends PropertyEditorSupport to custom build your property editor. </li></ul>
  41. 41. Customization ... <classname>BeanInfo <ul><li>package sun.beanbox.beans; </li></ul><ul><li>import java.beans.*; </li></ul><ul><li>public class NervousText07BeanInfo extends SimpleBeanInfo { </li></ul><ul><li>private final static Class beanClass = </li></ul><ul><li>NervousText07.class; </li></ul><ul><li>public BeanDescriptor getBeanDescriptor() { </li></ul><ul><li>BeanDescriptor bd = new BeanDescriptor(beanClass); </li></ul><ul><li>bd.setDisplayName(&quot;Uneasy Text 07&quot;); </li></ul><ul><li>return bd; </li></ul><ul><li>} </li></ul>
  42. 42. Customization... <ul><li>public PropertyDescriptor[] getPropertyDescriptors() { </li></ul><ul><li>try { </li></ul><ul><li>PropertyDescriptor textPD = </li></ul><ul><li>new PropertyDescriptor(&quot;text&quot;, beanClass); </li></ul><ul><li>PropertyDescriptor rv[] = {textPD}; </li></ul><ul><li>textPD.setPropertyEditorClass(NervousText07TextPropertyEditor.class); </li></ul><ul><li>return rv; </li></ul><ul><li>} catch (IntrospectionException e) { </li></ul><ul><li>throw new Error(e.toString()); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  43. 43. Customization... <classname>Editor <ul><li>package sun.beanbox.beans; </li></ul><ul><li>import java.beans.*; </li></ul><ul><li>public class NervousText07TextPropertyEditor </li></ul><ul><li>extends PropertyEditorSupport { </li></ul><ul><li>public String[] getTags() { </li></ul><ul><li>String values[] = { </li></ul><ul><li>&quot;Nervous Text&quot;, </li></ul><ul><li>&quot;Anxious Text&quot;, </li></ul><ul><li>&quot;Funny Text&quot;, </li></ul><ul><li>&quot;Wobbly Text&quot;}; </li></ul><ul><li>return values; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  44. 44. BDK Output
  45. 45. Conclusion <ul><li>Easy to use. </li></ul><ul><li>Difficult to build. </li></ul><ul><li>Like all OO design, needs careful planning. </li></ul><ul><li>Similar to the String library in C++. </li></ul><ul><li>Wide selection of JavaBeans in the future? </li></ul>
  46. 46. Support <ul><li>BDK - Sun </li></ul><ul><li>NetBeans – www.netbeans.org </li></ul><ul><li>Jbuilder - Inprise </li></ul><ul><li>Super Mojo - Penumbra Software </li></ul><ul><li>Visual Age for Java - IBM </li></ul><ul><li>Visual Cafe - Symantec Corporation </li></ul><ul><li>JDeveloper Suite - Oracle </li></ul>
  47. 47. References <ul><li>http://java.sun.com/products/javabeans/ </li></ul><ul><li>Java Developer Connection Tutorials </li></ul><ul><ul><li>Java Beans, Part 1 to 4 </li></ul></ul><ul><ul><li>http://developer.java.sun.com/developer/onlineTraining/ </li></ul></ul>
  48. 48. References... <ul><li>Trail: JavaBeans </li></ul><ul><ul><li>http://java.sun.com/docs/books/tutorial/javabeans/index.html </li></ul></ul><ul><li>JavaBeans 1.01 Specification </li></ul><ul><ul><li>http://java.sun.com/beans/docs/spec.html </li></ul></ul>

×