[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [Newsgroup Home]
[news.eclipse.platform.swt] Re: DND with two different costum classes

A control can only have one DropTarget. That's why you get the SWTError when you tried to add multiple targets.

You are right, validate() is only used by the Clipboard and not during a drag and drop.

The source can offer several transfer types and the target can request for any of those types provided by the source. In your code, you never asked for any particular transfer type so the source will give you one by default.

The target can ask for the type of data being transferred by setting the event.currentDataType in the dragEnter, dragAccept or dragOver methods.

In your TreeDropTargetAdapter, implement a dragAccept method where you would set the datatype to be a transfer type of your choice. For example:

public void dropAccept(DropTargetEvent event) {
  event.currentDataType = event.dataTypes[0];
}

Your drop method will need to extract data based on the correct datatype as you already have done in your example.

You can see Snippet185 for a more complete example.

Duong

Moritz Kreutzer wrote:

In Snippet 171 this is solved with two different DropTargets with one Transfer type per DropTarget. If I try to do this, I got an SWTError ("Cannot initialize Drop"). I think, this is because I want to initialize multiple DropTargets on _one_ Control (a Tree=. (In the Snippets there are also multiple Controls.)
I also tried to implement validate() but this method seems to be never called.

Here is some sample code:

public class TreeDropTargetAdapter extends DropTargetAdapter {
public void drop(DropTargetEvent event) {
if (TreeParentTransfer.getInstance().isSupportedType(event.currentDataType)) {
// add to tree and input
} else if (TreeChildTransfer.getInstance().isSupportedType(event.currentDataType)) {
// add...
}
}
}


//////////////////////////


public class TreeDragSourceAdapter extends DragSourceAdapter {
public void dragSetData(DragSourceEvent event) {
Object dragObj = ((IStructuredSelection)treeViewer.getSelection()).getFirstElement();
if (dragObj instanceof TreeParent)
event.data = new TreeParent[] { (TreeParent) dragObj};
else if (dragObj instanceof TreeChild)
event.data = new TreeChild[] { (TreeChild) dragObj};
}
}


//////////////////////////


public class TreeeView extends ViewPart {
public void createPartControl(Composite parent) {
DropTarget dt = new DropTarget(tree,DND.DROP_COPY | DND.DROP_MOVE | DND.DROP_DEFAULT);
dt.setTransfer(new Transfer[] {TreeParentTransfer.getInstance(), TreeChildTransfer.getInstance() });
dt.addDropListener(new TreeDropTargetAdapter());

DragSource ds = ...

}
}


//////////////////////////


public class TreeParentTransfer extends ByteArrayTransfer {
  private static final String MYTYPENAME = "TREEPARENTTRANSFER";
  private static final int MYTYPEID = registerType(MYTYPENAME);
  private static TreeParentTransfer _instance = new TreeParentTransfer();

  private TreeParentTransfer() {
  }

  public static TemplateTransfer getInstance() {
    return _instance;
  }

  public void javaToNative(Object object, TransferData transferData) {
    if (!checkTemplate(object) || !isSupportedType(transferData)) {
      DND.error(DND.ERROR_INVALID_DATA);
    }
    // convert stuff
  }

  public Object nativeToJava(TransferData transferData) {
    if (!isSupportedType(transferData))
      return null;

    byte[] buffer = (byte[]) super.nativeToJava(transferData);
    if (buffer == null)
      return null;

// convert stuff

    protected String[] getTypeNames() {
      return new String[] { MYTYPENAME };
    }

    protected int[] getTypeIds() {
      return new int[] { MYTYPEID };
    }

    private boolean checkTemplate(Object obj) {
      return (obj != null && obj instanceof TreeParent[]);
    }

    protected boolean validate(Object object) {
      return object instanceof TreeParent[];
    }
}


//////////////////////////


// same for TreeChild



I hope this will help you.

Regards, Moritz.