Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Drop Target Not Accepting Drops(No matter what, I get the "no drop" icon.)
Drop Target Not Accepting Drops [message #498870] Wed, 18 November 2009 21:37 Go to next message
Ian Harac is currently offline Ian HaracFriend
Messages: 10
Registered: November 2009
Location: Southern Indiana
Junior Member

I have spent two frustrating days on this and am getting into hair-tearing mode.

I have a Gridviewer (Nebula), and a ListViewer (standard SWT). Gridviewer is set to be the drag source, ListViewer the drop target. Each has had the appropriate method set, ie, addDropSupport, addDragSupport. Dragging from the gridviewer works -- I want it to drag the column name, and that's what it does, no matter where in the column you click.

I have set the transfer types for each to TextTransfer().

If I drag outside of my Java app and onto, say, Microsoft OneNote, I can drop the text, so I am 100% sure the drag is set up properly.

If I drag over the target, I get DragEntered, DragOver,DragLeave messages, but never ever Drop. The drag image is decorated with the "No Drop" icon, but not by any code I have written. I have gone over and over the code to verify everything is, or seems to be, set up properly, that the data is the right type, that the drop target wants that type, etc. However, nothing I do causes it to register a Drop.

I've tried using a ViewerDropAdapter, and this, too, never registers a drop -- the ValidateDrop and PerformDrop methods never get called.

Here's what I think are the relevant code snippets.

Transfer[] types=new Transfer[]{TextTransfer.getInstance()};
//makes the ListViewer
lvActiveFields=new ListViewer(grpAnalysis,SWT.BORDER|SWT.V_SCROLL);
					lvActiveFields.setLabelProvider(new ViewerLabelProvider());
					lvActiveFields.setContentProvider(new ListContentProvider());
					lvActiveFields.setInput(fields);
					listActiveFields=lvActiveFields.getList();
					listActiveFields.setToolTipText("Drag fields of interest to this box.");
					{
						GridData gridData=new GridData(SWT.LEFT,SWT.FILL,false,true,1,1);
						gridData.widthHint=129;
						listActiveFields.setLayoutData(gridData);
					}
					{
						lvActiveFields.addDropSupport(DND.DROP_COPY,types,new DropTargetFieldsDropTargetListener(lvActiveFields));
					}

//the drop listener
//note there's some shotgun debugging code in here in a vain attempt to pin down what's going on...

private class DropTargetFieldsDropTargetListener extends ViewerDropAdapter
	{
		final TextTransfer textTransfer=TextTransfer.getInstance();

		protected DropTargetFieldsDropTargetListener(Viewer viewer)
		{
			super(viewer);
		}

		@Override
		public void dragEnter(DropTargetEvent event)
		{
			System.out.println("Drag Enter");
		}

		@Override
		public void drop(DropTargetEvent event)
		{
			System.out.println("Dropped");
			if (textTransfer.isSupportedType(event.currentDataType))
			{
				Object o=textTransfer.nativeToJava(event.currentDataType);
				String t=(String)o;
				fields.add(t);
				lvActiveFields.refresh();
			}
		}

		@Override
		public void dragOver(DropTargetEvent event)
		{
			System.out.println("Drag Over");
			
			if (textTransfer.isSupportedType(event.currentDataType))
			{
				
				Object o=textTransfer.nativeToJava(event.currentDataType);
				String t=(String)o;
				if (t!=null)
				{
					System.out.println(t);

				}
				else
				{
					System.out.println("t is null.");
				}
			}
		}

		@Override
		public void dropAccept(DropTargetEvent arg0)
		{
			System.out.println("Drop Accept");
		}

		@Override
		public void dragLeave(DropTargetEvent arg0)
		{
			System.out.println("Drag leave");
		}

		@Override
		public boolean performDrop(Object arg0)
		{
			System.out.println("Dropped");
			lvActiveFields.add(arg0);
			return true;
		}

		@Override
		public boolean validateDrop(Object arg0,int arg1,TransferData arg2)
		{
			System.out.println("Validated");
			return true;
		}
	}

Re: Drop Target Not Accepting Drops [message #499039 is a reply to message #498870] Thu, 19 November 2009 15:29 Go to previous messageGo to next message
Grant Gayed is currently offline Grant GayedFriend
Messages: 2150
Registered: July 2009
Senior Member
Hi Ian, I hope you still have your hair,

I think your code is correct, but since you're only adding support for
DND.DROP_COPY, it will only work if you hold CTRL while dragging (dragging
without CTRL pressed implies a move, not a copy). The snippet below
captures your case, and note that dragging from the Label to the List is
only allowed if CTRL is pressed.

public class Main0 {
private class MyContentProvider implements IStructuredContentProvider {
public Object[] getElements(Object inputElement) {
return (MyModel[]) inputElement;
}
public void dispose() {}
public void inputChanged(Viewer viewer, Object oldInput, Object
newInput) {}
}
public class MyModel {
public int counter;
public MyModel(int counter) {
this.counter = counter;
}
public String toString() {
return "Item " + this.counter;
}
}
public Main0(Shell shell) {
final ListViewer v = new ListViewer(shell, SWT.H_SCROLL |
SWT.V_SCROLL);
v.setLabelProvider(new LabelProvider());
v.setContentProvider(new MyContentProvider());
MyModel[] model = createModel();
v.setInput(model);
Transfer[] types = new Transfer[] { TextTransfer.getInstance() };
v.addDropSupport(DND.DROP_COPY, types, new
DropTargetFieldsDropTargetListener(v)); // <---
}
private MyModel[] createModel() {
MyModel[] elements = new MyModel[10];
for (int i = 0; i < 10; i++) {
elements[i] = new MyModel(i);
}
return elements;
}
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new GridLayout());
final Label label = new Label(shell, SWT.SINGLE);
label.setText("drag source");
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("");
}
}
});
new Main0(shell);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) display.sleep();
}
display.dispose();
}
private class DropTargetFieldsDropTargetListener extends
ViewerDropAdapter {
protected DropTargetFieldsDropTargetListener(Viewer viewer) {
super(viewer);
}
public void drop(DropTargetEvent event) {
System.out.println("drop");
}
public boolean performDrop(Object arg0) {
System.out.println("performDrop");
return true;
}
public boolean validateDrop(Object arg0, int arg1, TransferData
arg2) {
System.out.println("validateDrop");
return true;
}
}
}

HTH,
Grant


"Ian Harac" <mrlizard@gmail.com> wrote in message
news:he1pfn$lmg$1@build.eclipse.org...
> I have spent two frustrating days on this and am getting into hair-tearing
mode.
>
> I have a Gridviewer (Nebula), and a ListViewer (standard SWT). Gridviewer
is set to be the drag source, ListViewer the drop target. Each has had the
appropriate method set, ie, addDropSupport, addDragSupport. Dragging from
the gridviewer works -- I want it to drag the column name, and that's what
it does, no matter where in the column you click.
>
> I have set the transfer types for each to TextTransfer().
>
> If I drag outside of my Java app and onto, say, Microsoft OneNote, I can
drop the text, so I am 100% sure the drag is set up properly.
>
> If I drag over the target, I get DragEntered, DragOver,DragLeave messages,
but never ever Drop. The drag image is decorated with the "No Drop" icon,
but not by any code I have written. I have gone over and over the code to
verify everything is, or seems to be, set up properly, that the data is the
right type, that the drop target wants that type, etc. However, nothing I do
causes it to register a Drop.
>
> I've tried using a ViewerDropAdapter, and this, too, never registers a
drop -- the ValidateDrop and PerformDrop methods never get called.
>
> Here's what I think are the relevant code snippets.
>
>
> Transfer[] types=new Transfer[]{TextTransfer.getInstance()};
> //makes the ListViewer
> lvActiveFields=new ListViewer(grpAnalysis,SWT.BORDER|SWT.V_SCROLL);
> lvActiveFields.setLabelProvider(new ViewerLabelProvider());
> lvActiveFields.setContentProvider(new ListContentProvider());
> lvActiveFields.setInput(fields);
> listActiveFields=lvActiveFields.getList();
> listActiveFields.setToolTipText("Drag fields of interest to this box.");
> {
> GridData gridData=new GridData(SWT.LEFT,SWT.FILL,false,true,1,1);
> gridData.widthHint=129;
> listActiveFields.setLayoutData(gridData);
> }
> {
> lvActiveFields.addDropSupport(DND.DROP_COPY,types,new
DropTargetFieldsDropTargetListener(lvActiveFields));
> }
>
> //the drop listener
> //note there's some shotgun debugging code in here in a vain attempt to
pin down what's going on...
>
> private class DropTargetFieldsDropTargetListener extends ViewerDropAdapter
> {
> final TextTransfer textTransfer=TextTransfer.getInstance();
>
> protected DropTargetFieldsDropTargetListener(Viewer viewer)
> {
> super(viewer);
> }
>
> @Override
> public void dragEnter(DropTargetEvent event)
> {
> System.out.println("Drag Enter");
> }
>
> @Override
> public void drop(DropTargetEvent event)
> {
> System.out.println("Dropped");
> if (textTransfer.isSupportedType(event.currentDataType))
> {
> Object o=textTransfer.nativeToJava(event.currentDataType);
> String t=(String)o;
> fields.add(t);
> lvActiveFields.refresh();
> }
> }
>
> @Override
> public void dragOver(DropTargetEvent event)
> {
> System.out.println("Drag Over");
>
> if (textTransfer.isSupportedType(event.currentDataType))
> {
>
> Object o=textTransfer.nativeToJava(event.currentDataType);
> String t=(String)o;
> if (t!=null)
> {
> System.out.println(t);
>
> }
> else
> {
> System.out.println("t is null.");
> }
> }
> }
>
> @Override
> public void dropAccept(DropTargetEvent arg0)
> {
> System.out.println("Drop Accept");
> }
>
> @Override
> public void dragLeave(DropTargetEvent arg0)
> {
> System.out.println("Drag leave");
> }
>
> @Override
> public boolean performDrop(Object arg0)
> {
> System.out.println("Dropped");
> lvActiveFields.add(arg0);
> return true;
> }
>
> @Override
> public boolean validateDrop(Object arg0,int arg1,TransferData arg2)
> {
> System.out.println("Validated");
> return true;
> }
> }
>
>
Re: Drop Target Not Accepting Drops [message #499045 is a reply to message #499039] Thu, 19 November 2009 16:07 Go to previous messageGo to next message
Ian Harac is currently offline Ian HaracFriend
Messages: 10
Registered: November 2009
Location: Southern Indiana
Junior Member

Please forgive me while I up and kill myself.

I had just about gotten there by building a seemingly identical app in a "clean" environment to just test DnD without any of the other active code which may have been having an effect, and noticed that DND.DROP_MOVE was about the only difference. I didn't set it as an option because, thinking too logically, I didn't want to move data, just copy it, and somehow I missed that the Ctrl-to-copy logic was built in, not added by the programmer. (Where is that documented, BTW?)

Let me see if that fixes it. I suspect it will. Thank you very much for the whack on the head.
Re: Drop Target Not Accepting Drops [message #499247 is a reply to message #499045] Fri, 20 November 2009 14:51 Go to previous message
Grant Gayed is currently offline Grant GayedFriend
Messages: 2150
Registered: July 2009
Senior Member
I don't think "Ctrl+Drag == Copy" is documented in swt because it's a
behaviour that comes from the platform. On OSX, "Alt+Drag == Copy". And
even better, on motif, DND is only triggered by either holding the _middle_
mouse button (on a 3-button mouse), or both button 1 and 2 (on a 2-button
mouse).

Grant


"Ian Harac" <mrlizard@gmail.com> wrote in message
news:he3qfv$hth$1@build.eclipse.org...
> Please forgive me while I up and kill myself.
>
> I had just about gotten there by building a seemingly identical app in a
"clean" environment to just test DnD without any of the other active code
which may have been having an effect, and noticed that DND.DROP_MOVE was
about the only difference. I didn't set it as an option because, thinking
too logically, I didn't want to move data, just copy it, and somehow I
missed that the Ctrl-to-copy logic was built in, not added by the
programmer. (Where is that documented, BTW?)
>
> Let me see if that fixes it. I suspect it will. Thank you very much for
the whack on the head.
Previous Topic:Where is the Online Javadoc of SWT?
Next Topic:Layout not based on content of control
Goto Forum:
  


Current Time: Sun Jul 05 11:32:17 GMT 2020

Powered by FUDForum. Page generated in 0.02207 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top