Exemplo n.º 1
0
  public static void setDragDrop(final Text text) {
    Transfer[] types = new Transfer[] {TextTransfer.getInstance()};
    int operations = DND.DROP_MOVE | DND.DROP_COPY | DND.DROP_LINK;

    final DragSource source = new DragSource(text, operations);
    source.setTransfer(types);
    source.addDragListener(
        new DragSourceListener() {
          Point selection;

          @Override
          public void dragStart(DragSourceEvent e) {
            selection = text.getSelection();
            e.doit = selection.x != selection.y;
          }

          @Override
          public void dragSetData(DragSourceEvent e) {
            e.data = text.getText(selection.x, selection.y - 1);
          }

          @Override
          public void dragFinished(DragSourceEvent e) {
            if (e.detail == DND.DROP_MOVE) {
              text.setSelection(selection);
              text.insert("");
            }
            selection = null;
          }
        });

    DropTarget target = new DropTarget(text, operations);
    target.setTransfer(types);
    target.addDropListener(
        new DropTargetAdapter() {
          @Override
          public void drop(DropTargetEvent event) {
            if (event.data == null) {
              event.detail = DND.DROP_NONE;
              return;
            }
            text.append((String) event.data);
          }
        });
  }