Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Drag and Drop in a Tree with dynamic insertion mark?
Drag and Drop in a Tree with dynamic insertion mark? [message #445938] Mon, 15 November 2004 14:29 Go to next message
Benjamin Pasero is currently offline Benjamin PaseroFriend
Messages: 337
Registered: July 2009
Senior Member
Hi,

I would like to Drag and Drop leaf items in a Tree widget. From the
Snippet section, I found one that closely meets my requirements
( http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.swt.sni ppets/src/org/eclipse/swt/snippets/Snippet91.java?rev=HEAD&a mp;content-type=text/vnd.viewcvs-markup).

Now, the Snippet only allows to drag and drop items from one parent to
another one, but I would like to place a leaf item just before (or after)
a parent iten.

Therefor something like a dynamic insertion mark is needed, that is
showing before or after a TreeItem.

But, how to detect that the user is currently dragging to a point between
two TreeItems?

Did anybody wrote something like that before?

A good example of what I am trying to implement is the Bookmark Management
in Firefox. Just goto "Bookmarks" > "Manage Bookmarks..." and from the
TableTree on the right side, drag a Category. See that when the mouse
draggs between two categories, a small insertion mark is showing.

Regards,
Ben
Re: Drag and Drop in a Tree with dynamic insertion mark? [message #445983 is a reply to message #445938] Mon, 15 November 2004 16:41 Go to previous messageGo to next message
Veronika Irvine is currently offline Veronika IrvineFriend
Messages: 1272
Registered: July 2009
Senior Member
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 ();
}

"Benjamin Pasero" <bpasero@rssowl.org> wrote in message
news:cnaeg4$4cc$1@eclipse.org...
> Hi,
>
> I would like to Drag and Drop leaf items in a Tree widget. From the
> Snippet section, I found one that closely meets my requirements
> ( http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.swt.sni ppets/src/org/eclipse/swt/snippets/Snippet91.java?rev=HEAD&a mp;content-type=text/vnd.viewcvs-markup).
>
> Now, the Snippet only allows to drag and drop items from one parent to
> another one, but I would like to place a leaf item just before (or after)
> a parent iten.
>
> Therefor something like a dynamic insertion mark is needed, that is
> showing before or after a TreeItem.
>
> But, how to detect that the user is currently dragging to a point between
> two TreeItems?
>
> Did anybody wrote something like that before?
> A good example of what I am trying to implement is the Bookmark Management
> in Firefox. Just goto "Bookmarks" > "Manage Bookmarks..." and from the
> TableTree on the right side, drag a Category. See that when the mouse
> draggs between two categories, a small insertion mark is showing.
>
> Regards,
> Ben
>
Re: Drag and Drop in a Tree with dynamic insertion mark? [message #445984 is a reply to message #445983] Mon, 15 November 2004 16:58 Go to previous messageGo to next message
Veronika Irvine is currently offline Veronika IrvineFriend
Messages: 1272
Registered: July 2009
Senior Member
My previous example was incomplete; it only showed the drag over effect.

I have improved Snippet 91 to show the drag over effect and to drop the tree
item accordingly.

http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.swt.sni ppets/src/org/eclipse/swt/snippets/Snippet91.java?rev=HEAD&a mp;content-type=text/vnd.viewcvs-markup

"Veronika Irvine" <veronika_irvine@oti.com> wrote in message
news:cnam87$irc$1@eclipse.org...
> 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 ();
> }
>
> "Benjamin Pasero" <bpasero@rssowl.org> wrote in message
> news:cnaeg4$4cc$1@eclipse.org...
>> Hi,
>>
>> I would like to Drag and Drop leaf items in a Tree widget. From the
>> Snippet section, I found one that closely meets my requirements
>> ( http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.swt.sni ppets/src/org/eclipse/swt/snippets/Snippet91.java?rev=HEAD&a mp;content-type=text/vnd.viewcvs-markup).
>>
>> Now, the Snippet only allows to drag and drop items from one parent to
>> another one, but I would like to place a leaf item just before (or after)
>> a parent iten.
>>
>> Therefor something like a dynamic insertion mark is needed, that is
>> showing before or after a TreeItem.
>>
>> But, how to detect that the user is currently dragging to a point between
>> two TreeItems?
>>
>> Did anybody wrote something like that before?
>> A good example of what I am trying to implement is the Bookmark
>> Management in Firefox. Just goto "Bookmarks" > "Manage Bookmarks..." and
>> from the TableTree on the right side, drag a Category. See that when the
>> mouse draggs between two categories, a small insertion mark is showing.
>>
>> Regards,
>> Ben
>>
>
>
Re: Drag and Drop in a Tree with dynamic insertion mark? [message #445985 is a reply to message #445983] Mon, 15 November 2004 16:58 Go to previous messageGo to next message
Paul Singleton is currently offline Paul SingletonFriend
Messages: 37
Registered: July 2009
Member
What is the neatest way for the drop() handler to figure out
whether (the user thinks) the drop is above, onto or below
the event.item? Should we record the most recent setting of
event.feedback somewhere?

Paul Singleton

Veronika Irvine wrote:
> 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 ();
> }
Re: Drag and Drop in a Tree with dynamic insertion mark? [message #445987 is a reply to message #445985] Mon, 15 November 2004 17:04 Go to previous messageGo to next message
Veronika Irvine is currently offline Veronika IrvineFriend
Messages: 1272
Registered: July 2009
Senior Member
Use the x, y coordinates in the drop event. It is possible to have a drop
event without ever getting a drag over event if the user performs the action
very quickly.

"Paul Singleton" <paul@jbgb.com> wrote in message
news:cnan8h$kir$1@eclipse.org...
> What is the neatest way for the drop() handler to figure out
> whether (the user thinks) the drop is above, onto or below
> the event.item? Should we record the most recent setting of
> event.feedback somewhere?
>
> Paul Singleton
>
> Veronika Irvine wrote:
>> 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 ();
>> }
Re: Drag and Drop in a Tree with dynamic insertion mark? [message #445996 is a reply to message #445987] Mon, 15 November 2004 20:00 Go to previous message
Benjamin Pasero is currently offline Benjamin PaseroFriend
Messages: 337
Registered: July 2009
Senior Member
Thanks to Veronika for updating the snippet.

Ben

> Use the x, y coordinates in the drop event. It is possible to have a drop
> event without ever getting a drag over event if the user performs the action
> very quickly.

> "Paul Singleton" <paul@jbgb.com> wrote in message
> news:cnan8h$kir$1@eclipse.org...
>> What is the neatest way for the drop() handler to figure out
>> whether (the user thinks) the drop is above, onto or below
>> the event.item? Should we record the most recent setting of
>> event.feedback somewhere?
>>
>> Paul Singleton
>>
>> Veronika Irvine wrote:
>>> 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 ();
>>> }
Previous Topic:Dynamic TableTree
Next Topic:Can't Debug JAR File Using JFrames
Goto Forum:
  


Current Time: Sun Sep 22 23:10:19 GMT 2024

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

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

Back to the top