SWT DnD (Drag and drop) Composite : Drag Drop « SWT JFace Eclipse « Java






SWT DnD (Drag and drop) Composite

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.DropTargetEvent;
import org.eclipse.swt.dnd.DropTargetListener;
import org.eclipse.swt.dnd.TextTransfer;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;

public class Ch12DnDComposite extends Composite {

  public Ch12DnDComposite(Composite parent) {
    super(parent, SWT.NONE);

    FillLayout layout = new FillLayout();
    setLayout(layout);

    Text leftText = new Text(this, SWT.MULTI);
    Text rightText = new Text(this, SWT.MULTI);

    createDragSource(leftText);
    createDragSource(rightText);

    createDropTarget(leftText);
    createDropTarget(rightText);
  }

  private void createDropTarget(final Text targetText) {
    Transfer[] types = new Transfer[] { TextTransfer.getInstance() };
    DropTarget dropTarget = new DropTarget(targetText, DND.DROP_COPY);
    dropTarget.setTransfer(types);

    dropTarget.addDropListener(new DropTargetListener() {

      public void dragEnter(DropTargetEvent event) {
      }

      public void dragLeave(DropTargetEvent event) {
      }

      public void dragOperationChanged(DropTargetEvent event) {
      }

      public void dragOver(DropTargetEvent event) {
      }

      public void drop(DropTargetEvent event) {
        String data = (String) event.data;
        targetText.append(data);
      }

      public void dropAccept(DropTargetEvent event) {
      }
    });
  }

  private void createDragSource(final Text sourceText) {
    Transfer[] types = new Transfer[] { TextTransfer.getInstance() };
    DragSource dragSource = new DragSource(sourceText, DND.DROP_COPY);
    dragSource.setTransfer(types);
    dragSource.addDragListener(new DragSourceListener() {

      public void dragStart(DragSourceEvent event) {
        if (sourceText.getSelectionText().length() > 0) {
          event.doit = true;
        }
      }

      public void dragSetData(DragSourceEvent event) {
        event.data = sourceText.getSelection();
      }

      public void dragFinished(DragSourceEvent event) {
        //do nothing
      }
    });
  }
}

           
       








Related examples in the same category

1.SWT DND (Drag and Drop) comprehensive Example SWT DND (Drag and Drop) comprehensive Example
2.Word JumblesWord Jumbles
3.Illustrates draggingIllustrates dragging
4.Simple DND (Drag and Drop) ExampleSimple DND (Drag and Drop) Example
5.Drag and Drop: determine native data types available (motif only)
6.Drag and Drop: determine data types available (win32 only)Drag and Drop: determine data types available (win32 only)
7.Drag and Drop example snippet: define a default operation (in this example, Copy)Drag and Drop example snippet: define a default operation (in this example, Copy)
8.Drag and Drop example snippet: define my own data transfer typeDrag and Drop example snippet: define my own data transfer type
9.Drag and Drop example snippet: drag leaf items in a treeDrag and Drop example snippet: drag leaf items in a tree
10.Drag and Drop example snippet: drag text between two labelsDrag and Drop example snippet: drag text between two labels