V. JavaBeans A. component architectures ------------------------------------------ COMPONENT ARCHITECTURES: A NEW FIELD FOR LANGUAGE DESIGN def: a *software component* is Examples: button slider graph Note: components are like not objects Architecture must define way to: ------------------------------------------ What's the difference between components and class libraries? B. Java's component architecture 1. overview ------------------------------------------ JAVABEANS: COMPONENTS FOR JAVA Terms: a *bean* is a *bean instance* is Target: components that can be When are they used? at design-time, customize and produce bean instance at run-time bean instance performs its jobs ------------------------------------------ ------------------------------------------ ASPECTS OF THE BEAN ARCHITECTURE Events: beans can be sources or listeners Properties: like fields accessed through methods get, set changes can trigger events or be constrained GUI or Application builder: tool that composes beans (visually) Reflection and Introspection: ways for tools to find out about beans Customization: setting properties of a bean Container: code that glues beans together Persistence: saving customized and connected beans ------------------------------------------ 2. events ------------------------------------------ WHY EVENTS? Why can't we connect beans by method calls? Example: slider and button beans pressing the button should reset slider Why not: public void pressButton { slider.reset(); } ------------------------------------------ ------------------------------------------ EVENTS How events work: 0. "source" defines interface interface UserSleepsListener extends java.util.EventListener { void userSleeps(UserSleepsEvent e); } 1. listener object is created class MySleepListener implements UserSleepsListener { MySleepListener() { /* ... */ } void userSleeps(UserSleepsEvent e) { // do something ... } } UserSleepsListener myLstnr = new MySleepListener(); 2. listener registers with source src.addUserSleepsListener(myLstnr); 3. source is mutated src.looksLikeYawning(); 4. source's method notifies listeners 5. Notifcation leads to call: myLstnr.userSleeps(event); ------------------------------------------ ------------------------------------------ CLASSES TO SUPPORT ENVENTS java.util.EventObject events themselves java.beans.PropertyChangeSupport holds set of listeners (registration) can notify them (fire events) java.beans.PropertyEditorSupport java.beans.VetoableChangeSupport java.beans.EventSetDescriptor holds set of listeners ------------------------------------------ ------------------------------------------ SEMANTIC PROBLEMS WITH EVENTS Multicast: many listeners, all get notified Unicast: at most one listener What if the set of listeners changes while they are being notified? What if a listener throws an exception? What if listener itself generates an event? What if source holds a lock? ------------------------------------------ 3. properties ------------------------------------------ PROPERTIES Simple properties: Like fields, access through 2 methods public Color getBackground(); public void setBackground(Color c); Indexed properties: array-valued properties access through 4 methods public Color[] getSpectrum(); public void setSpectrum(Color[] c); public Color getSpectrum(int i); public void setSpectrum(int i, Color c); ------------------------------------------ ------------------------------------------ DISCOVERING PROPERTIES Application builder finds properties by: a. looking for BeanInfo class named BeanInfo b. and looking for methods named get set ------------------------------------------ How can it do this? ------------------------------------------ CUSTOMIZATION Application builder makes a "property sheet" containing the properties of a bean [------------------------] [ BackgroundColor: white ] [------------------------] ------------------------------------------ ------------------------------------------ BOUND PROPERTIES def: a *bindable property* is one that allows listeners to receive PropertyChangeEvents syntax: a settable property in a class that has methods: public void addPropertyChangeListener( PropertyChangeListener l) public void removePropertyChangeListener( PropertyChangeListener l) can be the target of a binding package java.beans; public interface PropertyChangeListener { void propertyChange( PropertyChageEvent e); } ------------------------------------------ ------------------------------------------ CONSTRAINED PROPERTIES def: A *constrainable property* is one that allows listeners to decide if a property change is permitted. syntax: a settable property in a class that has methods public void addVetoableChangeListener( VetoableChangeListener l) public void removeVetoableChangeListener( VetoableChangeListener l) ------------------------------------------ What should the VetoableChangeListener interface be? What should happen when one listener vetoes the change? C. summary How is JavaBeans like or unlike a programming language? Would it benefit from being more of a language?