Operating system is the most commonly used drag and drop movement of files to copy, drag and drop increased use of simple software. SWT also supports drag and drop, but more complicated programming. Figure 18.1 Schematic diagram of the realization of drag, drag it first to set a good source (DragSource) and to receive the targeted (DropTarget), and then drag the data to set a delivery carrier (Transfer), the last is the most tedious DropTarget were added to give DragSource and drag the listener, and drag their way under the circumstances of each method to achieve the listener.
Figure 18.1 SWT implementation diagram of Figure 18.2 drag drag drag an instance of a given instance of the following, the operation effect as shown in Figure 18.2. Graph on the left is a button on the right is a text box, you can drag the button text to the text box. If you hold down the Shift key drag, compared to mobile text, the result is a button into a blank text to move the text box. Concrete realization of the code is as follows:
public class DragAndDrop1 (
private TextTransfer textTransfer = TextTransfer.getInstance ();
private Button sourceText;
private Text targetText;
public static void main (String  args) (
new DragAndDrop1 (). open ();
private void open () (
final Display display = Display.getDefault ();
final Shell shell = new Shell ();
shell.setSize (300, 100);
/ / --------- Create a window of the other interface components -------------
/ / Create window components
shell.setLayout (new FillLayout ());
sourceText = new Button (shell, SWT.NONE);
new Label (shell, SWT.NONE); / / buffer zones
targetText = new Text (shell, SWT.BORDER);
/ / Set ----------------- --------- drag
/ / Set sourceText for the drag source. Allows data to be moved or copied
DragSource source = new DragSource (sourceText, DND.DROP_MOVE | DND.DROP_COPY);
source.setTransfer (new Transfer  (textTransfer });// set the transmission vector for text-based
source.addDragListener (new MyDragSourceListener ());
/ / Set the target to targetText
DropTarget target = new DropTarget (targetText, DND.DROP_MOVE | DND.DROP_COPY | DND.DROP_DEFAULT);
target.setTransfer (new Transfer  (textTransfer));
target.addDropListener (new MyDropTargetListener ());
/ / ----------------- END ------------------------
while (! shell.isDisposed ()) (
if (! display.readAndDispatch ())
private class MyDragSourceListener implements DragSourceListener (
/ / Determine whether to allow drag. Set the empty string here Do not allow drag and drop
public void dragStart (DragSourceEvent event) (
Button button = getSource (event); / / getSource a custom method
if (button.getText (). trim (). equals (""))
event.doit = false;
/ / Custom method to obtain drag source component
private Button getSource (DragSourceEvent event) (
DragSource source = (DragSource) event.widget;
Button button = (Button) source.getControl ();// that sourceButton
/ / Set the need to transfer data Transfer
public void dragSetData (DragSourceEvent event) (
if (textTransfer.isSupportedType (event.dataType)) (/ / if the type of data to support drag and drop
Button button = getSource (event);
event.data = button.getText ();
/ / Drag and drop operation after the completion of the implementation of this method
public void dragFinished (DragSourceEvent event) (
if (event.detail == DND.DROP_MOVE) (/ / If it is moving the text, delete sourceText
Button button = getSource (event);
private class MyDropTargetListener implements DropTargetListener (
/ / Mouse into the target component, call this method
public void dragEnter (DropTargetEvent event) (
/ / Default to DND.DROP_DEFAULT, here, set it to copy; hold down the Ctrl key as DND.DROP_COPY; hold down the Shift key as DND.DROP_MOVE.
if (event.detail == DND.DROP_DEFAULT)
event.detail = DND.DROP_COPY;
/ / Mouse component in the target range will continue to call this method. In this method, mainly to set event.feedback, its possible values are:
/ / FEEDBACK_EXPAND to expand the current item under the cursor to drag the sub-item, only for the tree component.
/ / FEEDBACK_INSERT_AFTER is under the cursor in a displayed after an insert tag, only for tables and trees.
/ / FEEDBACK_INSERT_BEFORE is currently under the cursor in a display before an insert tag, only for tables and trees.
/ / FEEDBACK_NONE do nothing.
/ / FEEDBACK_SCROLL the target components can be rolling, so that you can drag items on the current invisible, only for tables and trees.
/ / FEEDBACK_SELECT to the current item under the cursor is selected, only for tables and trees.
public void dragOver (DropTargetEvent event) (
event.feedback = DND.FEEDBACK_NONE;
/ / When the button is pressed or release aid (such as Ctrl, Shift) This method is called when
public void dragOperationChanged (DropTargetEvent event) (
if (event.detail == DND.DROP_DEFAULT)
event.detail = DND.DROP_COPY;
/ / When the mouse leaves the target component will call this method
public void dragLeave (DropTargetEvent event) ()
/ / Upon completion of drag and drop, drop method called before the implementation of this method.
public void dropAccept (DropTargetEvent event) ()
/ / Drag and drop operation in the completion of the last method call
public void drop (DropTargetEvent event) (
if (textTransfer.isSupportedType (event.currentDataType)) (
String str = (String) event.data; / / transmit data out
DropTarget target = (DropTarget) event.widget;
Text text = (Text) target.getControl ();// that targetText
Description of the procedures:
● Although the drag used in the tables and trees, and this section is an instance of the button and text boxes to do, but the basic principles and code structure are the same.
● You can set multiple drag targeted. Drag a component can be both the source and can also be drag targeted.
● event.detail = DND.DROP_NONE can drag failure.
● TextTransfer is a subclass of Transfer to see Transfer of the class hierarchy can be found in more drag and drop transfer vector.
【】 Http://book.csdn.net/bookfiles/439/10043915579.shtml transferred from
【】 Http://hi.baidu.com/xingzhecom/blog/item/c7a5594aefe8f32b09f7efb9.html transferred from
SWT classes related to drag and drop in org .. eclipse.swt.dnd package. Drag first need to achieve a DragSoruce class, which is to be drag and drop the object, a DropTarget, that is, to put the destination object, but also drag and drop objects between the two carry the data transmission with Transfar class to define. Java will be in the process of dragging the data into the global variable data stored locally, then the process in place, and then save the global variables from the local data will be removed.
Drag and drop source is a class, the real drag is created when binding the control, such as
Table t = new Table (shell, SWT.NONE)
DragSource source = new DragSource (t, DND.Drop_MOVE | DND> DROP_COPY)
The same widget can only be bound to a DragSource or DropTarget on. Create drag source drag and drop the second argument is the type of action to support the process, which determines the drag target. DND class defines some constants dedicated to drag and drop.
Constant operation instructions
The item is copied when dragged in or out of this control.
The item is moved from its current location to wherever it's dropped.
Dropping the item creates a link back to the original.
Nothing happens when the item is dropped.
The example that allows mobile and replication.
Drag and drop drag and drop source was created to define the process required to transfer the data type, what data format will be stored in the local system global variables; use DragSource.setTransfer (Transfer  transferAgents) method for data definition, its parameters Transfer is an instance (usually an instance of its subclass) consisting of an array such as:
dragSource.setTransfer (new Transfer  (TextTransfer.getInstance () });// pass strings or to allow transfer of two data types:
Source.setTransfer (new Transfer  (TextTransfer.getInstance (), FileTransfer.getInstance ()});
Finally add the drag and drop event handling method that: dragSource.addDragListener (new DragSourceListener) or use DragSourceAdapter. The monitor includes three events:
a) dragStart (DragSourceEvent): Start drag moment occurred; generally used to determine whether to begin dragging. If that event were not allowed to drag the doit property false. Such as the form for the drag source to determine the form a selected row can start dragging.
b) dragSetData (DragSourceEvent): the release trigger operation has been executed. This method shall be to drop the data, the initial event in the data field is assigned to. event of the dataType field specifies the data type to be set if the drag source is created when binding Transfer allows transfer of multiple data types (that is, binding a variety of Transfer), is required here to determine under event.dataType What format will the data into the data domain, such as:
if (TextTransfer.getInstance (). isSupportedType (event.dataType))
(... / / Assign a string to event.data)
else if (FileTransfer.getInstance (). isSupportedType (event.dataType))
(... / / Assign string array to event.data)
c) dragDinished (DragSourceEvent): After the completion of drag and drop events, usually some cleanup work, such as if the drop is defined as moving in to remove the drag source data has been moved. By event.detail target domain can be carried out to know what action, these actions include
i. DND.DROP_COPY: Copy the data target
ii. DND.DROP_MOVE: cut the target data, to delete the original data
iii. DND.DROP_LINK: aim to create a shortcut object
iv. DND.DROP_TARGED_MOVE: purpose of the object to change the location of the data, usually used to move files corresponding to different operations in different mouse shapes. When drag and drop operation in the end to achieve what needs to target identified in the control of the listener (see below).
Transfer class implementation using SWT drag source to the destination release of the data type of communication: mobile data must be mutually understood format. Transfer the reason there is a need to drag and drop the data stored in the local operating system as a global variable, so the need for a java implementation of data between the local data format conversion mechanism.
Transfer each sub-class represents a specific data type, and data to know how to convert between java and the local operating system, including text, files, and RTF format text, if the data type does not meet the requirements, users can customize Transfer sub-class definitions (see org.eclipse.swt.dnd.Transfer and org.eclipse.swt.dnd.ByteArrayTransfe document). Transfer the applications, can be regarded as a representative example of a specific type of black box data. Each child class has a static factory method getInstance () to create an instance, we only need it to be passed as a parameter to specify the data type, do not call it the other way. In essence, by Transfer, SWT calls javaToNative () and nativeToJava () to convert the data (when needed). Transfer sub-class of common:
l FileTransfer: name used to pass one or more files, in fact, an array of strings (interpreted as multiple file paths stored in the string array);
l TextTransfer: passing a string;
l RTFTransfer: traditional rich text formatting string format
By creating a DropTarget to register a control the ability to drag and drop with the receiver, for example:
DropTarget target = new DropTarget (table, DND.DROP_COPY | DND.DROP_DEFAULT);
The second parameter which defines the operation to allow the release target. In the mouse to drag the process, we can graph the changes in the mouse that the current passes through it is an effective DropTarget, this process is called "drag over effect". Mouse graphics can also tell us after the data were Drop, what kind of operation will be executed, is copied or moved or something else. SetTransfer way through after setting allows reception of data types. An example:
int operations = DND.DROP_MOVE | DND.DROP_COPY | DND.DROP_LINK;
Transfer  types = new Transfer  (TextTransfer.getInstance ()};// and source can be defined as a variety of Tranfer
DropTarget target = new DropTarget (label, operations);
Finally the need to add the listener Target: addDropListener (new DorpTargetListener ()); listener of all the methods of the parameters are DropTargtEvent, it has several important attributes:
a) currentDataType: the type of data to be released. It is a TransferData instance of the class, we do not know the specific value, because this class is to determine non-Transfer support the use of: Transfer.isSupportedType (transferData).
b) dataTypes: DragSource data types available in the collection (TransferData class set)
c) operations: the set of available operations is the source and destination to allow operation of the intersection;
d) detail: drag and drop the completion of the operation to be executed
e) data: the data storage drag and drop listener methods included:
l dragEnter (DropTargtEvent event): the cursor into the target area when the trigger. This method is generally used to define the type of drag and drop operation, that is, to event.detail assignment; event.detail the value of the final drop in the end determine what action.
event.detail value can only be event.operations in one or DND.DROP_NONE (that is, without any operation). The event.operations is drag and drop source DragSource support all operations (create, enter the second parameter) with the purpose to support all the operations DropTarget intersection. (This part is none of all the materials mentioned, including some foreign materials, such as "SWT / JFace in Action" also was not clear) that it is a whole number, by observing the binary number is 1 of you may know include what operation (can be learned by bits and computing).
DropTarget increase DND.DROP_DEFAULT parameters when you create that allows you to define the default action, which it specifically refer to this method of operation requires the definition is to DND.DROP_DEFAULT specify a specific operation. If this method and in dragOperationChanged () no to it in the definition, then it will be the system defined as DND.DROP_MOVE. DND.DROP_DEFAULT purpose is to determine if the process of dragging the auxiliary button is not pressed, not, event.detail equivalent DND.DROP_DEFAULT, then you can specify the default action event.detail to achieve. For example:
if (event.detail == DND.DROP_DEFAULT) (
/ / The value assigned to the event.detail must event.operations a, event.operations DragSource in operations are supported.
if ((event.operations & DND.DROP_COPY)! = 0) (
event.detail = DND.DROP_COPY;
) Else (
event.detail = DND.DROP_NONE;
If you press the drop in the auxiliary key is, naturally, is no longer the default action, and the impact of auxiliary keys in the way dragOperationChanged () is defined.
l dragOver (DropTargtEvent event): the cursor into the target area of the event, as long as the cursor in the area, the event will continue with the trigger, even if the cursor does not move. Usually drag on the table or when the tree to use this approach, and event.feedback property
l dragOperationChanged (DropTargtEvent event): the user press or release event is triggered when the secondary key, secondary key definition may be under the local system; but the definition of supporting key events must be included in the event.operations in effect unless (event . detail into DND.DROP_NONE). In the operations permitting:
n drop by holding down the Ctrl, event.detail into DND.DROP_COPY;
n Hold down the Shift, event.detail into DND.DROP_MOVE;
n while holding down the CTRL + SHIFT, event.detail into DND.DROP_LINK;
n open auxiliary keys are event.detail into DND.DROP_DEFAULT;
If you do not DropTarget Add DND.DROP_DEFAULT, then release into a secondary key event.detail DND.DROP_MOVE.
Here can also change currentDataType value (the value after conversion, however, must be dataTypes a).
l dragLeave: Mouse leave event is triggered when an object (or drag and drop by pressing the ESC key). Generally used for drag and drop after the release of some resources
l dropAccept: drag and drop before the completion of the event with a final opportunity to define data types
l drop: drag and drop event is triggered after the completion. By the drag source through evet.data available to the global variables stored in the data. Such as
if (TextTransfer.getInstance (). isSupportedType (event.currentDataType))
(... / / Read event.data)
else if (FileTransfer.getInstance (). isSupportedType (event. currentDataType))
(... / / Read event.data)