Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Drag and Drop of Objects - is there an easier way?
Drag and Drop of Objects - is there an easier way? [message #451685] Sat, 05 March 2005 10:08 Go to next message
h1055071 is currently offline h1055071Friend
Messages: 335
Registered: July 2009
Senior Member
I want to drag and drop user objects from tree node to tree node in SWT.

The only way I can do it at the moment is to drag and drop a String ID that
maps to the objects I'm dragging. Do I really have to implement my own
ByteArrayTransfer class to do basic drag and drop of objects or is there an
easier way?

Phil
Re: Drag and Drop of Objects - is there an easier way? [message #451706 is a reply to message #451685] Sun, 06 March 2005 15:01 Go to previous message
Eclipse UserFriend
Originally posted by: kent.generatescape.com

here is another way, this is based on one of the snippets you can find if
you trawl through all the messages:

public class MovingItems2
{
public static void main(String[] args)
{

final Display display = Display.getDefault();
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 parent = (TreeItem) event.item;
TreeItem item = new TreeItem(parent, SWT.NONE);
item.setText(text);
}
}
});

shell.setSize(400, 400);
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}

}

"Phillip Beauvoir" <p.beauvoir@bolton.ac.uk> wrote in message
news:d0c0et$ut8$1@www.eclipse.org...
>I want to drag and drop user objects from tree node to tree node in SWT.
>
> The only way I can do it at the moment is to drag and drop a String ID
> that maps to the objects I'm dragging. Do I really have to implement my
> own ByteArrayTransfer class to do basic drag and drop of objects or is
> there an easier way?
>
> Phil
>
Previous Topic:Support level for printer drivers
Next Topic:Using Firefox instead of Mozilla
Goto Forum:
  


Current Time: Tue Apr 16 11:41:46 GMT 2024

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

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

Back to the top