98-09-281 JavaBeans introduction Klaus-Peter Heidrich for the University of Karlstad Inst. for...
-
date post
20-Dec-2015 -
Category
Documents
-
view
214 -
download
0
Transcript of 98-09-281 JavaBeans introduction Klaus-Peter Heidrich for the University of Karlstad Inst. for...
98-09-28 1
JavaBeans introductionJavaBeans introduction
Klaus-Peter Heidrich
for the University of Karlstad
Inst. for Information Technology
Dept. of Computer Science
98-09-28 Computer Science, University of
Karlstad2
IntroductionIntroduction
What is a Java Bean? „A Java Bean is a reusable software component
that can be visually manipulated in builder tools.“
Java Beans are Java classes that conform to the Java Beans specification, can be visible (button, slider) or invisible (network component)
98-09-28 Computer Science, University of
Karlstad4
MotivationMotivation
Build an application out of prebuild parts like in industrial production of for example cars or computers
Cross platform component model Interoperability (ActiveX, OpenDoc...) Constructing instead of coding Visual building of applications
98-09-28 Computer Science, University of
Karlstad7
How is it done?How is it done?
A Bean is delivered in binary form It exposes its attributes (methods, instance
variables, properties, and interfaces) through a default mechanism included in the Java API or explicit Information Classes
Bridges translate the attributes to other Component Models
98-09-28 Computer Science, University of
Karlstad8
Basic ConceptsBasic Concepts
A Java Bean supports Introspection Customization Properties Events Persistence
98-09-28 Computer Science, University of
Karlstad9
Requirements for a BeanRequirements for a Bean
A Bean is represented by one ore more Java classes which follow the Java Beans specifications, it must not inherit from any special class
A JavaBean must Have a default Constructor without no arguments be Customizable allowing access to its internal state be Serializable, must be able to save its state in a stream
It should be Packaged in JAR-File
all Classes, Pictures, Documentation... in one file provide BeanInfo and Customizer Classes
98-09-28 Computer Science, University of
Karlstad10
Design vs. RuntimeDesign vs. Runtime
A Java Bean has two lives: In the design environment, where it should
provide design information to the application builder to let the user customize its behaviour and appearance
In the application to do the task it was build for
98-09-28 Computer Science, University of
Karlstad11
Features of a Java BeanFeatures of a Java Bean
A Java Bean has certain attributes Properties
To represent the internal state Methods
To be called from outside Events
To inform about changes
98-09-28 Computer Science, University of
Karlstad12
The Bean Development KitThe Bean Development Kit
Available for free at http://www.javasoft.com/products/javabeans/software/
Reference implementation of a builder application to test own Java Beans
Allows customization of Beans and simple linking of Beans with adapters
98-09-28 Computer Science, University of
Karlstad14
ExampleExample•ProgressBean
•TimerBean
•ButtonBean
The TimerBean calls the ProgressBar in an editable intervall after being started by the Button.
98-09-28 Computer Science, University of
Karlstad15
Class DiagramClass Diagram
startstoprunaddTimerListenerremoveTimerListenergetTimeOutsetTimeOut
timeOutlisteners
TimerBean
Thread Component
getValuesetValuegetMaxsetMaxaddPropertyChangeListenerremovePropertyChangeListenerprogress
maxvaluelisteners
ProgressBar
addActionListenerremoveActionListener...
Listeners...
Button
98-09-28 Computer Science, University of
Karlstad16
Example Interaction DiagramExample Interaction Diagram
Button TimerBean ProgressBarAdapter1
actionperfomedstart
Adapter2
timedOutprogress
timedOutprogress
98-09-28 Computer Science, University of
Karlstad17
PropertiesProperties
•represent the internal state of a component
•control behaviour or visual representation
•can be read or written•the process of setting up properties at design- or runtime is called Customization
98-09-28 Computer Science, University of
Karlstad18
PropertiesProperties
Properties can be: Simple
(simple Java type or Class) Indexed
(an array of the above) Bound
an event is created when the property changes to inform the attached Listeners
Constrainedlike Bound, but the Listeners can revert the change
98-09-28 Computer Science, University of
Karlstad19
Property ExampleProperty Example
A Property is defined by two accessor methods (setter and getter) allowing other classes or the Builder to manipulate their state
public void setTimeout (int t) {
timeout = t;
}
public int getTimeout () {
return timeout;
}
98-09-28 Computer Science, University of
Karlstad20
Bound Properties ExampleBound Properties Example
protected PropertyChangeSupport listeners =
new PropertyChangeSupport(this);
public void addPropertyChangeListener (PropertyChangeListener l) {
listeners.addPropertyChangeListener(l);
}
public void removePropertyChangeListener (PropertyChangeListener l) {
listeners.removePropertyChangeListener(l);
}
public synchronized void setValue(int v) {
Integer oldValue = new Integer(value);
value = v;
repaint();
listeners.firePropertyChange("value", oldValue, new Integer(value));
}
98-09-28 Computer Science, University of
Karlstad21
EventsEvents
Mechanism to plug beans together Event is a notification between source and
one or more listeners Listener registers itself in being interested
in events Beans may be plugged together by adapters
generated by builder tool
98-09-28 Computer Science, University of
Karlstad22
TimerEventTimerEvent
public class TimerEvent extends EventObject {
long time;
public TimerEvent(Object source, long time) {
super(source);
this.time = time;
}
public long getTime() {
return time;
}
}
public interface TimerListener extends EventListener {
public abstract void timedOut(TimerEvent te);
}
98-09-28 Computer Science, University of
Karlstad23
Handling ListenersHandling Listeners
protected Vector listeners = new Vector ();
public void addTimerListener (TimerListener listener) {
listeners.addElement (listener);
}
public void removeTimerListener (TimerListener listener) {
listeners.removeElement (listener);
}
protected void fireTimeOut() {
TimerEvent event = new TimerEvent (this, System.currentTimeMillis());
Vector listeners = (Vector) this.listeners.clone ();
for (int i = 0; i < listeners.size (); ++ i)
((TimerListener) listeners.elementAt (i)).timedOut (event);
}