Drag and Drop - Adding Drag and Drop to an SWT Application

xiaoxiao2021-03-06  68

Drag and Drop

Adding Drag and Drop to an SWT Application

SummaryDrag and drop provides a quick and easy mechanism for users to re-order and transfer data within an application and between applications. This article is an overview of how to implement Drag and Drop and Clipboard data transfers within an SWT application.

By Veronika Irvine, IBM OTI Labsaugust 25, 2003

Drag and Drop Overview

Consider the Simple Example of Dragging Another from One Table To Another As Shown In Figure 1:

Figure 1: Shopping Cart EXAMPLE

On the right there is a list of items that I can purchase and on the left is my shopping cart. To buy something, I select it from the list on the right (I have selected "Truffle Assortment" in Figure 1), drag it over to my shopping cart and drop it. When I let go of the mouse over my shopping cart, the item will be added to the list of items I wish to purchase. In this data transfer, the list of assorted chocolates on the right is the drag source and my shopping cart is the drop target. A drag source is the source of the data being transferred and a drop target is the receiver. As I drag the mouse over a drop target, I get feedback in various forms. First, The Cursor Changes To Let Me Know That I am Over a Valid Drop target by Chang Changing from a "Do Not Enter" Sign to an Arrow (this is Called A "Drag over Effect"). The Cursor Also Tells Me What Kind of Operation Will BE Performed WHEN THE DATA IS TRANSFERRED - The Data May Be Copied or Moved or a Link May Be Made to the Data. Secondly, IF I am dragging over a widget that has sub-items like a tree or table, the sub-item may be highlighted to indicate that the data will be dropped on a specific sub-item (this is called a "drag under effect"). This is useful when you are organizing data into folders or rearranging items. in this article, I will explain in more detail the terms I have introduced above and explain how SWT allows an application to define and manipulate these components.DragSource DropTarget Clipboard Transfer

Drag Source

A drag source is the provider of data in a Drag and Drop data transfer as well as the originator of the Drag and Drop operation. The data provided by the drag source may be transferred to another location in the same widget, to a different widget within the same application, or to a different application altogether. For example, you can drag text from your application and drop it on an email application, or you could drag an item in a tree and drop it below a different node in the same tree. Let Us Walk Through A Simple Example Showing How To Define A Drag Source. The Example In Listing 1 Shows How To Drag Text from a label widget.

1Import Org.eclipse.swt.dnd. *; 2 3 // Enable a label as a Drag Source4Final Label Draglabel = New Label; 5Draglabel.Settext ("Text To Be TransferRed"); 6 7 / / Allow data to be copied or moved from the drag source8int operations = DND.DROP_MOVE | DND.DROP_COPY; 9DragSource source = new DragSource (dragLabel, operations); 10 11 // Provide data in Text format12Transfer [] types = new Transfer [] {TextTransfer.getInstance ()}; 13source.setTransfer (types); 14 15source.addDragListener (new DragSourceListener () {16 public void dragStart (DragSourceEvent event) {17 // Only start the drag if there is actually text in the18 // Label - this text will be what is dropped on the target.19 ix (Draglabel.getText (). Length () == 0) {20 Event.doit = ​​false; 21} 22} 23 Public Void DragSetData (DragsourceEvent Event) { 24 // Provide the data of the requested type.25 if (TextTransfer.getInstance (). IssupportedType (Event.DataType) {26 Event.Data = Draglabel.ge tText (); 27} 28} 29 public void dragFinished (DragSourceEvent event) {30 // If a move operation has been performed, remove the data31 // 33 dragLabel from the source32 if (event.detail == DND.DROP_MOVE). SetText (""); 34} 35} 36}); Listing 1: Dragging Text from a label widget.

Line 1: All The SWT Drag and Drop Classes Are Defined In The package org.eclipse.swt.dnd.

Lines 3 to 5:... Create a widget In order to start a drag, the user holds the mouse button down in a widget and drags the mouse In our example, the user starts the drag in the label Note that you can not have multiple . drag sources on one widget If you try to create a second drag source, an SWTError will be thrown.Lines 7 to 9: to make a widget into a drag source, we must create an org.eclipse.swt.dnd.DragSource object . The DragSource constructor takes two arguments, the widget that will be the location of the drag source and the operations that are allowed. The allowed operations determine what kind of actions the drop target can do with the data being transferred. The allowed values ​​are any Bitwise or Combination of DND.Drop_copy, DND.Drop_move or dnd.drop_link. in The Example, We are allowing the data to be copied or moved.

Lines 11 to 13: To complete the definition of a drag source, you must specify the types of data that can be transferred A data type is defined by a subclass org.eclipse.swt.dnd.Transfer such as TextTransfer or FileTransfer For.. a detailed description of Transfer types see Transfer. A drag source can provide more than one format of the data, however, it is required to provide the data in any of the specified formats when requested. in this example, the user can drag text from The label.

Line 15: Once The Drag Source IS Defined, A Mechanism IS Required for It To Interact with The Drag and Drop Operation. Added a DragsourceListener Does.

The Following Event Sequences May Occur:

DragStart DragStart, One or More DragSetdata, Dragfinished DragStart, Dragfinished

Note:.. If an exception is thrown from a drag and drop event handler, on some platforms this will cause the entire windowing system to hang Since this is extremely undesirable, SWT DND wraps all event listener code in an exception block If your event handler throws an exception, no error will be reported and the DND operation will default to DND.DROP_NONE. This "silent" exception handling is a common reason why developers think that DND is not behaving correctly. It is recommended that you wrap your event handler code and catch any possible exceptions yourself and handle them appropriately In future releases, we will try to come up with a better way to indicate that a failure has occurred.Lines 16 to 22:. The dragStart event signals that the user has performed the action that INITIATES A DRAG AND DROP OPERATION. The Exact Action Taken By The User Depends on The Platform - On Windows for Example, The User Presses Down with the Left Mouse Button and Drags for a Certain Number of Pixels (T he exact distance is configurable by the user in the Mouse control panel). On Motif, the user presses down the middle mouse button. The application does not need to be concerned about the exact conditions that started the operation because SWT handles these platform differences. When the dragStart event is received, the application has the choice to start a Drag and Drop operation or not. For example, if no valid items are currently selected in the drag source widget, the application may choose to cancel the operation. The operation is Cancelled by setting the evenet.doit field to 'false'.

Note: the application is not required to specify the type of data that will be transferred until the dragStart event has been received That is, the application can defer the call to DragSource.setTransfer until the dragStart event However, once the dragStart event has.. been processed, if no transfer types have been specified for the drag source, the Drag and Drop operation will be cancelled.By allowing the Drag and Drop operation, the application is making a promise to provide data of the specified type when requested. Failure to Do So is a violatination of the dnd Contract and Could Result in Unexpected Behavior.

Lines 23 to 28: The dragSetData event is a request for the data promised in the dragStart event This event may be called multiple times either repeatedly for the same data type or for any of the data types promised On some platforms, a potential drop.. target can request data when the mouse is moving over the drop target (eg Windows) and on some platforms the data may only be requested when a drop is performed (the mouse is released over a valid drop site) (eg Motif). Therefore, do not make any assumptions that the drag and drop operation is completed when receiving this event There is no way to know where the data is being dropped -. it could be dropped on the same application, even on the same widget or on another application.

The type of data being requested is specified in the event.dataType field. The event.dataType field contains a TransferData object. While the fields of the TransferData object are public, they are platform dependant. Therefore, you should not access the fields directly but rather pass the TransferData to a Transfer object to determine the type of data. This is done in the example by calling TextTransfer.getInstance (). isSupportedType (line 25) .The drag source must fill in the event.data field. The exact form of the data depends on the Transfer type. for example, TextTransfer expects a String object containing the entire text whereas a FileTransfer expects a String array where each entry in the array is the absolute path of a File. The javadoc for each transfer type should describe . More information.

Lines 29 to 34: The dragFinished event indicates that the drag and drop operation is complete The user may have dropped the data on a valid location, dropped the data on an invalid location or hit Escape If the user dropped the data on an invalid.. location or hit Escape, the event.doit field will be false and the event.detail field will be DND.DROP_NONE. If the user dropped the data on a valid location, the event.doit field will be true and the event.detail field Will Indicate The Kind of Operation Performed by The Drop target. This Will Be One of Values ​​Specified In Table 1.

dragFinished event.detail valueDescriptionDND.DROP_COPYThe drop target made a copy of the data.DND.DROP_LINKThe drop target made a link to the data - usually only used for files.DND.DROP_MOVEThe drop target made a copy of the data and the drag source should now delete the original and update its display.DND.DROP_TARGET_MOVEThe drop target moved the data from its original location to a new location This is usually only used with files In this case, the drag source does not need to delete the original;.. it Just Needs to Update ITS Display Information.Table 1: Valid Dragfinished Event.Detail Values

DROP TARGET

A drop target receives data in a Drag and Drop operation. The data received by the drop target may have come from the same widget, from a different widget within the same application, or from a different application altogether. For example, you can drag text From An Email Application and Drop It On Your Application, or You Could Drag An Item in A Tree and Drop It Below a Different Node In The Same Tree.

Let Us Walk Through a Simple Example Showing How To Define A Drop target. The Example In Listing 2 Shows How To Drop Files Or Text On A Table Widget.

1Import Org.eclipse.swt.dnd. *; 2 3 // Enable a Table AS A DROP TARGET4FINAL TABLE DROPTABLE = New Table (Shell, SWT.BORDER); 5for (INT i = 0; i <10; i ) {6 TableItem item = new tableitem (DROPTABLE, SWT.NON); 7 item.Settext ("item" i); 8} 9 10 // allow data to be copied or moved to the drop target11operty = DND.DROP_MOVE | DND.DROP_COPY | DND.DROP_DEFAULT; 12DropTarget target = new DropTarget (dropTable, operations); 13 14 // Receive data in Text or File format15final textTransfer textTransfer = TextTransfer.getInstance (); 16final FileTransfer fileTransfer = FileTransfer.getInstance (); 17types = new Transfer [] {fileTransfer, textTransfer}; 18target.setTransfer (types); 19 20target.addDropListener (new DropTargetListener () {21 public void dragEnter (DropTargetEvent event) {22 if (event.detail == DND.DROP_DEFAULT) {23 if ( (Event.Operations & DND.drop_copy)! = 0) {24 Event.detail = DND.DROP_COPY; 25} else {26 Event.detail = DND.DROP_NONE; 27} 28} 29 // Will Accept Text But Prefer to Have Files Dropped 30 for (INT I = 0; I

43 if (textTransfer.isSupportedType (event.currentDataType)) {44 // NOTE: on unsupported platforms this will return null45 String t = (String) (textTransfer.nativeToJava (event.currentDataType)); 46 if (t = null!) {47 System.out.println (t); 48} 49} 50} 51 public void dragOperationChanged (DropTargetEvent event) {52 if (event.detail == DND.DROP_DEFAULT) {53 event.detail = (event.operations & DND .Drop_copy)! = 0) {54 Event.detail = DND.DROP_COPY; 55} else {56 Event.detail = DND.Drop_none; 57} 58} 59 // Allow text to be moved but files shouth release only be copied60ix ( fileTransfer.isSupportedType (event.currentDataType)) {61 if (event.detail = DND.DROP_COPY) {62 event.detail = DND.DROP_NONE;! 63} 64} 65} 66 public void dragLeave (DropTargetEvent event) {67} 68 Public void Dropaccept (Drop) TargetEvent event) {69} 70 public void drop (DropTargetEvent event) {71 if (textTransfer.isSupportedType (event.currentDataType)) {72 String text = (String) event.data; 73 TableItem item = new TableItem (dropTable, SWT. None); 74 item.setText (text); 75} 76 if (FileTransfer.issupportedType (Event.currentDataType) {77 string [] files = (String []) Event.data; 78 for (INT i = 0; i

Line 1: All the related drag and drop classes are defined in the package org.eclipse.swt.dnd.Lines 3 to 8:. Create a widget The user drags the data over a target, which in user interface terms is some visible widget ................................. ..

Lines 10 to 12: Define the types of operations that this drop target is likely to perform on any data dropped on it This is a bitwise OR combination of any of DND.DROP_COPY, DND.DROP_MOVE, or DND.DROP_LINK By default,.. If No Operations Are Defined, The Dnd.drop_move Operation is Assumed.

The operation may also include DND.DROP_DEFAULT (Note: the DND.DROP_DEFAULT style must be combined with one or more of the other DND.DROP_ * styles). The DND.DROP_DEFAULT style allows the drop target to determine the default operation - the default operation is what happens when no modifier keys are pressed. If the DND.DROP_DEFAULT style is NOT specified, the default operation will be DND.DROP_MOVE. See also dragEnter and dragOperationChanged below.

Lines 14 to 18:.. Define the types of data that this drop target will accept A data type is defined by a subclass org.eclipse.swt.dnd.Transfer such as TextTransfer or FileTransfer For a detailed description of Transfer types see Transfer. A drop target can be receptive to more than one type of data. The current 2.1 implementation will, however, only allow one format to be retrieved in the final drop. that is to say, the drop target can indicate that it is interested in both text and RTF text, but only one of these is provided in the drop event -.. either text or RTF text The advantage of registering for both formats is that some applications may only provide text and some may only provide RTF text If an application provides both, you can choose which format you would prefer to receive See dragEnter below.Line 20:. Once the drop target is defined, a mechanism is required for it to interact with the Drag and drop operation Adding a DropTargetListener does this..

Note:.. If an exception is thrown from a drag and drop event handler, on some platforms this will cause the entire windowing system to hang Since this is extremely undesirable, SWT DND wraps all event listener code in an exception block If your event handler throws an exception, no error will be reported and the DND operation will default to DND.DROP_NONE. This "silent" exception handling is a common reason why developers think that DND is not behaving correctly. It is recommended that you wrap your event handler code And Catch Any Possible Exceptions YourSelf and Handle The Appropriately. in Future Releases, We Will Try to com up with a failure has ion turred.

The Following Event Sequences May Occur:

dragEnter, dragLeave dragEnter, one or more dragOver *, dragLeave dragEnter, one or more dragOver *, dragLeave, dropAccept dragEnter, one or more dragOver *, dragLeave, dropAccept, drop * may be a mixture of dragOver and dragOperationChanged

Line 21 to 40:. The dragEnter event occurs when a drag and drop operation is in progress and the cursor enters the bounds of the drop target widget If the cursor leaves the bounds of the widget and re-enters, another dragEnter is issued.

Lines 22 to 28: A drag over effect is a visual cue to the user about what kind of operation will be performed when the drop occurs The exact appearance of the visual cue is platform dependant - some examples are shown in Figure 2. The drop. target can update this drag over effect by setting the event.detail field to one of DND.DROP_COPY, DND.DROP_MOVE, DND.DROP_LINK or DND.DROP_NONE. The value set in the event.detail field must be one of the values ​​defined in the event.operations field, which is a bitwise OR of the operations supported by the DragSource. If a value is chosen that is not allowed by the DragSource, the operation would be set to DND.DROP_NONE. The event.detail field can also be Updated in The Dragover, DragoperationChanged, DROPACCEPT AND DROP Events.

In dragEnter, the application can define the default operation. As discussed above, if the drop target is created with the style DND.DROP_DEFAULT, it will be notified when there are no user modifier keys pressed. In this case, the event.detail field in the dragEnter event is set to DND.DROP_DEFAULT. The application can specify the default operation by changing the event.detail field to the desired operation. If the application does not change the event.detail field from DND.DROP_DEFAULT to some operation, it will by default be changed to DND.DROP_MOVE. The DND.DROP_DEFAULT value is also set in the dragOperationChanged event.In our example, we make Copy the default operation if it is allowed by the drag source.

OperationWin32 CursormoveCopyLinknone

Figure 2: Cursors for the allowed Transfer Operations

Lines 29 to 39: The drop target can choose what type of data it would prefer to receive The dragEnter event has two fields for this, event.currentType, which is the type of data preferred by the application (represented by a TransferData object). , and event.dataTypes, which is the list of types provided by the drag source (represented by an array of TransferData objects). You can set the event.currentType to any value in the event.dataTypes. These fields can also be modified in The Dragover, DragoperationChanged, And Dropaccept events.

In the example, we are allowing Text or Files to be dropped on the table, but if both are available, we prefer to get the Files. If a file is being transferred, we also have restricted the operation to allow only copy (we don 'T Want You to Delete Files from your Operating System WHEN You Play this esample :)).

Lines 41 to 50: The dragOver event occurs repeatedly as the user drags the cursor over the drop target widget If the cursor is motionless, the dragOver event will continue to be received at regular intervals In addition to what is shown in the example,.. you can modify the event.detail or event.currentType fields. This is most often done with a table or tree when the operation changes based on what item you are over. For example, you may have a tree representing the file system and you may allow files to be dropped on folders but not on other files The event.item field will indicate what item you are over in a table or tree.Line 42:. A drag under effect is some visual cue or action in the drop target widget that Gives The User More Detailed Feedback About Where The DRAY OCCUR. The Application CAN Control The Drag Under Effect by Setting The Event.Feedback Field As Shown in Table 2.

dragOver event.feedback valuesDescriptionDND.FEEDBACK_SELECTThe item under the cursor is selected; applies to table and trees.DND.FEEDBACK_SCROLLThe widget is scrolled up or down to allow the user to drop on items that are not currently visible; applies to tables and trees.DND .FEEDBACK_EXPANDThe item currently under the cursor is expanded to allow the user to select a drop target from a sub item; applies to trees.DND.FEEDBACK_INSERT_BEFOREAn insertion mark is shown before the item under the cursor; applies to tables and trees.DND.FEEDBACK_INSERT_AFTERAn INSERTION MARK IS SHOWN AFTER THE ITEM Under The Cursor; Applies To Tables and Trees.dnd.FeedBack_noneno Effect Is Shown.

Table 2: Drag Under Effect Types

Lines 43 to 49:. When dragging data over the target, the information provided in the event tells you what type of data is being dragged but does not give the content of the data For example, the event.currentType may indicate that a file is being dragged but the event does not indicate the name of the file or the extension of the file. If your application will perform a different operation for a Java file versus a text file, then you might want to get the data in the dragOver event. Unfortunately this is not supported on all platforms. As of 2.1, it is only possible to access the data in the dragOver event on Windows. The data can be accessed by passing the event.currentType TransferData object to the nativeToJava method of the corresponding Transfer object .. On all platforms except Windows, this will return null in the future, this capability may be extended to other platforms where supported (Note:. in 1.0 through 2.1, you can only get the data for event.currentType but in 3.0, you CAN Get the data for any type in event.datatypes).

Lines 51 to 65:.. The dragOperationChanged event occurs when the user presses or releases a modifier key (such as Ctrl, Shift, Command, Option) The modifier keys are used to switch the operation to be performed For example, on Windows when just the Ctrl key is down, a copy is requested, when the Ctrl and Shift keys are both down, a link is requested and when just the Shift key is down, a move is requested. When no modifier keys are pressed, the default operation is Requested. See the Dragenter Event for more details.

Lines 66 to 67:. The dragLeave event occurs when the cursor moves outside of the drop target widget If you allocated any resources in dragEnter, you should free them in dragLeave The dragLeave event also occurs if the user cancels the Drag and Drop operation by. hitting Escape, and just before a drop is performed.Lines 68 to 69:. The dropAccept event provides the application with one last chance to define the type of data that will be returned in the drop event This is done by setting the event.currentDataType To one of the valueness defined in Event.datatypes.

Lines 70 to 83: The drop event occurs when the user releases the mouse over the drop target if a valid operation and currentDataType were requested in the previous events The event.data field contains the data requested The object type contained in the event... Data Field Depends On What Transfer Type Was Requested. The Data IS of The Type Defined in The Event.currentDataType Field.

.

CLIPBOARD

Drag and drop allows the simultaneous transfer of data between a source and a target but sometimes the user may wish to copy data to be transferred at a later point in time. The Clipboard acts like a temporary holder for the data. In addition, the same Data May Be copied to multiple targets using the clipboard.

In The Following Example, We will Copy Data in Two Different Formats ONTO The Clipboard and Retrieve One of the Formats from The Clipboard.

1Import org.eclipse.swt.dnd. *; 2. .3public static void main (string [] args) {4 Display Display = new display (); 5 final clipboard cb = new clipboard (display); 6 shell shell = New shell (Display); 7 final text text = new text (shell, swt.border | swt.multi); 8 text.setbounds (10, 10, 300, 300); 9 Button Button = New Button (shell, SWT. Push); 10 Button.Settext ("copy"); 11 Button.SetBounds (320, 10, 100, 40); 12 Button.Addlistener (SWT.SELECTION, New Listener () {13 public void handleevent (Event E) { 14 string textdata = text.getSelectionText (); 15 if (TextData == Null) Return; 16 // To show the rtf formatting, make the text bold and italic17 string rtfdata = "{// r r r r 1 1 / = textData "}"; 18 textTransfer textTransfer = TextTransfer.getInstance (); 19 rTFTransfer rtfTransfer = RTFTransfer.getInstance (); 20 Transfer [] types = new Transfer [] {textTransfer, rtfTransfer}; 21 cb.setContents (new Object [] {TextData , RTFDATA}, Types; 22} 23}); 24 Button = New Button (shell, swt.push); 25 Button.Settext ("Paste"); 26 Button.SetBounds (320, 60, 100, 40); 27 button.addListener (SWT.Selection, new Listener () {28 public void handleEvent (Event e) {29 TextTransfer transfer = TextTransfer.getInstance (); 30 String data = (String) cb.getContents (transfer); 31 if ( Data == null) Return; 32 text.insert (data); 33} 34}; 35 shell.Open (); 36 While (! shell.isdisposed ()) {37 if (! display.readDispatch ()) 38 Display.sleep (); 39 40} 41 cb.dispose (); 42 display.dispose (); 43}

Listing 3: USING THE CLIPBOARD to COPY TEXTLINE 1: All The Related Drag and Drop Classes Are Defined In The package org.eclipse.swt.dnd.

Line 5: Create a clipboard object A Clipboard object gives you access to the operating system clipboard It will allow you to exchange data within your application or with other applications Note:... A Clipboard object uses system resources and must be released when you are Finished (See Line 41). You can choose to create a new clipboard Object Each Time You Want To Access The Data on The Clipboard or You Can Keep An Instance Around for your application.

Lines 14 To 21: Copy The Text Selected in The Text Widget ONTO The Clipboard. If no text is selected, do nothing.

When placing data on the clipboard, it is possible to make the data available in more than one format. In our example, we are using both Text and RTF Text formats. This makes it possible for our application to interact with a larger variety of applications . that is, if we only place RTF text on the clipboard, then we can not interact with an application such as Notepad that does not understand RTF text. To better integrate your application into the environment, make your data available in as many useful forms as Possible.

Each successful call to Clipboard.setContents will clear any previous content from the clipboard. This applies not only to the data formats you are placing on the clipboard but to all data formats. For example, if the clipboard contains File data placed there by some other application and you call Clipboard.setContents with Text data, the File data will no longer be available. If you want to have multiple kinds of data available on the clipboard at the same time, you must pass them to the clipboard in the same Clipboard. setContents call. The Clipboard.setContents API takes an array of data objects and an array of data types. The values ​​for the data objects must be in the same order as the data types and must be valid for the corresponding data type (that is, a TextTransfer expects a String, a File Transfer expects a String [] - the javadoc for each type should specify the expected format of the data) .The data that you place on the clipboard will be available even after your application IS colored. (NOTE: GTK is an exception to this. currently, there is no means the application.)

On some platforms, there are actually multiple clipboards available. On Unix / Linux there is a PRIMARY clipboard which is supposed to contain the data that was most recently selected (implicitly set by the act of selection) and there is a CLIPBOARD clipboard which is supposed to contain data that was explicitly set through a keyboard accelerator (such as CTRL Insert) or through a menu item (there are also other clipboards but these are used infrequently). In Motif, only the CLIPBOARD Clipboard is supported by SWT. In GTK , SWT has rudimentary support for both clipboards;. when you call Clipboard.setContents, the data will be placed on both the PRIMARY and the CLIPBOARD clipboards In the future, this support should be expanded to Motif and to support the implicit style On the. Mac, SWT Supports The Default Scrap.Lines 29 To 32: IF The IS Text Available On The Clipboard, Insert It Text Widget.

To request data of a certain type, call Clipboard.getContents with a Transfer subclass of the appropriate type. In our example, we are pasting Text and have used the TextTransfer class. If there is no data of this type available, null will be returned ............................

On GTK, the SWT implementation looks for a matching data value first in the CLIPBOARD clipboard and then in the PRIMARY clipboard. On Motif, the SWT implementation only looks for a match in the CLIPBOARD clipboard.

Line 41: Dispose of The Clipboard. AS Mentioned Above, Failure to Dispose The Clipboard Will Result System Resources Being Leaked.

New for 3.0 - How To Query Data Types on the clipboard

In 2.1 and earlier releases of SWT, the only mechanism available to determine if a specific data type is available on the clipboard is to use Clipboard.getContents and actually transfer the data. In addition to being slow, this mechanism can cause problems when a " Cut "operation is being performed. In a" Cut "operation, one application puts data on the clipboard and when that data is transferred from the clipboard, the original copy of the data is deleted. In 3.0, there will be new API to query what types of data are on the clipboard without actually transferring the data to your application An example of how to use it is shown in Listing 4.1TransferData [] available = cb.getAvailableTypes ();. 2boolean enabled = false; 3for (int i = 0; I

Listing 4: Query The Data Types Available On The Clipboard.

Clipboard.getAvailableTypes returns an array of TransferData objects. A TransferData object contains platform specific information that represents a data type. While the fields of the TransferData object are public, do not attempt to interpret them directly. Instead use the "isSupportedType" method of any Subclass of Transfer as shown in line 4.

TRANSFER

Transfer is an abstract class that provides a mechanism for converting between a Java representation of data and a platform specific representation of data and vice versa. The Java representation of the data is the currency the application uses. For example, text is represented by a String object. The platform specific representation is the currency of the operating system and is represented in SWT by the TransferData object. Table 3 shows the subclasses of Transfer provided in org.eclipse.swt.dnd.TransferJava formatExampleTextTransferString "hello world" RTFTransferString "{/ / RTF1 // B // I Hello World} "FileTransferString [] new string [] {file1.getabsolutePath (), file2.getabsolutePath ()}

Table 3: Transfer Types provided by SWT

The TransferData class contains public fields that are platform-specific. Because the fields in TransferData vary platform to platform from, applications should not access them. The purpose of making the fields public is to allow developers to extend the Transfer class and provide additional platform specific Types for Data Transfer (EG, Bitmap Images or Wave Files).

Summary

Drag via This article describes the SWT mechanism for transferring data either and Drop or using the Clipboard. SWT uses the underlying operating system mechanism which allows data transfer across applications for maximum system integration. SWT provides standard data transfer types as well as an infrastructure for defining Your OWN Transfer Types or Supporting Additional Platform-Defined Data Types.

THL789

9CBS certified blog expert

System analyst

Android

artificial intelligence

转载请注明原文地址:https://www.9cbs.com/read-94980.html

New Post(0)