|
|
|||||||||||
About com.ms.object.dragdropThis package provides classes and interfaces for implementing visual drag-and-drop operations. OverviewThe com.ms.object.dragdrop package uses the transfer model defined in the com.ms.object package, adding behavior that is specific to visual drag-and-drop operations. The basic dragdrop protocol is defined by the DragHandler, DragSession, and DragSource interfaces. The DragHelper and DragProxy classes help developers implement the dragdrop protocol for objects that are not supported by the system. DragProxy is a simple DragHandler implementation that forwards all calls to a specified DragHandler, with an optional offset to the drag coordinates. The DragHelper class implements the DragHandler interface and distributes DragHandler method calls to child handlers. The DragHandler interface is implemented by recipients who want to receive drops from a drag source. This interface contains methods for notifying the recipient when the object has entered, exited, moved, or has been dropped by the user. DragSession extends the TransferSession interface and adds the concepts of modifier keys and mouse state. The DragSession interface is generally implemented by the dragdrop implementation. To customize the behavior of a drag operation as it progresses, you can implement the DragSource interface, which provides capabilities such as cursor control. Drag Handler ExampleThe following example shows how to implement a DragHandler. In particular, this class implements a ColorTile object that can receive a new color (java.awt.Color) by a drag-and-drop operation. class ColorTile extends Canvas implements DragHandler { static final private Class colorClass = new Color( ).getClass( ); Color color; Color colorHot; boolean hot = false; public ColorTile(Color c, int x, int y) { super( ); initFromColor(c); reshape(16 * x + 64, 16 * y + 64, 128, 128); } private void initFromColor(Color c) { color = c.darker( ); colorHot = c; hotTrack(hot); } private void hotTrack(boolean hot) { this.hot = hot; Color c = hot? colorHot : color; setBackground(c); if (isShowing( )) repaint( ); } public void dragEnter(DragSession session) { hotTrack(true); } public void dragLeave( ) { hotTrack(false); } public int dragOver(DragSession session, int x, int y) { //Interrogate the data to see if there is a color. MetaObject data = session.getTransferData( ); if ((data != null) && data.hasObject(null, colorClass)) { //There is a color. Return a value that indicates // that we can copy it if the source is allowing copy // operations. return session.getAvailableEffects( ) & Transfer.COPY; } return Transfer.NONE; } public void drop(DragSession session int x, int y) { MetaObject data = session.getTransferData( ); if (data != null) { Object object = data.getObject(null, colorClass); if (object instanceof Color) initFromColor((Color)object); } } } Drag Session ExampleStarting a drag session differs, depending on whether you are using AFC or AWT. Two code samples follow: the first is for AWT, and the second is for AFC. AWT Exampleimport java.awt.*; import com.ms.object.*; import com.ms.object.dragdrop.*; import com.ms.awt.WDragSession; class DragMeAWT extends Canvas { //... public boolean mouseDown(Event e, int x, int y) { ObjectBag data = new ObjectBag(); data.addObject(null, null, Color.red); WDragSession.beginDrag(data, Transfer.COPY | Transfer.LINK); return true; } }
AFC Exampleimport com.ms.ui.*; import com.ms.object.*; import com.ms.object.dragdrop.*; import java.awt.Color; class DragMeAFC extends UICanvas { //... public boolean mouseDown(Event e, int x, int y) { ObjectBag data = new ObjectBag(); data.addObject(null, null, Color.red); UIDragDrop.beginDrag(data, Transfer.COPY | Transfer.LINK); return true; } } Classes
Interfaces
HierarchyObject | +--DragHelper (DragHandler) | +--DragProxy (DragHandler) DragSource DragSession DragHandler
|