Docsity

JavaBeans: Concepts, Properties, Events, Persistence - CMSC 433, UMD, Fall 2001 - Prof. Al, Study notes of Programming Languages

Toggle sidebar
bg1
CMSC 433, Alan Sussman, U. Maryland 1
CMSC433, Fall 2001
JavaBeans, with examples
Alan Sussman
November 27, 2001
CMCS 433, Fall 2001 -Alan Sussman 2
Administrivia
•Exams returned today
–mean: 60 median: 62
25%: 53 75%: 68
–answers posted
•Project 5
–due Wednesday, 6PM
–same for Project 4 commentary
•Project 6 available soon
–JavaBeans tutorial link on Readings page
CMCS 433, Fall 2001 -Alan Sussman 3
Last time
•JavaBeans
–Software components in Java
–Event model
•same as for AWT and Swing GUI libraries
•listeners register with a bean to be notified of events
–Properties –get and set
•bound –other beans notified of a property change
•constrained –other beans can veto a property change
–Introspection
•use Java reflection to find out about a bean’s properties,
events, other methods
–Persistence
•all beans can be serialized and deserialized
CMCS 433, Fall 2001 -Alan Sussman 4
Simple Bean example
import java.awt.*;
import java.io.Serializable;
public class SimpleBean extends Canvas
implements Serializable {
// Constructor sets inherited properties
public SimpleBean(){
setSize(60,40);
setBackground(Color.red);
}
}
CMCS 433, Fall 2001 -Alan Sussman 5
Properties
•If a component supports functions:
–public void setMyValue(int v)
–public int getMyValue()
•It has a MyValue property of type int
•For boolean types, getter function can be named
is<Prop>()
•Can have read-only, read/write or write-only
properties
–don’t have to define both getter and setter method
CMCS 433, Fall 2001 -Alan Sussman 6
Example, with Simple Property
import java.awt.*; import java.io.Serializable;
public class SimpleBean extends Canvas
implements Serializable{
private Color color = Color.green;
// property getter method
public Color getColor(){
return color;
}
// property setter method. Sets new SimpleBean
// color and repaints.
public void setColor(Color newColor){
color = newColor;
repaint();
}
pf3
pf4
pf5

Partial preview of the text

Download JavaBeans: Concepts, Properties, Events, Persistence - CMSC 433, UMD, Fall 2001 - Prof. Al and more Study notes Programming Languages in PDF only on Docsity!

CMSC433, Fall 2001

JavaBeans, with examples

Alan Sussman

November 27, 2001

CMCS 433, Fall 2001 - Alan Sussman 2

Administrivia

• Exams returned today

  • mean: 60 median: 62
  • answers posted

• Project 5

  • due Wednesday, 6PM
  • same for Project 4 commentary

• Project 6 available soon

  • JavaBeans tutorial link on Readings page

CMCS 433, Fall 2001 - Alan Sussman 3

Last time

  • JavaBeans
    • Software components in Java
    • Event model
      • same as for AWT and Swing GUI libraries
      • listeners register with a bean to be notified of events
    • Properties – get and set
      • bound – other beans notified of a property change
      • constrained – other beans can veto a property change
    • Introspection
      • use Java reflection to find out about a bean’s properties, events, other methods
    • Persistence
      • all beans can be serialized and deserialized

CMCS 433, Fall 2001 - Alan Sussman 4

Simple Bean example

import java.awt.*; import java.io.Serializable; public class SimpleBean extends Canvas implements Serializable { // Constructor sets inherited properties public SimpleBean(){ setSize(60,40); setBackground(Color.red); } }

CMCS 433, Fall 2001 - Alan Sussman 5

Properties

  • If a component supports functions:
    • public void setMyValue(int v)
    • public int getMyValue()
  • It has a MyValue property of type int
  • For boolean types, getter function can be named

is()

  • Can have read-only, read/write or write-only

properties

  • don’t have to define both getter and setter method

CMCS 433, Fall 2001 - Alan Sussman 6

Example, with Simple Property

import java.awt.*; import java.io.Serializable; public class SimpleBean extends Canvas implements Serializable{ private Color color = Color.green; // property getter method public Color getColor(){ return color; } // property setter method. Sets new SimpleBean // color and repaints. public void setColor(Color newColor){ color = newColor; repaint(); }

CMCS 433, Fall 2001 - Alan Sussman 7

Simple Property, cont.

public void paint(Graphics g) { g.setColor(color); g.fillRect(20, 5, 20, 30); } // Constructor sets inherited properties public SimpleBean(){ setSize(60,40); setBackground(Color.red); } }

CMCS 433, Fall 2001 - Alan Sussman 8

Java Bean Event Patterns

• A Bean Event must extend

  • class java.util.EventObject {

public EventObject(Object src);

public Object getSource();

• Name should end in Event

  • e.g., tempChangeEvent

CMCS 433, Fall 2001 - Alan Sussman 9

Event Listeners

• must implement java.util.EventListener

  • just a marker interface

• have event-Listener methods

  • void ( e);
  • interface TempChangeListener {

void tempChanged(TempChangedEvent e);

CMCS 433, Fall 2001 - Alan Sussman 10

Event sources

• Event sources fire events

• Have methods to attach/detach Listeners

  • public void add(ListenerType ls);
  • public void remove(ListenerType ls);

CMCS 433, Fall 2001 - Alan Sussman 11

Event Adapters

• Easy to construct event adapters

  • For example, an adapter that receives

temperatureChanged events, and generates

temperatureIncreased and

temperatureDecreasedEvents

CMSC433, Fall 2001

JavaBeans, with examples

Alan Sussman

November 29, 2001

CMCS 433, Fall 2001 - Alan Sussman 19

Creating a Listener

// implement the PropertyChangeListener interface public class MyClass implements java.beans.PropertyChangeListener, java.io.Serializable { void propertyChange(PropertyChangeEvent evt) { // handle a property change event // e.g., call a setter method in the listener class } }

// and register the listener with the source Bean button.addPropertyChangeListener(aButtonListener); CMCS 433, Fall 2001 - Alan Sussman 20

Constrained Properties

• Source Bean contains one or more

constrained properties

  • should also usually be bound properties

• Listeners can veto property changes

  • before the actual property change occurs
  • implement VetoableChangeListener interface
  • Listener throws PropertyVetoException
  • set method throws …

CMCS 433, Fall 2001 - Alan Sussman 21

Constrained Properties

import java.beans.* public class Constrained … { // instantiate VetoableChangeSupport object private VetoableChangeSupport vetos = new VetoableChangeSupport(this); // methods to implement property change listener list public void addVetoableChangeListener( VetoableChangeListener l) { vetos.addVetoableChangeListener(l); } public void removeVetoableChangeListener( VetoableChangeListener l) { vetos.removeVetoableChangeListener(l); } CMCS 433, Fall 2001 - Alan Sussman 22

Constrained Properties, cont.

// modify property setter method to fire PropertyChangeEvent // including adding throws clause public void setPriceInCents(int newPriceInCents) throws PropertyVetoException { int oldPriceInCents = ourPriceInCents; // First tell the vetoers about the change. // If anyone objects, don't catch the exception // but just let it pass on to the caller. vetos.fireVetoableChange("priceInCents", new Integer(oldPriceInCents), new Integer(newPriceInCents)); // Noone vetoed, so go ahead and make the change. ourPriceInCents = newPriceInCents; changes.firePropertyChange("priceInCents", new Integer(oldPriceInCents), new Integer(newPriceInCents)); } }

CMCS 433, Fall 2001 - Alan Sussman 23

Constrained Properties, cont.

// this builds a PropertyChangeEvent object, and calls // vetoableChange(PropertyChangeEvent pce) on each registered // listener public void fireVetoableChange(String propertyName, Object oldValue, Object newValue) throws PropertyVetoException

CMCS 433, Fall 2001 - Alan Sussman 24

Creating a Listener

• Same as for PropertyChangeListener

  • listener Bean implements

VetoableChangeListener interface

  • with method

vetoableChange(PropertyEvent evt)

throws PropertyVetoException;

  • called by source Bean on each registered listener object, and exercises veto power by throwing the PropertyVetoException

CMCS 433, Fall 2001 - Alan Sussman 25

Serialization and Persistence

• Can manipulate Java Beans in a builder tool

• Doesn’t help if can’t distribute the beans

• Serialize the beans

  • Bean must implement java.io.Serializable or

java.io.Externalizable (to get complete control

over the serialization)

• Application loads beans from Serialized

form

CMCS 433, Fall 2001 - Alan Sussman 26

Default Serialization

  • Beans that implement Serializable must have a

no-argument constructor

  • to call when reconstituting the object
  • Don’t need to implement Serializable if already

implemented in a superclass

  • unless need to change the way it works
  • All fields except static and transient ones are

serialized

  • default serialization ignores those fields
  • transient also can mark an entire class as not serializable

CMCS 433, Fall 2001 - Alan Sussman 27

Selective Serialization

  • To override default serialization, implement

ReadObject() and/or WriteObject()

  • to exercise complete control over what gets serialized
  • to serialize objects default serialization can’t handle
  • to add data to serialization stream that is not a field in the object
  • if override WriteObject(), override ReadObject() too private void writeObject(java.io.ObjectOutputStream out) throws IOException; private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException;

CMCS 433, Fall 2001 - Alan Sussman 28

Example – Molecule Demo Bean

private void writeObject(java.io.ObjectOutputStream s) throws java.io.IOException { s.writeInt(ourVersion); // a static field s.writeObject(moleculeName); // a class field } private void readObject(java.io.ObjectInputStream s) throws java.lang.ClassNotFoundException, java.io.IOException { // Compensate for missing constructor reset(); if (s.readInt() != ourVersion) { throw new IOException("Molecule.readObject: version mismatch"); } moleculeName = (String) s.readObject(); }

CMCS 433, Fall 2001 - Alan Sussman 29

Default Read/Write Object

private void writeObject(java.io.ObjectOutputStream s) throws java.io.IOException { //First write out defaults s.defaultWriteObject(); //... } private void readObject(java.io.ObjectInputStream s) throws java.lang.ClassNotFoundException, java.io.IOException { //First read in defaults s.defaultReadObject(); //... }

CMCS 433, Fall 2001 - Alan Sussman 30

Externalizable interface

• To get complete control over Bean’s

serialization

  • e.g., for writing/reading a specific file format
  • implement readExternal() and

writeExternal()

  • these classes also require a no-argument

constructor

CMCS 433, Fall 2001 - Alan Sussman 37

Example, finished

public BeanDescriptor getBeanDescriptor() { return new BeanDescriptor(beanClass); } ... private final static Class beanClass = ExplicitButton.class; }

CMCS 433, Fall 2001 - Alan Sussman 38

Controlling exposed features

• Base class features not exposed

  • use BeanInfo.getAdditionalBeanInfo

• Properties, events, methods without

descriptors not exposed

• Low-level reflection used for features with

getter methods returning null