developing javabeans™ componentsread.pudn.com/downloads95/ebook/385572/developing... ·...
TRANSCRIPT
J1-673 1
Developing JavaBeans™Components
Onno Kluyt, Janet Koenig, Jeff DunnSun Microsystems
J1-673 2
In This Hour…
• JavaBeans™ component architectureoverview
• BeanContext API in detail
• Drag & Drop API in detail
• Q&A
J1-673 3
What the JavaBeans™Technolo gy Enables
In-houseDevelopedIn-house
Developed3rd Party
Beans3rd Party
BeansCustomBeans
CustomBeans
AppletApplet
ApplicationApplicationServletServlet
ToolTool
J1-673 4
What Is a Bean
JVMJVM
LifecycleLifecycle
CustomizationCustomization
EventsEvents
MethodsMethods
PropertiesProperties
PersistencePersistenceBeanBean
lang util awt net io applet rmi beans sql . . .
UnixUnix WintelWintel JavaOSJavaOS . . .. . .
J1-673 5
Milestones
• Delivered in the Java™ 2 Platform:– Extensible Runtime Containment and
Services Protocol– Drag & Drop
• JavaBeans Activation Framework 1.0.1
• InfoBus 1.2
• Beans Development Kit (BDK) 1.1
J1-673 6
BeanContext: RuntimeContainment and Services
Janet Koenig
J1-673 7
Contents
• Overview and goals
• API and usage
• Example
J1-673 8
Overview
• Support for “container” type applications– Frameworks– Browsers– Word processors
• Support for providing services to components– Error logging– Document printing interface– Spelling checker
J1-673 9
Goals
• Standard logical containment hierarchyfor components
• Standard mechanism for components toobtain services from their environment
• Improve existing support for propagatingexecution environment to components– DesignMode– Visibility– Support for Beans that are also Applets
J1-673 10
Containment Model
J1-673 11
BeanContext
• Logical hierarchy based on Collection API
• Resource support via getResource() andgetResourceAsStream()
• Distinguish between design mode andruntime via DesignMode interface
• Support for visibility via visibility interface
J1-673 12
BeanContextProx y
• If extending java.awt.Component– Delegate to BeanContextProxy– Allows one BeanContext to support multiple
containers
• If implementing BeanContext interface– Delegate to BeanContextContainerProxy
J1-673 13
BeanContextChild
• Simple Beans function normally
• Interface is necessary if Bean willrequest services
• Exposes component property fornesting BeanContext
• Nesting/unnesting operation is vetoable
J1-673 14
Changes in Containment
• BeanContextMembershipListener– childrenAdded()
– childrenRemoved()
• BeanContextMembershipEvent– Encapsulates child/children added or removed– Event propagation through hierarchy via
get/setPropagatedFrom()
J1-673 15
Services Model
J1-673 16
BeanContextServices
• Register services with this BeanContext– addService()– revokeService()
• Mechanism for child components toobtain or release service implementations– hasService()– getService()– releaseService()
J1-673 17
BeanContextServiceProvider
• BeanContext forwards requests toassociated BeanContextServiceProvider
• Factory for requested service
• Supports requests from multiple sources
J1-673 18
Change in Services Events
BeanContextServiceAvailableEvent
BeanContextServiceRevokedEvent
• Services can be “forcibly revoked”
• Propagation through hierarchy
J1-673 19
Change in Service Listeners
• BeanContextServiceRevokedListener– Components automatically registered when
service reference is successfully obtained
• BeanContextServicesListener– Listener for both service revoked and service
available events
J1-673 20
Gettin g Started
• Compliant, extensible supportimplementations– BeanContextSupport
– BeanContextChildSupport
– BeanContextServicesSupport
• BeanBox sample code
J1-673 21
BeanBox
public class BeanBox extends Panel implements BeanContextProxy
{
// Instantiate BeanContext proxyBeanContextServicesSupport services =
new BeanContextServicesSupport ();
// …
// BeanContextProxy interface method for exposing proxypublic BeanContext getBeanContext ()
{
return services ;
}
// …
}
J1-673 22
BeanBox (Cont.)
// Register the Method Tracing service with the BeanBox
services.addService ( MethodTracer.class ,
// Service provider for Method Tracer service
new BeanContextServiceProvider () {
public Object getService (BeanContextServices bcs,
Object requestor,
Class serviceClass,
Object serviceSelector) {
// …
}
});
// …
J1-673 23
Juggler
// Juggler Bean
public class Juggler extends Applet implements Runnable,BeanContextProxy , BeanContextServicesListener ,PropertyChangeListener , DesignMode {
// …
// Instantiate BeanContextChild proxy
private BeanContextChildSupport child =
new BeanContextChildSupport () {
// …
J1-673 24
Juggler (Cont.)
// Invoked by BeanBox’s BeanContextProxy when it adds new Juggler instance.
protected void initializeBeanContextResources () {
//…
// If service is available, get a reference to it, else listen for it.
if ( context.hasService ( MethodTracer.class )) {
mtService = ( MethodTracer )context.getService( … );
} else {
context. addBeanContextServicesListener ( … );
}
context . addPropertyChangeListener ( “ designMode ”, Juggler.this);
// …
}
J1-673 25
Juggler (Cont.)
// Disable method tracing for runtime. Enable for design mode if debugging.
public void setDesignTime ( boolean mode) {
// …
}
// Listener method for “designMode” property change event.
public void propertyChange ( PropertyChangeEvent evt) {
// …
setDesignTime ( mode );
}
J1-673 26
Applying BeanContext APIin LEIF
Rich Kadel, Chief Technology OfficerDTAI Inc.
J1-673 27
LEIF
• LEIF is the L ightweight E xtensibleInformation F ramework
• LEIF helps you:– Develop and deploy unified Enterprise
applications– Integrate, interrelate, summarize, and visualize
information– Manage yesterday’s legacy data with today’s
changing information to plan for tomorrow
JDJ Best Framework
J1-673 28
BeanContext in LEIF
• LEIF is a host for managing information-aware components (LEIF Extensions)
• Extensions are like Beans with knowledgeof information schema and semantics
• “View” Extensions (visualizations) can beduplicated and grouped with other Views– A Window can contain many Views– The Window provides a BeanContext with Services– The Views know what Services to use
• Major Benefit: No more Singletons!Code is more modular
J1-673 29
• The View alwaysgets the rightUndoManager andStatusBar
Quick Example
File Edit View Format HelpWindow - w/BeanContextServices
Bar Chart View
View 1
Timeline View
View 2
Undo
Status Message Area
File Edit View Format HelpWindow - w/BeanContextServices
Bar Chart View
View 1
Table View
View 2Undo
Status Message Area
J1-673 30
Dynamic MenuItems Need toKnow What’s Around
Zoom to selecteditem using the“current Map
service”
LeifBean
DisplayModel
Producer/Consumer/ProcessorExtensions
LeifSession (per LeifUser)
LeifWindowBeanLeiflet (non-MDI)
AbstractView orother Component
Non-ComponentLeifView orBCP/BCCWindow
UndoableAction
BCP or BCCComponent
(except Windows)
JAFCommandBean
Other BeanContextProxyor BeanContextChild
Objects
Leiflet (MDI)
Contentssame asnon-MDI
J1-673 32
Services Can Come and Go
• Some services are transient
• Two options for handling this:– Request, use, release, and request again (second
response may be different from first)– Request, hold, listen for ServiceRevokedEvent,
use anytime until revoked
• Example:– If I need the current Map service, I get the one
registered with my BeanContext– If there are two Maps, I get the one on top– If there are none, a Map has to be created
J1-673 33
For More Information
• http://www.dtai.com/leif
• LEIF at the JavaOne SM conference– Technical Session “Code Mobility and Dynamic
Composition Using Jini TM Technology” onThursday at 5:15 (Sheraton Palace HotelBallroom).
– Exhibitor Booth 901 (“DTAI, Inc.”)
J1-673 34
Drag and Drop Subsystem
Jeff DunnStaff Engineer, Sun Microsystems Inc.
J1-673 35
Datatransfer Protocol(After a Successful Drop)
Request Available DataFlavorsRequest Available DataFlavors
N.b.: Exactly ONE object is transferred per drop!
Request One DataFlavorRequest One DataFlavor
List of Available DataFlavorsList of Available DataFlavors
Provides One “Item” inRequested Format
Provides One “Item” inRequested Format
Interpret Object Received andCall dropComplete()
Interpret Object Received andCall dropComplete()
Drag Source Drop Target
J1-673 36
What Works?
• Full Support For– Transfers Between JVM and Native Application– Text (Unicode, ISO8859-1, US-ASCII)– List of Files– Instances of core Java 2 TM Classes (Kestrel)
• Unsupported– Nonstandard Text Encoding
(e.g., X11-Compound-Text)– Instances of Arbitrary Classes
J1-673 37
Minimal Dra g Source
• Create a DragSource (default is often OK)
• Implement the Transferable interface+ DataFlavor[] getAvailableFlavors()
+ boolean isDataFlavorSupported(DataFlavor)
+ Object getTransferData(DataFlavor)
J1-673 38
Minimal Drop Tar get
• Create a DropTarget (default is often OK)
• Implement the DropTargetListener interface+ dragEnter(DropTargetDragEvent)
+ dragOver(DropTargetDragEvent)
+ dropActionChanged(DropTargetDragEvent)
+ dragExit(DropTargetEvent)
+ drop(DropTargetDropEvent)
J1-673 39
Comin g Soon!
• Convenience Methods For Text TransfersPublic void drop(DropTargetDropEvent dropEvent) { // . . . Deleted Code . . . DataFlavor chosenFlavor =
DataFlavor.selectBestTextFlavor (
transferable.getTransferDataFlavors()); Reader reader = null; try { reader =
chosenFlavor. getReaderForText (transferable);
} /* Catch Required Exceptions */ // . . . Deleted Code . . .}
J1-673 40
Summar y
• JavaBeans™: The ComponentArchitecture for the Java™ Platform
• Information pointers– The Beans web site -
http://java.sun.com/beans
– Tools listing -http://java.sun.com/beans/tools.html
– 3rd Party Beans catalog -http://java.sun.com/beans/directory
– Feedback - [email protected]
– News group - comp.java.lang.beans
J1-673 41
Related Activities
• JavaBeans BOF– Wednesday, 8pm-9pm, A3 Marriott
• InfoBus BOF– Wednesday, 6:45pm-7:45pm, A2 Marriott
• JFC: Intro and Roadmap– Thursday 12:15-1:15, Hall A
J1-673 42
Q&A
J1-673 43