Builder tools e.g JBuilder, Bean Builder know a bean's features by looking at its public methods, properties and events called introspection implemented using Java's reflection cla
Trang 1Môn Lập trình Java NC
Chương 5: Java Beans
Trang 2 Introduction
The Bean-Writing process
Using Bean to build an Application
Naming Patterns for Bean Properties and Events
Bean Property Types
Adding Custom Bean Events
Property Editors
Going Beyond Naming Patterns
Customizers
Trang 3 a self-contained, reusable software unit
it can be visually composed with other beans to
make bigger components, applets, applications, etc
Trang 41 Introduction (cont.)
A bean is designed to expose its features:
e.g public methods, properties (data), events
Exposure allows beans to be used inside visual
building tools (e.g JBuilder)
Exposure is based on coding rules that a programmer must follow when writing a bean
these rules are mostly standard naming
conventions for methods
Trang 5Object Oriented Programming 2 - Chapter 6: Java Beans
1 Introduction (cont.)
Beans are persistent
their data is automatically stored when the program using them finishes
the data is restored when the program next starts
persistence is implemented in JavaBeans by using Java's object serialization features
Trang 61 Introduction (cont.)
Simple Java object must support the following five
features for it to become a JavaBean:
Trang 82 The Bean-Writing process
How Builder Tools Use Beans?
Builder tools (e.g JBuilder, Bean Builder) know a
bean's features by looking at its public methods, properties and events
called introspection
implemented using Java's reflection classes
A builder can also examine a bean's information object (BeanInfo), if the programmer has included one
Trang 9Object Oriented Programming 2 - Chapter 6: Java Beans
2 The Bean-Writing process (cont.)
How Builder Tools Use Beans?
A builder will have visual support for setting/reading a bean's properties, and for accessing its methods
A builder will be able to 'link' beans based on events
a source bean can send an event to a listener bean
Trang 102 The Bean-Writing process (cont.)
Using Java's Bean Builder
Bean Builder Website:
https://bean-builder.dev.java.net/
there's a "readme" link, and a good tutorial
Download Bean Builder v.1.0 beta
from the "Documents and Files" link under "Project Tools"
also at our Website on fivedots
Trang 11Object Oriented Programming 2 - Chapter 6: Java Beans
2 The Bean-Writing process (cont.)
JavaBeans – Development Phases:
Involves execution of the container application in
which the JavaBean is placed
Trang 122 The Bean-Writing process (cont.)
JavaBeans – Development Phases (cont.):
Construction Phase involves creation of the following:
class files
mft file
jar file
Trang 13Object Oriented Programming 2 - Chapter 6: Java Beans
2 The Bean-Writing process (cont.)
JavaBeans – Development Phases (cont.):
The Manifest file:
Is used by the target application to recognize a
JavaBean
Is saved with a mft extension
Contains a list of all class files that make up a
JavaBean
Trang 142 The Bean-Writing process (cont.)
JavaBeans – Development Phases (cont.):
The JAR file:
Is similar to a zip file
Is used to package a JavaBean for distribution
Contains the manifest file and all other files such as the class files and picture files of the bean
Has a jar extension
Trang 15Object Oriented Programming 2 - Chapter 6: Java Beans
3 Using Bean to build an
Application
Packaging Beans in JAR Files
Composing Beans in a Builder Environment
Trang 163 Using Bean to build an
Application (cont.)
From Package to JAR
A JAR file must contain a manifest
it lists the contents of the JAR file
a programmer writes the manifest in the
manifest.tmp file
When the JAR file is being created, the jar utility
converts manifest.tmp into a MANIFEST.MF file stored
in the META_INF directory of the JAR file
Trang 17Object Oriented Programming 2 - Chapter 6: Java Beans
3 Using Bean to build an
Trang 183 Using Bean to build an
Application (cont.)
There may be many classes (and other resources) in
a JAR file
each has its own Name and Java-Bean line
Not all the resources in a JAR file need to be beans; if
a class is a bean then the Java-Bean line is set to true
(otherwise false)
beans will appear in the Toolbox window
Trang 19Object Oriented Programming 2 - Chapter 6: Java Beans
3 Using Bean to build an
Application (cont.)
Create the JAR file
c:> jar cfm LogoAnimator.jar manifest.tmp
myPackage\*.*
Options
c - create a JAR file
f - indicates that next argument is a filename
m - next argument is a manifest.tmp file
used to create MANIFEST.MF
Trang 203 Using Bean to build an
Application (cont.)
List the JAR file
To confirm files were stored properly:
Trang 21Object Oriented Programming 2 - Chapter 6: Java Beans
3 Using Bean to build an
Application (cont.)
Run the JAR file
c:> java -jar LogoAnimator.jar
run the application in the bean
java uses the manifest file to execute main() of
Main-Class
Or give java the name of the class to execute:
c:> java -cp myJar.jar
myPackage.myClass
Trang 223 Using Bean to build an
Application (cont.)
Adding Bean to the Bean Builder
Load the JAR file into Bean Builder from the "File/Load Jar File" menu item.
Then select it in the Design Mode window, then click
in the design panel
Trang 23Object Oriented Programming 2 - Chapter 6: Java Beans
4 Naming Patterns for Bean
Properties and Events
Trang 245 Bean Property Types
JavaBeans have 4 kinds of properties, which are
supported in different ways:
simple properties
indexed properties
bound properties
constrained properties
Trang 25Object Oriented Programming 2 - Chapter 6: Java Beans
5 Bean Property Types (cont.)
A Simple Property = two methods
A read/write bean property is implemented as a
set/get method pair:
public void setPropertyName(
DataType value)
public DataType getPropertyName()
If the property is boolean, use isPropertyName()
instead of getPropertyName()
Trang 265 Bean Property Types (cont.)
Example
A fileName property (a String) is represented by:
public String getFileName();
public void setFileName(String fn);
A running property (a boolean) is:
public boolean isRunning();
public void setRunning(boolean b);
Trang 27Object Oriented Programming 2 - Chapter 6: Java Beans
5 Bean Property Types (cont.)
Indexed Properties
An indexed property is one that gets or sets an array
The programmer must supply two pairs of get and set methods:
one pair for setting/getting the entire array
one pair for setting/getting an array element
Trang 285 Bean Property Types (cont.)
Trang 29Object Oriented Programming 2 - Chapter 6: Java Beans
5 Bean Property Types (cont.)
Trang 305 Bean Property Types (cont.)
Trang 31Object Oriented Programming 2 - Chapter 6: Java Beans
5 Bean Property Types (cont.)
When a change occurs in the bean, an event listener will be called
it must notify the PropertyChangeListeners by
calling firePropertyChange()
bean
<listener_method>() {
firePropertyChange( );
}
Property Change Listeners
listening objects
change
events
Trang 325 Bean Property Types (cont.)
Example SliderFieldPanel Bean
A JSlider and a JTextField
A change to either should affect the other, and any
listener objects should be notified
Trang 33Object Oriented Programming 2 - Chapter 6: Java Beans
5 Bean Property Types (cont.)
Listening Beans Format
Beans that are registered listeners for property
changes must implement the PropertyChangeListener interface
The interface has one method:
void propertyChange(PropertyChangeEvent e)
it is called when the property value changes, and ewill contain the old and new values
Trang 345 Bean Property Types (cont.)
Listening Beans Format (cont.)
class Listener implements
PropertyChangeListener {
void propertyChange(PropertyChangeEvent ev) {
Object newVal = ev.getNewValue();
Object oldVal = ev.getOldValue();
}
Trang 35Object Oriented Programming 2 - Chapter 6: Java Beans
5 Bean Property Types (cont.)
Trang 365 Bean Property Types (cont.)
Example
0
from IntText bean
10
to IntText bean
range bean GUI
0
from
veto if from > to
range bean IntText bean change events
Trang 37Object Oriented Programming 2 - Chapter 6: Java Beans
6 Updating Steps for a Bean
1 Notify all change listeners of the wish to
change the property value
2 If none of the listeners throws an exception,
then
3 Notify all the listeners that the change has now been carried out
Trang 386 Updating in Code
// method in bean with property
public void setValue(int v)
{
Integer newVal = new Integer(v);
Integer oldVal = new Integer(v-1);
try {
vetoSupport.fireVetoableChange(
"value", oldVal, newVal);
// if here then no exception thrown
Trang 39Object Oriented Programming 2 - Chapter 6: Java Beans
7 Notes
Constrained properties use bound property techniques
to do the final update e.g
changeSupport.firePropertyChange( )
vetoSupport is used in a similar way to changeSupport:
vetoable listeners must be registered with it via
public add/remove methods
Trang 408 Vetoable Listener Code
Beans that are registered vetoable listeners for
property changes must implement the VetoableChangeLi stener interface
The interface has one method:
void vetoableChange
(PropertyChangeEvent e)
it is called when the property value wishes to
change, and e will contain the old and new values
Trang 41Object Oriented Programming 2 - Chapter 6: Java Beans
class Listener implements
VetoableChangeListener {
void
vetoableChange(PropertyChangeEvent ev) {
Object newVal = ev.getNewValue(); Object oldVal = ev.getOldValue();
if ( /* some test */ ) throw new PropertyVetoException(
Trang 429 Bean Information
A builder uses introspection to create lists of all
the methods, events, and properties in a bean
This can be confusing, especially if the bean
inherits a class with many properties
e.g LogoAnimator inherits JPanel
Trang 43Object Oriented Programming 2 - Chapter 6: Java Beans
It is possible to specify which
methods/events/properties will be visible for a bean inside the builder
The bean must include an extra class which
implements the BeanInfo interface
it lists the visible attributes of the bean
9 Bean Information (cont.)
Trang 44 Use a BeanInfo class to restrict the display to 4
properties and 1 event
Trang 46Any Bound Property?
The builder must be told about any bound properties
in the bean:
currentValue.setBound(true);
Trang 47Object Oriented Programming 2 - Chapter 6: Java Beans
Introspection Exceptions
Both getPropertyDescriptors() and
getEventSetDescriptors()must have try/catch blocks which can catch IntrospectionExceptions
these occur if the builder detects an error while examining the property or event information at run time
Trang 48Specifying an Event
Each EventSetDescriptor object has 4 values:
event source class, event set name, event
listener interface class, listener method name
e.g EventSetDescriptor changed =
new EventSetDescriptor(
beanClass, "propertyChange", java.beans.PropertyChangeListener.class,
"propertyChange");
Trang 49Object Oriented Programming 2 - Chapter 6: Java Beans
Other Descriptor Classes
Trang 50Using Descriptor Objects
All the descriptor objects of a given kind (e.g
PropertyDescriptor objects) must be collected into
an array and be returned by the get method for th
eir class
e.g getPropertyDescriptors(),
getEventSetDescriptors()
Trang 51 a Customizer class allows several properties to
be edited at the same time
The Bean context
the environment in which the bean is running
it can be examined at run time to find other
beans, so that beans can communicate
Trang 52JavaBeans Persistence
JavaBeans persistence uses JavaBeans
properties to save beans to a stream and to read
them back at a later time or in a different virtual
Trang 54Example (cont.)
Trang 55Object Oriented Programming 2 - Chapter 6: Java Beans
Example (cont.)
To save an object to a stream, use an XMLEncoder:
XMLEncoder out = new XMLEncoder(new
FileOutputStream( .));
out.writeObject(frame);
out.close();
To read it back, use an XMLDecoder:
XMLDecoder in = new XMLDecoder(new
FileInputStream( .));
JFrame newFrame = (JFrame) in.readObject();
in.close();