Download Web Technologies – Lecture Notes Unit-4

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
1
Web Technologies
B.Tech. IT III Year II Semester
UNIT IV
Java Beans
By:
G Indira Priyadarshini
IT DEPT, VJIT
 2000 Prentice Hall, Inc. All rights reserved.
Outline of Presentation
•
•
•
•
•
•
•
Introduction
Java Bean API
Properties
Introspection Using Design Patterns
BDK
Example (Animation Bean)
Introspection using BeanInfo Interface
 2000 Prentice Hall, Inc. All rights reserved.
Definition
• Java Bean is a reusable software component that
can be manipulated visually in a ‘builder tool’.
(from Java Bean Specification)
• The JavaBeans API provides a framework for
defining reusable, embeddable, modular software
components.
 2000 Prentice Hall, Inc. All rights reserved.
Introduction
• JavaBeans (beans)
– Reusable software component model
– Assemble predefined components
• Create powerful applications and applets
– Graphical programming and design environments
• Builder tools
• Support beans, reuse and integrate components
– Component assembler
• Programmer who use defined components
– Work on design of GUI and functionality
• Do not need to know implementation
– Just need to know services
The JavaBeans API
• Features implemented as extensions to standard
Java Class Library
• Main Component Services
–
–
–
–
–
–
GUI merging
Persistence
Event Handling
Introspection
Application Builder Support
Security issues
 2000 Prentice Hall, Inc. All rights reserved.
User Interface Merging
• Containers usually have Menus and/or toolbars
• Allows components to add features to the menus
and/or toolbars
• Define mechanism for interface layout between
components and containers
 2000 Prentice Hall, Inc. All rights reserved.
Persistence
• Components can be stored and retrieved
• Default – inherit serialization
• Can define more complex solutions based on
needs of the components
 2000 Prentice Hall, Inc. All rights reserved.
Event Handling
• Defines how components interact
• Java AWT event model serves as basis for the
event handling API’s
• Provides a consistent way for components to
interact with each other
 2000 Prentice Hall, Inc. All rights reserved.
Introspection
• Defines techniques so components can expose
internal structure at design time
• Allows development tools to query a component
to determine member variables, methods, and
interfaces
Two Approaches of Introspection are
1. Standard naming patterns (Design Patterns)
2. Using BeanInfo Interface
 2000 Prentice Hall, Inc. All rights reserved.
Application Builder Support
• Provides support for manipulating and editing
components at design time
• Used by tools to provide layout and customizing
during design
• Should be separate from component
• Not needed at run time
 2000 Prentice Hall, Inc. All rights reserved.
Security Issues
• JavaBeans are sbject to the standard Java security
model
• The security model has neither extended nor
relaxed.
• If a bean runs as an untrusted applet then it will be
subject to applet security
• If a bean runs as a stand-alone application then it
will be treated as a normal Java application.
 2000 Prentice Hall, Inc. All rights reserved.
Properties
• A builder tool can:
–
–
–
–
–
discover a bean’s properties
determine the properties’ read/write attribute
locate an appropriate “property editor” for each type
display the properties (in a sheet)
alter the properties at design-time
 2000 Prentice Hall, Inc. All rights reserved.
Types of Properties
• Simple : single value property
• Index: multiple-value properties
• Bound: provide event notification when value
changes
• Constrained: how proposed changes can be
okayed or vetoed by other object
 2000 Prentice Hall, Inc. All rights reserved.
Properties “Introspection” using design patterns
• When a builder tool introspect your bean it
discovers two methods: getter and setter methods.
• Design Patterns for Properties are
– public T getN() or public T[ ] getN()
– Public T getN(int index)
– public void setN(T value) or public void setN(T value[ ])
– public void setN(int index,T value)
T – Type N- name of the property
Eg : public Color getColor()
public void setColor(Color c)
 2000 Prentice Hall, Inc. All rights reserved.
Simple Properties….
• Adding a Color property
– Create and initialize a private instance variable
• private Color color = Color.blue;
– Write public getter & setter methods
• public Color getColor() {
– return color;
• }
• public void setColor(Color c) {
– color = c;
– repaint();
• }
 2000 Prentice Hall, Inc. All rights reserved.
Events “Introspection” using design patterns
• Source Bean fires events at the listeners using
method of those interfaces.
• Example: if a source Bean register ActionListsener
objects, it will fire events at those objects by
calling the actionPerformed method on those
listeners
Design patterns for Events :
Events can be identified by the following patterns
public void addTListener(Tlistener eventListener);
public void addTListener(Tlistener eventListener) throws
TooManyListeners;
public void removeTListener(Tlistener eventListener); T -Type
 2000 Prentice Hall, Inc. All rights reserved.
Methods “Introspection” using design patterns
• Design Patterns are not used for naming nonproperty methods.
• The Introspection Mechanism finds all of the
public methods of a bean
 2000 Prentice Hall, Inc. All rights reserved.
BeanInfo interface
• Question: how does a Bean exposes its features in
a property sheet?
• Answer: using java.beans.Introspector class
(which uses Core Reflection API)
• The discovery process is named “introspection”
• OR you can associate a class that implements the
BeanInfo with your bean
 2000 Prentice Hall, Inc. All rights reserved.
BeanInfo interface….
• Why use BeanInfo then?
• Using BeanInfo you can:
– Expose features that you want to expose
 2000 Prentice Hall, Inc. All rights reserved.
Bean Customization
• The appearance and behavior of a bean can be
customized at design time.
• Two ways to customize a bean:
– using a property editor
• each bean property has its own editor
• a bean’s property is displayed in a property sheet
– using customizers
• gives you complete GUI control over bean customization
• used when property editors are not practical
 2000 Prentice Hall, Inc. All rights reserved.
Property Editors
• If you provide a custom property editor class, then
you must refer to this class by calling
PropertyDescriptor.setPropertyEditorClass in a
BeanInfo class.
• Each bean may have a BeanInfo class which
customizes how the bean is to appear.
SimpleBeanInfo implements that interface
 2000 Prentice Hall, Inc. All rights reserved.
Beans Development Kit (BDK)
• To start the BeanBox:
– run.bat (Windows)
– run.sh (Unix)
 2000 Prentice Hall, Inc. All rights reserved.
BDK
• ToolBox contains the beans available
• BeanBox window is the form where you visually
wire beans together.
• Properties sheet: displays the properties for the
Bean currently selected within the BeanBox
window.
 2000 Prentice Hall, Inc. All rights reserved.
24
Developing a Simple Bean Using the BDK
1.
2.
3.
4.
5.
6.
7.
Create a directory for the new Bean
Create java source file(s)
Compile the source file(s)
Create manifest file
Create the jar file
Start the BDK
test
 2000 Prentice Hall, Inc. All rights reserved.
MyFirstBean
• import java.awt.*;
• import java.io.Serializable;
• public class FirstBean extends Canvas implements
Serializable {
•
public FirstBean() {
•
setSize(50,30);
•
setBackground(Color.blue);
•
}
• }
 2000 Prentice Hall, Inc. All rights reserved.
First Bean
• Compile: javac FirstBean.java
• Create a manifest file:
• manifest.txt
– Name: FirstBean.class
– Java-Bean: True
• Create a jar file:
• jar cfm FirstBean.jar mani.txt FirstBean.class
 2000 Prentice Hall, Inc. All rights reserved.
Example (Animation bean)
• Example of bean concept
– Have animation bean
• Want two buttons, start and stop
– With beans, can "hook up" buttons to startAnimation
and stopAnimation methods
• When pressed, method called
• Builder tool does work
– Use previously defined, reusable components
– Little or no code must be written
• Component assembler can "connect the dots”
 2000 Prentice Hall, Inc. All rights reserved.
Example (Animation bean)
• BeanBox installation
– Free utility from JavaBeans Development Kit (BDK)
http://java.sun.com/beans/software/index.html
• Windows, Solaris, and platform independent versions
• In Windows version, minor bug
– Do not install in directory with spaces in name
• To run, go to install directory, beanbox subdirectory, load
run.bat (or run.sh)
• BeanBox test container for JavaBeans
– Preview how bean will be displayed
– Not meant to be robust development tool
Example (Animation bean)
• Use screen captures from Windows
– Start application, following appears:
ToolBox has 16 sample
JavaBeans
Properties
customizes selected
bean.
Method Tracer displays
debugging messages (not
discussed)
BeanBox window tests beans.
Background currently selected
(dashed box).
Example (Animation bean)
– Initially, background selected
• Customize in Properties box
Example (Animation bean)
– Now, add JavaBean in BeanBox window
• Click ExplicitButton bean in ToolBox window
– Functions as a JButton
• Click crosshair where center of button should appear
• Change label to "Start the Animation"
Example (Animation bean)
– Select button (if not selected) and move to corner
• Position mouse on edges, move cursor appears
• Drag to new location
– Resize button
• Put mouse in corner, resize cursor
• Drag mouse to change size
Example (Animation bean)
– Add another button (same steps)
• "Stop the Animation"
– Add animation bean
• In ToolBox, select Juggler and add to BeanBox
• Animation begins immediately
Properties for juggler.
Example (Animation bean)
– Now, "hook up" events from buttons
• Start and stop animation
– Edit menu
• Access to events from beans that are an event source (bean can
notify listener)
• Swing GUI components are beans
• Select "Stop the Animation"
• Edit->Events->button push -> actionPerformed
Example (Animation bean)
– Line appears from button to mouse
• Target selector - target of event
– Object with method we intend to call
– Connect the dots programming
• Click on Juggler, brings up EventTargetDialog
– Shows public methods
– Select stopJuggling
Example (Animation bean)
– Event hookup complete
• Writes new hookup/event adapter class
• Object of class registered as actionListener fro button
• Can click button to stop animation
– Repeat for "Start the Animation" button
• Method startAnimation
Example (Animation bean)
• Save as design
– Can reloaded into BeanBox later
– Can have any file extension
• Opening
– Applet beans (like Juggler) begin executing immediately
Example (Animation bean)
• Save as Java Applet
– File->Make Applet
• Stores .class file in .jar (Java Archive File)
• Can rename and change directory
Example (Animation bean)
• To run applet
– Go to command line, go to directory where applet saved
– Should be .html file, load into appletviewer
• Background not yellow
• BeanBox container not saved as part of applet
• Applet is a container, can hold beans
– Archive property of <applet> tag
• Comma separated list of .jar files used
• .jar files for beans listed
• Source code in AppletName_files directory
1
<html>
2
<head>
3
<title>Test page for OurJuggler as an APPLET</Title>
4
</head>
5
<body>
6
<h1>Test for OurJuggler as an APPLET</h1>
7
This is an example of the use of the generated
8
OurJuggler applet.
9
archives, one per JAR used in building the Applet
HTML file
Notice the Applet tag requires several
10 <p>
11 <applet
12
archive="./OurJuggler.jar,./support.jar
13
,./juggler.jar
14
,./buttons.jar
15
"
16
code="OurJuggler"
17
width=382
18
height=150
19 >
20 Trouble instantiating applet OurJuggler!!
21 </applet>
1. archive
Program Output
Specifying the BeanInfo Class for a
JavaBean
11
12
14
15
public PropertyDescriptor[] getPropertyDescriptors()
{
PropertyDescriptor fieldWidth =
new PropertyDescriptor( "fieldWidth", beanClass );
– getPropertyDescriptors (overridden)
• Returns array of PropertyDescriptor objects
– Each describes property
– new PropertyDescriptor( "propertyName", beanClass )
• Property must have named set and get methods
26
currentValue.setBound( true );
– setBound( true )
• Specifies property is bound
Specifying the BeanInfo Class for a
JavaBean
39
public int getDefaultPropertyIndex()
40
{
41
42
return 1;
}
– getDefaultPropertyIndex (overridden)
• Returns array index of default property
• In this case, currentValue
44
public EventSetDescriptor[] getEventSetDescriptors() {
– getEventSetDescriptors
• Returns array of EventSetDescriptor objects
• Describes events supported by bean
Specifying the BeanInfo Class for a
JavaBean
46
47
48
49
50
EventSetDescriptor changed =
new EventSetDescriptor( beanClass,
"propertyChange",
java.beans.PropertyChangeListener.class,
"propertyChange");
– Constructor arguments
• 1: Class object, represents event source
• 2: String, event set name
– mouse includes mousePressed, mouseClicked...
– We use propertyChange
• 3: Class object, implemented event listener interface
– Creates anonymous object of class
• 4: String, name of listener method called when event occurs
Specifying the BeanInfo Class for a
JavaBean
52
53
changed.setDisplayName(
"SliderFieldPanel value changed" );
– Class EventSetDescriptor
– Method setDisplayName( stringName )
• Specifies name for event set in builder tool
1
// Fig. 25.38: SliderFieldPanelBeanInfo.java
2
Same package as
// The BeanInfo class for SliderFieldPanel
3
package jhtp3beans;
4
5
import java.beans.*;
bean.
Extend SimpleBeanInfo class, with
default implementation of BeanInfo
interface.
1. package
6
7
public class SliderFieldPanelBeanInfo extends SimpleBeanInfo {
11
1.1 import
public final static Class beanClass =
Create Class object, provides
SliderFieldPanel.class;
access to class
1.2 definition.
extends
Specify properties that can be
SimpleBeanInfo
by builder.
public PropertyDescriptor[]exposed
getPropertyDescriptors()
12
{
8
9
10
13
14
15
16
17
18
19
20
21
22
23
);
24
1.3 Class
try {
PropertyDescriptor fieldWidth =
new PropertyDescriptor( "fieldWidth", beanClass );
PropertyDescriptor currentValue =
new PropertyDescriptor(
"currentValue", beanClass );
PropertyDescriptor maximumValue =
2.1 setBound
new PropertyDescriptor(
"maximumValue", beanClass );
PropertyDescriptor minimumValue =
new PropertyDescriptor( "minimumValue", beanClass
Specify bound
25
// ensure PropertyChangeEvent occurs for this property
26
currentValue.setBound( true );
27
2. getProperty
Descriptors
property.
28
29
30
31
32
33
34
}
catch ( IntrospectionException ie ) {
throw new RuntimeException( ie.toString() );
35
}
36
PropertyDescriptor descriptors[] = { fieldWidth,
currentValue, maximumValue, minimumValue };
return descriptors;
3.
getDefaultProperty
Index
Get array index of default
property
(currentValue).
}
37
38
// the index for the currentValue property
39
public int getDefaultPropertyIndex()
40
{
41
42
return 1;
45
46
4.2 setDisplayName
public EventSetDescriptor[] getEventSetDescriptors() {
try {
EventSetDescriptor changed =
47
new EventSetDescriptor( beanClass,
48
"propertyChange",
49
java.beans.PropertyChangeListener.class,
50
"propertyChange");
51
52
53
changed.setDisplayName(
"SliderFieldPanel value changed" );
54
55
4.1 Constructor
}
43
44
4. getEventSet
Descriptors
EventSetDescriptor[] descriptors = { changed };
Describes events supported
by bean.
Set title of event in
builder.
56
57
58
59
60
61
62
63 }
return descriptors;
}
catch (IntrospectionException e) {
throw new RuntimeException(e.toString());
}
}
Program Output
Only the specified properties are listed.
JavaBeans World Wide Web Resources
• Web resources
– http://java.sun.com/beans/
• Download Beans Development Kit, docs
– http://java.sun.com/beans/spec.html
• Specifications
– http://java.sun.com/beans/tools.html
• Beans-enabled development tools
– http://java.sun.com/beans/directory/
• Searchable directory of beans
– http://java.sun.com/products/hotjava/bean/
index.html
• Evaluation version of bean with HTML rendering
Important Questions
1.
a) Explain the featues of java beans
b) Explain the advantages and disadvantages of
Java Beans.
2. Describe the different types of properties used in Java Beans with an
example.
3.
Explain about Introspection
4. Describe the bean info interface.
 2000 Prentice Hall, Inc. All rights reserved.