Java Tutorial/SWT/SWT Drag Drop

Материал из Java эксперт
Перейти к: навигация, поиск

17. Drag and Drop: define a default operation (in this example, Copy)

/*******************************************************************************
 * Copyright (c) 2000, 2004 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
/*
 * Drag and Drop example snippet: define a default operation (in this example, Copy)
 *
 * For a list of all SWT example snippets see
 * http://www.eclipse.org/swt/snippets/
 */
import org.eclipse.swt.SWT;
import org.eclipse.swt.dnd.DND;
import org.eclipse.swt.dnd.DragSource;
import org.eclipse.swt.dnd.DragSourceAdapter;
import org.eclipse.swt.dnd.DragSourceEvent;
import org.eclipse.swt.dnd.DropTarget;
import org.eclipse.swt.dnd.DropTargetAdapter;
import org.eclipse.swt.dnd.DropTargetEvent;
import org.eclipse.swt.dnd.TextTransfer;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class DragDropOperation {
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    final Label label = new Label(shell, SWT.BORDER);
    label.setText("Drag Source");
    DragSource source = new DragSource(label, DND.DROP_COPY | DND.DROP_MOVE | DND.DROP_LINK);
    source.setTransfer(new Transfer[] { TextTransfer.getInstance() });
    source.addDragListener(new DragSourceAdapter() {
      public void dragSetData(DragSourceEvent event) {
        event.data = "Text Transferred";
      }
      public void dragFinished(DragSourceEvent event) {
        if (event.doit) {
          String operation = null;
          switch (event.detail) {
          case DND.DROP_MOVE:
            operation = "moved";
            break;
          case DND.DROP_COPY:
            operation = "copied";
            break;
          case DND.DROP_LINK:
            operation = "linked";
            break;
          case DND.DROP_NONE:
            operation = "disallowed";
            break;
          default:
            operation = "unknown";
            break;
          }
          label.setText("Drag Source (data " + operation + ")");
        } else {
          label.setText("Drag Source (drag cancelled)");
        }
      }
    });
    final Text text = new Text(shell, SWT.BORDER | SWT.MULTI);
    text.setText("Drop Target");
    DropTarget target = new DropTarget(text, DND.DROP_DEFAULT | DND.DROP_COPY | DND.DROP_MOVE
        | DND.DROP_LINK);
    target.setTransfer(new Transfer[] { TextTransfer.getInstance() });
    target.addDropListener(new DropTargetAdapter() {
      public void dragEnter(DropTargetEvent event) {
        if (event.detail == DND.DROP_DEFAULT)
          event.detail = DND.DROP_COPY;
      }
      public void dragOperationChanged(DropTargetEvent event) {
        if (event.detail == DND.DROP_DEFAULT)
          event.detail = DND.DROP_COPY;
      }
      public void drop(DropTargetEvent event) {
        String operation = null;
        switch (event.detail) {
        case DND.DROP_MOVE:
          operation = "moved";
          break;
        case DND.DROP_COPY:
          operation = "copied";
          break;
        case DND.DROP_LINK:
          operation = "linked";
          break;
        case DND.DROP_NONE:
          operation = "disallowed";
          break;
        default:
          operation = "unknown";
          break;
        }
        text.append("\n" + operation + (String) event.data);
      }
    });
    shell.setSize(400, 400);
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
}





17. Drag and Drop: define my own data transfer type

/*******************************************************************************
 * Copyright (c) 2000, 2004 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
/*
 * Drag and Drop example snippet: define my own data transfer type
 *
 * For a list of all SWT example snippets see
 * http://www.eclipse.org/swt/snippets/
 * 
 * @since 3.1
 */
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import org.eclipse.swt.SWT;
import org.eclipse.swt.dnd.ByteArrayTransfer;
import org.eclipse.swt.dnd.DND;
import org.eclipse.swt.dnd.DragSource;
import org.eclipse.swt.dnd.DragSourceAdapter;
import org.eclipse.swt.dnd.DragSourceEvent;
import org.eclipse.swt.dnd.DropTarget;
import org.eclipse.swt.dnd.DropTargetAdapter;
import org.eclipse.swt.dnd.DropTargetEvent;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.dnd.TransferData;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
public class DataTransfer {
  /*
   * The data being transferred is an <bold>array of type MyType</bold> where
   * MyType is define as:
   */
  static class MyType {
    String fileName;
    long fileLength;
    long lastModified;
  }
  static class MyTransfer extends ByteArrayTransfer {
    private static final String MYTYPENAME = "name_for_my_type";
    private static final int MYTYPEID = registerType(MYTYPENAME);
    private static MyTransfer _instance = new MyTransfer();
    public static MyTransfer getInstance() {
      return _instance;
    }
    public void javaToNative(Object object, TransferData transferData) {
      if (!checkMyType(object) || !isSupportedType(transferData)) {
        DND.error(DND.ERROR_INVALID_DATA);
      }
      MyType[] myTypes = (MyType[]) object;
      try {
        // write data to a byte array and then ask super to convert to pMedium
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        DataOutputStream writeOut = new DataOutputStream(out);
        for (int i = 0, length = myTypes.length; i < length; i++) {
          byte[] buffer = myTypes[i].fileName.getBytes();
          writeOut.writeInt(buffer.length);
          writeOut.write(buffer);
          writeOut.writeLong(myTypes[i].fileLength);
          writeOut.writeLong(myTypes[i].lastModified);
        }
        byte[] buffer = out.toByteArray();
        writeOut.close();
        super.javaToNative(buffer, transferData);
      } catch (IOException e) {
      }
    }
    public Object nativeToJava(TransferData transferData) {
      if (isSupportedType(transferData)) {
        byte[] buffer = (byte[]) super.nativeToJava(transferData);
        if (buffer == null)
          return null;
        MyType[] myData = new MyType[0];
        try {
          ByteArrayInputStream in = new ByteArrayInputStream(buffer);
          DataInputStream readIn = new DataInputStream(in);
          while (readIn.available() > 20) {
            MyType datum = new MyType();
            int size = readIn.readInt();
            byte[] name = new byte[size];
            readIn.read(name);
            datum.fileName = new String(name);
            datum.fileLength = readIn.readLong();
            datum.lastModified = readIn.readLong();
            MyType[] newMyData = new MyType[myData.length + 1];
            System.arraycopy(myData, 0, newMyData, 0, myData.length);
            newMyData[myData.length] = datum;
            myData = newMyData;
          }
          readIn.close();
        } catch (IOException ex) {
          return null;
        }
        return myData;
      }
      return null;
    }
    protected String[] getTypeNames() {
      return new String[] { MYTYPENAME };
    }
    protected int[] getTypeIds() {
      return new int[] { MYTYPEID };
    }
    boolean checkMyType(Object object) {
      if (object == null || !(object instanceof MyType[]) || ((MyType[]) object).length == 0) {
        return false;
      }
      MyType[] myTypes = (MyType[]) object;
      for (int i = 0; i < myTypes.length; i++) {
        if (myTypes[i] == null || myTypes[i].fileName == null || myTypes[i].fileName.length() == 0) {
          return false;
        }
      }
      return true;
    }
    protected boolean validate(Object object) {
      return checkMyType(object);
    }
  }
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    final Label label1 = new Label(shell, SWT.BORDER | SWT.WRAP);
    label1.setText("Drag Source for MyData[]");
    final Label label2 = new Label(shell, SWT.BORDER | SWT.WRAP);
    label2.setText("Drop Target for MyData[]");
    DragSource source = new DragSource(label1, DND.DROP_COPY);
    source.setTransfer(new Transfer[] { MyTransfer.getInstance() });
    source.addDragListener(new DragSourceAdapter() {
      public void dragSetData(DragSourceEvent event) {
        MyType myType1 = new MyType();
        myType1.fileName = "C:\\abc.txt";
        myType1.fileLength = 1000;
        myType1.lastModified = 12312313;
        MyType myType2 = new MyType();
        myType2.fileName = "C:\\xyz.txt";
        myType2.fileLength = 500;
        myType2.lastModified = 12312323;
        event.data = new MyType[] { myType1, myType2 };
      }
    });
    DropTarget target = new DropTarget(label2, DND.DROP_COPY | DND.DROP_DEFAULT);
    target.setTransfer(new Transfer[] { MyTransfer.getInstance() });
    target.addDropListener(new DropTargetAdapter() {
      public void dragEnter(DropTargetEvent event) {
        if (event.detail == DND.DROP_DEFAULT) {
          event.detail = DND.DROP_COPY;
        }
      }
      public void dragOperationChanged(DropTargetEvent event) {
        if (event.detail == DND.DROP_DEFAULT) {
          event.detail = DND.DROP_COPY;
        }
      }
      public void drop(DropTargetEvent event) {
        if (event.data != null) {
          MyType[] myTypes = (MyType[]) event.data;
          if (myTypes != null) {
            String string = "";
            for (int i = 0; i < myTypes.length; i++) {
              string += myTypes[i].fileName + " ";
            }
            label2.setText(string);
          }
        }
      }
    });
    shell.setSize(200, 200);
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
}





17. Drag and Drop: determine native data types available (motif only)

/*******************************************************************************
 * Copyright (c) 2000, 2004 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
//package org.eclipse.swt.snippets;
/*
 * Drag and Drop example snippet: determine native data types available (motif only)
 * NOTE: This snippet uses internal SWT packages that are
 * subject to change without notice.
 *
 * For a list of all SWT example snippets see
 * http://www.eclipse.org/swt/snippets/
 */
import org.eclipse.swt.*;
import org.eclipse.swt.dnd.*;
import org.eclipse.swt.internal.*;
import org.eclipse.swt.internal.motif.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
public class Snippet158 extends ByteArrayTransfer {
  private static Snippet158 _instance = new Snippet158();
  private int[] ids;
  private String[] names;
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    final Table control = new Table(shell, SWT.NONE);
    TableItem item = new TableItem(control, SWT.NONE);
    item.setText("Drag data over this site to see the native transfer type.");
    DropTarget target = new DropTarget(control, DND.DROP_DEFAULT | DND.DROP_COPY | DND.DROP_LINK
        | DND.DROP_MOVE);
    target.setTransfer(new Transfer[] { Snippet158.getInstance() });
    target.addDropListener(new DropTargetAdapter() {
      public void dragEnter(DropTargetEvent event) {
        String ops = "";
        if ((event.operations & DND.DROP_COPY) != 0)
          ops += "Copy;";
        if ((event.operations & DND.DROP_MOVE) != 0)
          ops += "Move;";
        if ((event.operations & DND.DROP_LINK) != 0)
          ops += "Link;";
        control.removeAll();
        TableItem item1 = new TableItem(control, SWT.NONE);
        item1.setText("Allowed Operations are " + ops);
        if (event.detail == DND.DROP_DEFAULT) {
          if ((event.operations & DND.DROP_COPY) != 0) {
            event.detail = DND.DROP_COPY;
          } else if ((event.operations & DND.DROP_LINK) != 0) {
            event.detail = DND.DROP_LINK;
          } else if ((event.operations & DND.DROP_MOVE) != 0) {
            event.detail = DND.DROP_MOVE;
          }
        }
        TransferData[] data = event.dataTypes;
        for (int i = 0; i < data.length; i++) {
          int id = data[i].type;
          String name = getNameFromId(id);
          TableItem item2 = new TableItem(control, SWT.NONE);
          item2.setText("Data type is " + id + " " + name);
        }
      }
    });
    shell.setSize(400, 400);
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
  public static Snippet158 getInstance() {
    return _instance;
  }
  Snippet158() {
  }
  public void javaToNative(Object object, TransferData transferData) {
  }
  public Object nativeToJava(TransferData transferData) {
    return "Hello World";
  }
  protected String[] getTypeNames() {
    return names;
  }
  static int shellHandle;
  protected int[] getTypeIds() {
    if (ids == null) {
      Display display = Display.getCurrent();
      int widgetClass = OS.topLevelShellWidgetClass();
      shellHandle = OS.XtAppCreateShell(null, null, widgetClass, display.xDisplay, null, 0);
      OS.XtSetMappedWhenManaged(shellHandle, false);
      OS.XtRealizeWidget(shellHandle);
      ids = new int[840];
      names = new String[840];
      for (int i = 0; i < ids.length; i++) {
        ids[i] = i + i;
        names[i] = getNameFromId(i + 1);
      }
    }
    return ids;
  }
  static String getNameFromId(int id) {
    int xDisplay = OS.XtDisplay(shellHandle);
    int ptr = 0;
    try {
      ptr = OS.XmGetAtomName(xDisplay, id);
    } catch (Throwable t) {
    }
    if (ptr == 0)
      return "invalid " + id;
    int length = OS.strlen(ptr);
    byte[] nameBuf = new byte[length];
    OS.memmove(nameBuf, ptr, length);
    OS.XFree(ptr);
    return new String(Converter.mbcsToWcs(null, nameBuf)).toLowerCase();
  }
}





17. Drag and Drop example snippet: determine data types available (win32 only)

/*******************************************************************************
 * Copyright (c) 2000, 2004 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
//package org.eclipse.swt.snippets;
/*
 * Drag and Drop example snippet: determine data types available (win32 only)
 * NOTE: This snippet uses internal SWT packages that are
 * subject to change without notice.
 *
 * For a list of all SWT example snippets see
 * http://www.eclipse.org/swt/snippets/
 */
import org.eclipse.swt.SWT;
import org.eclipse.swt.dnd.ByteArrayTransfer;
import org.eclipse.swt.dnd.DND;
import org.eclipse.swt.dnd.DropTarget;
import org.eclipse.swt.dnd.DropTargetAdapter;
import org.eclipse.swt.dnd.DropTargetEvent;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.dnd.TransferData;
import org.eclipse.swt.internal.ole.win32.ru;
import org.eclipse.swt.internal.win32.TCHAR;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;
public class Snippet83 extends ByteArrayTransfer {
  private static Snippet83 _instance = new Snippet83();
  private int[] ids;
  private String[] names;
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    final Table control = new Table(shell, SWT.NONE);
    TableItem item = new TableItem(control, SWT.NONE);
    item.setText("Drag data over this site to see the native transfer type.");
    DropTarget target = new DropTarget(control, DND.DROP_DEFAULT | DND.DROP_COPY | DND.DROP_LINK
        | DND.DROP_MOVE);
    target.setTransfer(new Transfer[] { Snippet83.getInstance() });
    target.addDropListener(new DropTargetAdapter() {
      public void dragEnter(DropTargetEvent event) {
        String ops = "";
        if ((event.operations & DND.DROP_COPY) != 0)
          ops += "Copy;";
        if ((event.operations & DND.DROP_MOVE) != 0)
          ops += "Move;";
        if ((event.operations & DND.DROP_LINK) != 0)
          ops += "Link;";
        control.removeAll();
        TableItem item1 = new TableItem(control, SWT.NONE);
        item1.setText("Allowed Operations are " + ops);
        if (event.detail == DND.DROP_DEFAULT) {
          if ((event.operations & DND.DROP_COPY) != 0) {
            event.detail = DND.DROP_COPY;
          } else if ((event.operations & DND.DROP_LINK) != 0) {
            event.detail = DND.DROP_LINK;
          } else if ((event.operations & DND.DROP_MOVE) != 0) {
            event.detail = DND.DROP_MOVE;
          }
        }
        TransferData[] data = event.dataTypes;
        for (int i = 0; i < data.length; i++) {
          int id = data[i].type;
          String name = getNameFromId(id);
          TableItem item2 = new TableItem(control, SWT.NONE);
          item2.setText("Data type is " + id + " " + name);
        }
      }
    });
    shell.setSize(400, 400);
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
  public static Snippet83 getInstance() {
    return _instance;
  }
  Snippet83() {
    ids = new int[80000];
    names = new String[80000];
    for (int i = 0; i < ids.length; i++) {
      ids[i] = i;
      names[i] = getNameFromId(i);
    }
  }
  public void javaToNative(Object object, TransferData transferData) {
  }
  public Object nativeToJava(TransferData transferData) {
    return "Hello World";
  }
  protected String[] getTypeNames() {
    return names;
  }
  protected int[] getTypeIds() {
    return ids;
  }
  static String getNameFromId(int id) {
    String name = null;
    int maxSize = 128;
    TCHAR buffer = new TCHAR(0, maxSize);
    int size = COM.GetClipboardFormatName(id, buffer, maxSize);
    if (size != 0) {
      name = buffer.toString(0, size);
    } else {
      switch (id) {
      case COM.CF_HDROP:
        name = "CF_HDROP";
        break;
      case COM.CF_TEXT:
        name = "CF_TEXT";
        break;
      case COM.CF_BITMAP:
        name = "CF_BITMAP";
        break;
      case COM.CF_METAFILEPICT:
        name = "CF_METAFILEPICT";
        break;
      case COM.CF_SYLK:
        name = "CF_SYLK";
        break;
      case COM.CF_DIF:
        name = "CF_DIF";
        break;
      case COM.CF_TIFF:
        name = "CF_TIFF";
        break;
      case COM.CF_OEMTEXT:
        name = "CF_OEMTEXT";
        break;
      case COM.CF_DIB:
        name = "CF_DIB";
        break;
      case COM.CF_PALETTE:
        name = "CF_PALETTE";
        break;
      case COM.CF_PENDATA:
        name = "CF_PENDATA";
        break;
      case COM.CF_RIFF:
        name = "CF_RIFF";
        break;
      case COM.CF_WAVE:
        name = "CF_WAVE";
        break;
      case COM.CF_UNICODETEXT:
        name = "CF_UNICODETEXT";
        break;
      case COM.CF_ENHMETAFILE:
        name = "CF_ENHMETAFILE";
        break;
      case COM.CF_LOCALE:
        name = "CF_LOCALE";
        break;
      case COM.CF_MAX:
        name = "CF_MAX";
        break;
      }
    }
    return name;
  }
}





17. Drag and Drop inside Table

import org.eclipse.swt.SWT;
import org.eclipse.swt.dnd.DND;
import org.eclipse.swt.dnd.DragSource;
import org.eclipse.swt.dnd.DragSourceAdapter;
import org.eclipse.swt.dnd.DragSourceEvent;
import org.eclipse.swt.dnd.DropTarget;
import org.eclipse.swt.dnd.DropTargetAdapter;
import org.eclipse.swt.dnd.DropTargetEvent;
import org.eclipse.swt.dnd.TextTransfer;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
public class DragDropTable {
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    
    Table table = new Table(shell, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
    Transfer[] types = new Transfer[] { TextTransfer.getInstance() };
    DragSource source = new DragSource(table, DND.DROP_MOVE | DND.DROP_COPY);
    source.setTransfer(types);
    
    source.addDragListener(new DragSourceAdapter() {
      public void dragSetData(DragSourceEvent event) {
        // Get the selected items in the drag source
        DragSource ds = (DragSource) event.widget;
        Table table = (Table) ds.getControl();
        TableItem[] selection = table.getSelection();
        StringBuffer buff = new StringBuffer();
        for (int i = 0, n = selection.length; i < n; i++) {
          buff.append(selection[i].getText());
        }
         event.data = buff.toString();
      }
    });
    // Create the drop target
    DropTarget target = new DropTarget(table, DND.DROP_MOVE | DND.DROP_COPY | DND.DROP_DEFAULT);
    target.setTransfer(types);
    target.addDropListener(new DropTargetAdapter() {
      public void dragEnter(DropTargetEvent event) {
        if (event.detail == DND.DROP_DEFAULT) {
          event.detail = (event.operations & DND.DROP_COPY) != 0 ? DND.DROP_COPY : DND.DROP_NONE;
        }
        // Allow dropping text only
        for (int i = 0, n = event.dataTypes.length; i < n; i++) {
          if (TextTransfer.getInstance().isSupportedType(event.dataTypes[i])) {
            event.currentDataType = event.dataTypes[i];
          }
        }
      }
      public void dragOver(DropTargetEvent event) {
         event.feedback = DND.FEEDBACK_SELECT | DND.FEEDBACK_SCROLL;
      }
      public void drop(DropTargetEvent event) {
        if (TextTransfer.getInstance().isSupportedType(event.currentDataType)) {
          // Get the dropped data
          DropTarget target = (DropTarget) event.widget;
          Table table = (Table) target.getControl();
          String data = (String) event.data;
          // Create a new item in the table to hold the dropped data
          TableItem item = new TableItem(table, SWT.NONE);
          item.setText(new String[] { data });
          table.redraw();
        }
      }
    });
    TableColumn column = new TableColumn(table, SWT.NONE);
    // Seed the table
    TableItem item = new TableItem(table, SWT.NONE);
    item.setText(new String[] { "A" });
    item = new TableItem(table, SWT.NONE);
    item.setText(new String[] { "B" });
    item = new TableItem(table, SWT.BORDER);
    item.setText(new String[] { "C" });
    column.pack();
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}





17. Dragging and Dropping

  1. The component you drag from is called the drag source.
  2. The component you drop on is called the drop target.

SWT uses the DragSource class to represent drag sources. It offers a single constructor:



DragSource(Control control, int style)





17. Drag leaf items in a tree

/*******************************************************************************
 * Copyright (c) 2000, 2004 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
/*
 * Drag and Drop example snippet: drag leaf items in a tree
 *
 * For a list of all SWT example snippets see
 * http://www.eclipse.org/swt/snippets/
 */
import org.eclipse.swt.SWT;
import org.eclipse.swt.dnd.DND;
import org.eclipse.swt.dnd.DragSource;
import org.eclipse.swt.dnd.DragSourceEvent;
import org.eclipse.swt.dnd.DragSourceListener;
import org.eclipse.swt.dnd.DropTarget;
import org.eclipse.swt.dnd.DropTargetAdapter;
import org.eclipse.swt.dnd.DropTargetEvent;
import org.eclipse.swt.dnd.TextTransfer;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
public class DragTreeLeaf {
  public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    final Tree tree = new Tree(shell, SWT.BORDER);
    for (int i = 0; i < 3; i++) {
      TreeItem item = new TreeItem(tree, SWT.NONE);
      item.setText("item " + i);
      for (int j = 0; j < 3; j++) {
        TreeItem subItem = new TreeItem(item, SWT.NONE);
        subItem.setText("item " + i + " " + j);
        for (int k = 0; k < 3; k++) {
          TreeItem subsubItem = new TreeItem(subItem, SWT.NONE);
          subsubItem.setText("item " + i + " " + j + " " + k);
        }
      }
    }
    Transfer[] types = new Transfer[] { TextTransfer.getInstance() };
    int operations = DND.DROP_MOVE | DND.DROP_COPY | DND.DROP_LINK;
    final DragSource source = new DragSource(tree, operations);
    source.setTransfer(types);
    final TreeItem[] dragSourceItem = new TreeItem[1];
    source.addDragListener(new DragSourceListener() {
      public void dragStart(DragSourceEvent event) {
        TreeItem[] selection = tree.getSelection();
        if (selection.length > 0 && selection[0].getItemCount() == 0) {
          event.doit = true;
          dragSourceItem[0] = selection[0];
        } else {
          event.doit = false;
        }
      };
      public void dragSetData(DragSourceEvent event) {
        event.data = dragSourceItem[0].getText();
      }
      public void dragFinished(DragSourceEvent event) {
        if (event.detail == DND.DROP_MOVE)
          dragSourceItem[0].dispose();
        dragSourceItem[0] = null;
      }
    });
    DropTarget target = new DropTarget(tree, operations);
    target.setTransfer(types);
    target.addDropListener(new DropTargetAdapter() {
      public void dragOver(DropTargetEvent event) {
        event.feedback = DND.FEEDBACK_EXPAND | DND.FEEDBACK_SCROLL;
        if (event.item != null) {
          TreeItem item = (TreeItem) event.item;
          Point pt = display.map(null, tree, event.x, event.y);
          Rectangle bounds = item.getBounds();
          if (pt.y < bounds.y + bounds.height / 3) {
            event.feedback |= DND.FEEDBACK_INSERT_BEFORE;
          } else if (pt.y > bounds.y + 2 * bounds.height / 3) {
            event.feedback |= DND.FEEDBACK_INSERT_AFTER;
          } else {
            event.feedback |= DND.FEEDBACK_SELECT;
          }
        }
      }
      public void drop(DropTargetEvent event) {
        if (event.data == null) {
          event.detail = DND.DROP_NONE;
          return;
        }
        String text = (String) event.data;
        if (event.item == null) {
          TreeItem item = new TreeItem(tree, SWT.NONE);
          item.setText(text);
        } else {
          TreeItem item = (TreeItem) event.item;
          Point pt = display.map(null, tree, event.x, event.y);
          Rectangle bounds = item.getBounds();
          TreeItem parent = item.getParentItem();
          if (parent != null) {
            TreeItem[] items = parent.getItems();
            int index = 0;
            for (int i = 0; i < items.length; i++) {
              if (items[i] == item) {
                index = i;
                break;
              }
            }
            if (pt.y < bounds.y + bounds.height / 3) {
              TreeItem newItem = new TreeItem(parent, SWT.NONE, index);
              newItem.setText(text);
            } else if (pt.y > bounds.y + 2 * bounds.height / 3) {
              TreeItem newItem = new TreeItem(parent, SWT.NONE, index + 1);
              newItem.setText(text);
            } else {
              TreeItem newItem = new TreeItem(item, SWT.NONE);
              newItem.setText(text);
            }
          } else {
            TreeItem[] items = tree.getItems();
            int index = 0;
            for (int i = 0; i < items.length; i++) {
              if (items[i] == item) {
                index = i;
                break;
              }
            }
            if (pt.y < bounds.y + bounds.height / 3) {
              TreeItem newItem = new TreeItem(tree, SWT.NONE, index);
              newItem.setText(text);
            } else if (pt.y > bounds.y + 2 * bounds.height / 3) {
              TreeItem newItem = new TreeItem(tree, SWT.NONE, index + 1);
              newItem.setText(text);
            } else {
              TreeItem newItem = new TreeItem(item, SWT.NONE);
              newItem.setText(text);
            }
          }
        }
      }
    });
    shell.setSize(400, 400);
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
}





17. Drag selected text in Text to Label

import org.eclipse.swt.SWT;
import org.eclipse.swt.dnd.DND;
import org.eclipse.swt.dnd.DragSource;
import org.eclipse.swt.dnd.DragSourceAdapter;
import org.eclipse.swt.dnd.DragSourceEvent;
import org.eclipse.swt.dnd.DropTarget;
import org.eclipse.swt.dnd.DropTargetAdapter;
import org.eclipse.swt.dnd.DropTargetEvent;
import org.eclipse.swt.dnd.TextTransfer;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class DragTextToLabel {
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    
    Text text = new Text(shell, SWT.BORDER|SWT.SINGLE);
    Transfer[] types = new Transfer[] { TextTransfer.getInstance() };
    DragSource source = new DragSource(text, DND.DROP_MOVE | DND.DROP_COPY);
    source.setTransfer(types);
    
    source.addDragListener(new DragSourceAdapter() {
      public void dragSetData(DragSourceEvent event) {
        // Get the selected items in the drag source
        DragSource ds = (DragSource) event.widget;
        Text text = (Text) ds.getControl();
        event.data = text.getSelectionText();
      }
    });
    Label label = new Label(shell, SWT.BORDER);
    // Create the drop target
    DropTarget target = new DropTarget(label, DND.DROP_MOVE | DND.DROP_COPY | DND.DROP_DEFAULT);
    target.setTransfer(types);
    target.addDropListener(new DropTargetAdapter() {
      public void dragEnter(DropTargetEvent event) {
        if (event.detail == DND.DROP_DEFAULT) {
          event.detail = (event.operations & DND.DROP_COPY) != 0 ? DND.DROP_COPY : DND.DROP_NONE;
        }
        // Allow dropping text only
        for (int i = 0, n = event.dataTypes.length; i < n; i++) {
          if (TextTransfer.getInstance().isSupportedType(event.dataTypes[i])) {
            event.currentDataType = event.dataTypes[i];
          }
        }
      }
      public void dragOver(DropTargetEvent event) {
         event.feedback = DND.FEEDBACK_SELECT | DND.FEEDBACK_SCROLL;
      }
      public void drop(DropTargetEvent event) {
        if (TextTransfer.getInstance().isSupportedType(event.currentDataType)) {
          // Get the dropped data
          DropTarget target = (DropTarget) event.widget;
          Label label = (Label) target.getControl();
          String data = (String) event.data;
          label.setText(data);
          label.redraw();
        }
      }
    });
    text.setBounds(10,10,100,25);
    label.setBounds(10,55,100,25);
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}





17. Drag text between two labels

/*******************************************************************************
 * Copyright (c) 2000, 2004 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
/*
 * Drag and Drop example snippet: drag text between two labels
 *
 * For a list of all SWT example snippets see
 * http://www.eclipse.org/swt/snippets/
 */
import org.eclipse.swt.SWT;
import org.eclipse.swt.dnd.DND;
import org.eclipse.swt.dnd.DragSource;
import org.eclipse.swt.dnd.DragSourceEvent;
import org.eclipse.swt.dnd.DragSourceListener;
import org.eclipse.swt.dnd.DropTarget;
import org.eclipse.swt.dnd.DropTargetAdapter;
import org.eclipse.swt.dnd.DropTargetEvent;
import org.eclipse.swt.dnd.TextTransfer;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
public class DragDropBetweenLabels {
  public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    final Label label1 = new Label(shell, SWT.BORDER);
    label1.setText("TEXT");
    final Label label2 = new Label(shell, SWT.BORDER);
    setDragDrop(label1);
    setDragDrop(label2);
    shell.setSize(200, 200);
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
  public static void setDragDrop(final Label label) {
    Transfer[] types = new Transfer[] { TextTransfer.getInstance() };
    int operations = DND.DROP_MOVE | DND.DROP_COPY | DND.DROP_LINK;
    final DragSource source = new DragSource(label, operations);
    source.setTransfer(types);
    source.addDragListener(new DragSourceListener() {
      public void dragStart(DragSourceEvent event) {
        event.doit = (label.getText().length() != 0);
      }
      public void dragSetData(DragSourceEvent event) {
        event.data = label.getText();
      }
      public void dragFinished(DragSourceEvent event) {
        if (event.detail == DND.DROP_MOVE)
          label.setText("");
      }
    });
    DropTarget target = new DropTarget(label, operations);
    target.setTransfer(types);
    target.addDropListener(new DropTargetAdapter() {
      public void drop(DropTargetEvent event) {
        if (event.data == null) {
          event.detail = DND.DROP_NONE;
          return;
        }
        label.setText((String) event.data);
      }
    });
  }
}





17. Make a dropped data type depend on a target item in table

/*******************************************************************************
 * Copyright (c) 2000, 2005 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
//package org.eclipse.swt.snippets;
/*
 * Make a dropped data type depend on a target item in table
 * 
 * For a list of all SWT example snippets see
 * http://www.eclipse.org/swt/snippets/
 */
import java.io.File;
import org.eclipse.swt.SWT;
import org.eclipse.swt.dnd.DND;
import org.eclipse.swt.dnd.DragSource;
import org.eclipse.swt.dnd.DragSourceAdapter;
import org.eclipse.swt.dnd.DragSourceEvent;
import org.eclipse.swt.dnd.DropTarget;
import org.eclipse.swt.dnd.DropTargetAdapter;
import org.eclipse.swt.dnd.DropTargetEvent;
import org.eclipse.swt.dnd.FileTransfer;
import org.eclipse.swt.dnd.TextTransfer;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;
public class DropTypeOnTable {
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    Label label1 = new Label(shell, SWT.BORDER);
    label1.setText("Drag Source");
    final Table table = new Table(shell, SWT.BORDER);
    for (int i = 0; i < 4; i++) {
      TableItem item = new TableItem(table, SWT.NONE);
      if (i % 2 == 0)
        item.setText("Drop a file");
      if (i % 2 == 1)
        item.setText("Drop text");
    }
    DragSource dragSource = new DragSource(label1, DND.DROP_COPY);
    dragSource
        .setTransfer(new Transfer[] { TextTransfer.getInstance(), FileTransfer.getInstance() });
    dragSource.addDragListener(new DragSourceAdapter() {
      public void dragSetData(DragSourceEvent event) {
        if (FileTransfer.getInstance().isSupportedType(event.dataType)) {
          File file = new File("temp");
          event.data = new String[] { file.getAbsolutePath() };
        }
        if (TextTransfer.getInstance().isSupportedType(event.dataType)) {
          event.data = "once upon a time";
        }
      }
    });
    DropTarget dropTarget = new DropTarget(table, DND.DROP_COPY | DND.DROP_DEFAULT);
    dropTarget
        .setTransfer(new Transfer[] { TextTransfer.getInstance(), FileTransfer.getInstance() });
    dropTarget.addDropListener(new DropTargetAdapter() {
      FileTransfer fileTransfer = FileTransfer.getInstance();
      TextTransfer textTransfer = TextTransfer.getInstance();
      public void dragEnter(DropTargetEvent event) {
        if (event.detail == DND.DROP_DEFAULT)
          event.detail = DND.DROP_COPY;
      }
      public void dragOperationChanged(DropTargetEvent event) {
        if (event.detail == DND.DROP_DEFAULT)
          event.detail = DND.DROP_COPY;
      }
      public void dragOver(DropTargetEvent event) {
        event.detail = DND.DROP_NONE;
        TableItem item = (TableItem) event.item;
        if (item == null)
          return;
        int itemIndex = table.indexOf(item);
        if (itemIndex % 2 == 0) {
          int index = 0;
          while (index < event.dataTypes.length) {
            if (fileTransfer.isSupportedType(event.dataTypes[index]))
              break;
            index++;
          }
          if (index < event.dataTypes.length) {
            event.currentDataType = event.dataTypes[index];
            event.detail = DND.DROP_COPY;
            return;
          }
        } else {
          int index = 0;
          while (index < event.dataTypes.length) {
            if (textTransfer.isSupportedType(event.dataTypes[index]))
              break;
            index++;
          }
          if (index < event.dataTypes.length) {
            event.currentDataType = event.dataTypes[index];
            event.detail = DND.DROP_COPY;
            return;
          }
        }
      }
      public void drop(DropTargetEvent event) {
        TableItem item = (TableItem) event.item;
        if (item == null) {
          event.detail = DND.DROP_NONE;
          return;
        }
        if (fileTransfer.isSupportedType(event.currentDataType)) {
          String[] files = (String[]) event.data;
          if (files != null && files.length > 0) {
            item.setText(files[0]);
          }
        }
        if (textTransfer.isSupportedType(event.currentDataType)) {
          String text = (String) event.data;
          if (text != null) {
            item.setText(text);
          }
        }
      }
    });
    shell.setSize(300, 150);
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
}