1 / 36

JavaBeans*

JavaBeans*. ICS 123 Richard N. Taylor & Eric M. Dashofy UC Irvine http://www.isr.uci.edu/classes/ics123s02/ *with the usual thanks to David Rosenblum. What Is JavaBeans?. (One of) Sun Microsystems’s entry in the component sweepstakes

terra
Download Presentation

JavaBeans*

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. JavaBeans* ICS 123 Richard N. Taylor & Eric M. Dashofy UC Irvine http://www.isr.uci.edu/classes/ics123s02/ *with the usual thanks to David Rosenblum

  2. What Is JavaBeans? • (One of) Sun Microsystems’s entry in the component sweepstakes • Currently realized in Beans Development Kit (BDK) 1.1 spec (1999) • Component model for Java • Platform-independent • Language-dependent • Implemented as language extensions • Additional classes and interfaces in the Java Class Library • Basically a design pattern Based on standard naming conventions • The naming conventions allow beanboxes and other beans to dynamically discover a bean’s capabilities • But naming conventions have well-known problems • Detailed documentation is available at http://java.sun.com/products/javabeans/

  3. Three Levels of Use • Building applications from beans written by bean developers • Developing individual beans • Developing “beanboxes” and other tools for graphically manipulating beans • A more neutral term for such tools is “sandboxes”

  4. What Is a Bean? • A bean is a Java object • Exports properties, events and methods • Interface conforms to certain naming rules • Conforms to certain packaging rules • Usually (but not always) has a visual form for manipulation in beanboxes • Supported by package java.beans • Any Java class can be a bean • No predefined Bean class • Many AWT components are beans

  5. A Bare-Bones Bean public class Airplane extends Canvas { public Airplane() { setBackground(Color.white); // Plus other visual settings and setForeground(Color.blue); // other initialization } public Dimension getPreferredSize() { return new Dimension(50,50); // Or whatever is appropriate } public Dimension getMinimumSize() { return getPreferredSize(); // Or return an actual Dimension }} • Canvas is subclassed to make the bean a GUI object having its own window • Can subclass Component instead to create a “lightweight component” without its own window

  6. Elements of JavaBeans (I) • Events signal a bean’s response to changes in its properties and other phenomena • Properties represent attributes of a bean • BeanInfo customizes the information about a bean that is made available within a beanbox

  7. Elements of JavaBeans (II) • Customizers allow customization of a bean’s appearance and behavior in beanboxes • PropertyEditors define custom editors for a specific property • JAR files are archive files containing all the artifacts related to a bean • Manifest files describe the content of JAR files

  8. Events • Java events are used heavily in JavaBeans • An event in Java is an object • Events are instances of (subclasses of) java.util.EventObject • An event is sent by an object to a specific object • The identity of the sender is associated with the event • An event is sent as an argument in a method call • An object that generates events must keep track of its own listeners

  9. So What’s So Special About Java Events? • How do they differ from regular method calls? • They define a protocol for asynchronous interaction • They are generated by many predefined Java components • They are the primary mechanism for wiring interoperation paths between beans • But there’s still something missing ...

  10. Review:Implicit Invocation • Advantages Allows for decoupling and autonomy of components • Java events require tight coupling • Enhances reuse and evolution • Easy to introduce new components without affecting existing ones • Disadvantages • Components announcing events have no guarantee of getting a response • Components announcing events have no control over the order of responses • Event abstraction does not cleanly lend itself to data exchange • Difficult to reason about the behavior of an announcing component independently of the components that register for its events

  11. Listeners and Adapters • Listeners are interfaces • They define methods that an object must implement in order to receive event notifications • Listeners add and remove themselves (i.e., register and unregister) with the source of events they’re interested in • Event sources must provide methods to add and remove listeners • Adapters are classes • They are bare-bones implementations of listener interfaces • They provide a convenient way of quickly creating event listeners • They are usually subclassed to override a single listener method of interest

  12. Predefined Events in Java • ActionEvent: high-level user event • AdjustmentEvent: event from an Adjustable object (such as a scrollbar) • ComponentEvent: event from a GUI Component object • Container, Focus, Input (Key or Mouse), Paint, Window • ItemEvent: event from an ItemSelectable object • TextEvent: event from the editing of a TextComponent • Many of these have associated Adapters and/or Listeners

  13. Event Handling Scenario(involving ActionEvents) ActionEvent Listener ActionEventSource ActionEvent Listener addActionListener() actionPerformed(ae) addActionListener() actionPerformed(ae) actionPerformed(ae) removeActionListener() actionPerformed(ae)

  14. Action EventProgramming Example public class Button extends Component { // defined in java.awt public synchronized void addActionListener(ActionListener l) { ... } protected void processEvent(ActionEvent ae) { // called internally ... l.ActionPerformed(ae); }}public class ButtonListener implements ActionListener { Button button; public ButtonListener() { button = new Button("Press Me"); button.addActionListener(this); } public void ActionPerformed(ActionEvent buttonClick) { ... }}

  15. Properties • A property of a bean is a public attribute, accessed via Get and Set methods • Read-only properties have only a Get method • Write-only properties have only a Set method • Read/Write properties have both

  16. Kinds of Properties (I) • Simple properties: a single value P of some type T • public T getP(); • public void setP(T newP); • public boolean isP(); // same as getP when T is boolean • Indexed properties: an array of values • public T[] getP(); • public void setP(T[] newP); • public T getP(int index); • public void setP(int index, T newP); • Not supported in BDK 1.0 • How does a beanbox know that something is a simple or indexed property?

  17. Example Simple Properties public class Airplane extends Label { protected String serialNumber = "N173UA"; public String getSerialNumber() { return serialNumber; } public Airplane() { setText(getSerialNumber()); } protected int flightNumber = 0; public int getFlightNumber() { return flightNumber; } public void setFlightNumber(int n) { flightNumber = n; }} • FlightNumber is a read/write property • SerialNumber is a read-only property • Attributes storing the property values are made protected

  18. Kinds of Properties (II) • Bound properties: signal a PropertyChange event when changed • One event listener list for all bound properties • Supported by classes and interfaces in package java.beans • public class PropertyChangeEvent extends java.util.EventObject • public interface PropertyChangeListener extends EventListener • class PropertyChangeSupport implements Serializable • public void addPropertyChangeListener(PropertyChangeListener l); • public void removePropertyChangeListener(PropertyChangeListener l); • How does a beanbox know that something is a bound property?

  19. Example Bound Property import java.beans.* // Needed for PropertyChange stuff public class Airplane extends Label { protected int altitude = 0; protected PropertyChangeSupport changes = new PropertyChangeSupport(this); public int getAltitude() { return altitude; } public void setAltitude(int a) { int oldAltitude = altitude; altitude = a; changes.firePropertyChange("Altitude", new Integer(oldAltitude), new Integer(a)); } public void addPropertyChangeListener(PropertyChangeListener l) { changes.addPropertyChangeListener(l); } public void removePropertyChangeListener(PropertyChangeListener l) { changes.removePropertyChangeListener(l); }}

  20. Kinds of Properties (III) • Constrained properties: changes can be vetoed by other objects • One event listener list for all constrained properties • Supported by classes and interfaces in package java.beans • exception PropertyVetoException • public interface VetoableChangeListener extends EventListener • class VetoableChangeSupport implements Serializable • public void setP(T newP) throws PropertyVetoException; • public void addVetoableChangeListener(VetoableChangeListener l); • public void removeVetoableChangeListener(VetoableChangeListener l); • Usually also implemented as bound properties • Bound property listeners also get notified of changes to constrained properties • Only registered vetoers are given the opportunity to veto a change

  21. Example Constrained Property public class Airplane extends Label { protected int heading = 0; protected VetoableChangeSupport vetoes = new VetoableChangeSupport(this); public int getHeading() { return heading; } public void setHeading(int h) throws PropertyVetoException { int oldHeading = heading; vetoes.fireVetoableChange("Heading", new Integer(oldHeading), new Integer(h)); // ***** heading = h; changes.firePropertyChange("Heading", new Integer(oldHeading), new Integer(h)); } public void addVetoableChangeListener(VetoableChangeListener l) { vetoes.addVetoableChangeListener(l); } public void removeVetoableChangeListener(VetoableChangeListener l) { vetoes.removeVetoableChangeListener(l); }} • This is also a bound property (using the setup for Altitude) • PropertyVetoException would be raised within call at *****

  22. JAR Files • JAR archives contain files related to a bean • .class files • .ser files (serialized Beans) • .html files • image files • audio files • Manifest file • Manifest entries can include several fields, including an optional indication of whether or not a file is a bean • In the absence of a manifest file, all .class and .ser files are treated as beans

  23. Packaging the Airplane Bean • Create a MANIFEST file • Name: Airplane.classJava-Bean: TrueName: AirplaneBeanInfo.classJava-Bean: FalseName: Airplane.gif • Create the JAR file • jar cfm Airplane.jar MANIFEST Airplane.class \ AirplaneBeanInfo.class Airplane.gif • The jar command creates a file called META-INF/MANIFEST in the .jar file it creates, using the information contained in MANIFEST above

  24. BDK Directory Structure <root-directory> GNUmakefile beanbox docs jars … • Run beanbox from beanbox directory • run.sh in UNIX, run.bat in Windows • Beanbox looks in jars directory for .jar files • Look at GNUmakefile for ideas on how to automate creation of beans • Lots of documentation in docs directory

  25. The BDK Beanbox • A simple beanbox for graphically composing beans and testing bean interactions • Three windows • Beanbox: A canvas where beans are placed and wired together • Toolbox: A palette of available beans • Property sheet: Properties of the selected bean • The beanbox finds read-only and write-only properties for a bean but by default doesn’t display them in the property sheet for the bean

  26. The BDK Beanbox with the Airplane Bean Property Sheet Beanbox Toolbox

  27. Additional BDK Beanbox Expectations • Each bean class needs a zero-argument constructor method • The beanbox calls this method when you drop a bean in the beanbox • Helpful to define a package for all classes related to a bean

  28. Wiring Beans Together in the BDK Beanbox • Beanbox customizes Edit menu according to capabilities of currently selected bean • Wiring events • Event from generated bean passed as argument to method of target bean • Predefined events for the bean’s class • Component, container, key, mouse, focus, mouseMotion events • PropertyChange events • VetoableChange events • Any other events generated by the bean • Wiring bound properties • Change in bound property in one bean can be bound to a property in another bean

  29. Wiring Up Two Beans (I) • An Airplane bean and a Juggler bean • Airplane is currently selected

  30. Wiring Up Two Beans (II) • Select propertyChange event in Airplane bean

  31. Wiring Up Two Beans (III) • Wire the propertyChange event to the Juggler

  32. Wiring Up Two Beans (IV) • Have the juggler’s startJuggling method called upon receiving the Airplane’s propertyChange event

  33. Wiring Up Two Beans (V) • Juggler is now selected • Select mouseClicked event in Juggler bean

  34. Wiring Up Two Beans (VI) • Wire the Juggler’s mouseClicked event to itself

  35. Wiring Up Two Beans (VII) • Have the juggler’s stopJuggling method called upon receiving its own mouseClicked event

  36. Wiring Up Two Beans (VIII) • Everything is now wired up • Click on the Juggler to stop its juggling • Change the altitude of the airplane to start the Juggler juggling

More Related