Escolar Documentos
Profissional Documentos
Cultura Documentos
Introduction to JavaBeans
What's a Bean?
Beans Architecture
* Events
* Properties
* Methods
* EventObjects
AWTEvent in the AWT world.
* EventListeners
ActionListener, ItemListener, ...
* Event Sources (Beans)
The different AWT Components.
•Properties
•Methods
FileOutputStream fOut =
new FileOutputStream("test.out");
ObjectOutput out = new ObjectOutputStream(fOut);
out.writeObject(top);
out.flush();
out.close();
Events
* EventObject
* EventListener - (the sink)
* An Event Source (the Bean)
•EventObject
•EventListener
•Event Source
Without an event source, the HireEvent and HireListener are
virtually useless. The event source defines when and where an
event will happen. Classes register themselves as interested in
the event, and they receive notification when the event happens.
A series of methods patterns represents the registration process:
If you are using AWT components, AWT events already have this
behavior. Maintaining listeners is only necessary for new event
types, or adding listeners where they previously were not
(ActionEvent within a Canvas).
If you want to permit only one listener (unicast), you have the
addListenerType method throw the
java.util.TooManyListenersException exception when adding a
second listener (and you do not need a Vector). Also, remember
to synchronize the add/remove methods to avoid a race condition.
You have to call the method directly when the triggering event
happens.
----------------------------------------------------------------
-------
Properties
* Simple
* Indexed
* Bound
* Constrained
•Simple Properties
float salary;
public void setSalary (float newSalary) {
salary = newSalary;
}
public float getSalary () {
return salary;
}
boolean trained;
public void setTrained (boolean trained) {
this.trained = trained;
}
public boolean isTrained () {
return trained;
}
•Indexed Properties
•Bound Properties
•Constrained Properties
Constrained properties are similar to bound properties. In
addition to maintaining a list of PropertyChangeListeners, the
Bean maintains a list of VetoableChangeListeners. Then, prior to
the Bean changing a property value, it asks the
VetoableChangeListeners if its okay. If it isn't, the listener
throws a PropertyVetoException, which you declare the set
routine to throw.
And changes:
and
Methods
Besides all the methods required for each property and event of
a Bean, you usually create support methods which accept no
arguments or an argument of an event you listen for. That way, a
builder application can inspect a class quickly for a list of
the appropriate methods it can connect to. Non-public support
methods may also be available. However, public methods with
other parameter patterns are usually the result of a bad design
pattern, and not easily connectable from a builder application.
For instance, if you always pass along a time-stamp with an
event, you should probably subclass the event, add a time-stamp
attribute, and pass the subclass around. This results in a
simplified design and easier maintainability.
•Intro to Customization
Customization of Beans allows you as the Beans developer to
control what a Bean-integrator will see when they use a builder
tool. By default, a builder tool uses reflection to determine
what to display when designing programs with Beans. In most
cases, this is sufficient. However, there are times when you
want to provide different functionality. For instance, if you
want to provide your own customization interface, instead of
using the default property sheet, you can implement the
Customizer interface and provide a custom Panel. The OurButton
Bean in the Beans Development Kit (BDK) provides a Customizer.
•BeanInfo
•Introspection
•Events
EventSetDescriptor[] esd =
bi.getEventSetDescriptors();
for (int i=0;i<esd.length;i++)
System.out.print (esd[i].getName() + " ");
System.out.println ();
Properties
PropertyDescriptor pd[] =
bi.getPropertyDescriptors();
for (int i=0;i<pd.length;i++)
System.out.print (pd[i].getName() + " ");
System.out.println ();
For a TextField, this would print the names of all 155 methods
available, most of which are inherited from Component. This
includes the add/remove event listener methods, as they are
methods like the others.
•BeanInfo
import java.beans.*;
public class SizedTextFieldBeanInfo
extends SimpleBeanInfo {
private final static Class beanClass =
SizedTextField.class;
public PropertyDescriptor[]
getPropertyDescriptors() {
try {
PropertyDescriptor length =
new PropertyDescriptor("length", beanClass);
PropertyDescriptor rv[] = {length};
return rv;
} catch (IntrospectionException e) {
throw new Error(e.toString());
}
}
}
And, to wrap things up, you could even supply an icon for your
Bean with the help of an image file and the following added to
your BeanInfo definition:
Customizers
package employee;
import java.awt.*;
import java.awt.event.*;
import java.beans.*;
package employee;
import java.beans.*;
public class EmployeeBeanInfo extends SimpleBeanInfo {
public BeanDescriptor getBeanDescriptor() {
return new BeanDescriptor(
beanClass, customizerClass);
}
private final static Class beanClass =
Employee.class;
private final static Class customizerClass =
EmployeeCustomizer.class;
}
And, the property sheet for the Employee Bean might look like
this:
Datatype Editor
Boolean BoolEditor
Byte ByteEditor
Color ColorEditor
Double DoubleEditor
Float FloatEditor
Font FontEditor
Int IntEditor
Long LongEditor
Number NumberEditor
Short ShortEditor
String StringEditor
Persistence
•Bean Serialization
•Bean Reconstitution
Component c =
(Component)Beans.instantiate(
null, "java.awt.TextField");
import java.awt.*;
import java.io.*;
// serialize
try {
FileOutputStream f = new FileOutputStream(
"MyTextField.ser");
ObjectOutputStream s =
new ObjectOutputStream(f);
s.writeObject(mtf);
s.flush();
} catch (Exception e) {
System.out.println(e);
}
System.exit(0);
}
}
Component c =
(Component)Beans.instantiate(null, "MyTextField");
With MyTextField.ser
Without MyTextField.ser
•Bean Versioning
Then, when the versions are truly incompatible, you change the
SUID variable. When this does happen, you should keep track of
all the old values so you can properly handle the differences
(e.g., default setting for new instance variable). If you forget
to save the old setting, it is necessary to revert back to an
old version, regenerating the value, and moving forward again.