Eclipse drag and drop (Drag and Drop) (change)

2010-04-01  来源:本站原创  分类:Java  人气:603 

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);
sourceText.setText ("HelloWolrd");
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 ------------------------
shell.layout (); ();
while (! shell.isDisposed ()) (
if (! display.readAndDispatch ())
display.sleep ();
display.dispose ();
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
return button;
/ / 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); = 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);
button.setText ("");
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, 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) ( = 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); / / transmit data out
DropTarget target = (DropTarget) event.widget;
Text text = (Text) target.getControl ();// that targetText
text.setText (str);
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:// transferred from

-------------------------------------------------- --------------------------------
【】 Http:// 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
else if (FileTransfer.getInstance (). isSupportedType (event.dataType))
(... / / Assign string array to
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:
Transfer [] types = new Transfer [] (TextTransfer.getInstance ()};// and source can be defined as a variety of Tranfer
DropTarget target = new DropTarget (label, operations);
target.setTransfer (types);
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 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 available to the global variables stored in the data. Such as
if (TextTransfer.getInstance (). isSupportedType (event.currentDataType))
(... / / Read
else if (FileTransfer.getInstance (). isSupportedType (event. currentDataType))
(... / / Read

  • Eclipse drag and drop (Drag and Drop) (change) 2010-04-01

    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

  • Flex achieve drawing lines (which can drag and drop. With arrows. Can change color) 2010-05-14

    Based on the change in the previous change, not more than that, look at renderings and paste code: Main program: <?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="" creati

  • js drag and drop drag and drop 2011-01-07

    Good tutorial // iMouseDown represents the current mouse button state: up or down /* lMouseState represents the previous mouse button state so that we can check for button clicks an

  • drop user cascade drop user and the difference between 2010-10-19

    drop user; just delete the user, drop user × × cascade; will delete the user name and view all the tables. user Specify the user to be dropped. Oracle Database does not drop users whose schemas contain objects unless you specify CASCADE or unless you

  • drop user and drop user cascade difference 2010-10-19

    drop user; just delete the user, drop user × × cascade; will delete the user name of all tables and views. user Specify the user to be dropped. Oracle Database does not drop users whose schemas contain objects unless you specify CASCADE or unless you

  • [Change] javascript Drag and Drop 2010-03-30

    <html> <head> <title> Drag-and-drop effect </title> <style> #idContainer{ border:10px solid #990000; width:600px; height:300px; } #idDrag{ border:5px solid #C4E3FD; background:#C4E3FD; width:50px; height:50px; margin:50px; }

  • Flex 3 Getting Started (18): Building an advanced user interface, drag and drop support added 2010-08-26

    In a typical visual development environment, you can select in the application and drag an object moving back and forth on the screen. Flex Drag and Drop Manager allows users to select an object, such as an element of List control, or a control such

  • flex drag and drop operation (b) 2010-11-04

    Easier to make money at home Manually add the drag-and-drop support In the last article in Flex, you already know there are some controls built-in drag-and-drop operations support, and relatively easy to achieve, but often have to complete this opera

  • Flex using drag and drop 2010-09-05

    All Flex components support drag and drop, some subset of the components also drag and drop with other functions, as these components can be achieved adding a property to drag and drop. Achieved in the Flex drag and drop in the use of drag and drop m

  • js to achieve simple drag and drop 2010-09-25

    The simplest implementation of js said drag and drop drag and drop, and now major, medium and small sites are essentially similar things, especially for pop-up layer drag, it is common mess. . . In fact, for pop-up layer, the initial aim is very simp

  • Js popup (supports drag and drop, multiple pop automatically switch level) 2011-01-09

    Js popup Usage: 1, first import the css and js <script type="text/javascript" src="js/PopUpManager-compressed.js" ></script> <link href="css/PopUpLayer.css" type="text/css" rel="stylesheet"/

  • js drag the table out to achieve, to change column width 2010-04-13

    Read some on the table two days dragging out the js code, fear forgotten, Tie Shanglai arrayed. First, this aspect should be better on the compatible, but drag the mouse respond to the events it was not too: <! DOCTYPE html PUBLIC "- / / W3C / / D

  • frame size of the change (the mouse can drag) 2010-06-20

    <frameset rows="5,*" framespacing="0" frameborder="no" border="0"> <frame scrolling="no"style="border-top:#8AB5E8 5px solid;background-color:#8AB5E8;" noresize="noresize"/>

  • + javascript + css (imitating the drag google personalized page layout) 2009-04-14

    Comparing the content of many, the main are javascript code, the code from Tin's are on the google and under get better Translation: His blog And I used the database to preserve the layout of the framework, procedures u

  • jquery image crop preview, simply drag 2010-11-29

    See the online image crop, it is a feeling, based on my own jquery-ui, and then clip it is generally accepted principles, implementation of a simple preview To the entire page, on the whole do not know how. // JavaScript Document var x; var y; var pr

  • JS drag the container tools 2010-08-15

    var isIE = (document.all)? true: false; var $ = function (id) (return "string" == typeof id? document.getElementById (id): id;); var Class = (create: function () (return function () (this.initialize.apply (this, arguments);))) var Extend = funct

  • jquery drag effect layer implementations 2010-11-28

    jquery layer drag effect of the implementation methods: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ""> <html xmlns="

  • SQL Advanced usage constraints, Alter, Create, Drop 2010-06-22

    SQL PRIMARY KEY constraint PRIMARY KEY constraint to uniquely identify each record in a database table. Primary key must contain unique values. Primary key column can not contain NULL values. Each table should have a primary key, and each table can h

  • Oracle Flashback Flashback - restore error DML, DDL (Drop) operation 2010-12-22

    Human error is the failure of the database system is one important reason, according to the survey about 40% of the system is operational errors or user errors caused by human error and these are particularly difficult to avoid. Traditionally taken i

  • DROP TABLESPACE when the data file is automatically deleted 2010-03-29

    DROP TABLESPACE when the data file is automatically deleted OMF OMF and non-management in the DROP TABLESPACE data files will automatically deleted when doing the test: SQL> alter system set db_create_file_dest = '/ oradata / data / standby' scope =