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]]>Benjamin Pasero2004-11-15T14:29:24-00:00Re: Drag and Drop in a Tree with dynamic insertion mark?
https://www.eclipse.org/forums/index.php/mv/msg/141509/445983/#msg_445983
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
>]]>Veronika Irvine2004-11-15T16:41:24-00:00Re: Drag and Drop in a Tree with dynamic insertion mark?
https://www.eclipse.org/forums/index.php/mv/msg/141509/445984/#msg_445984
I have improved Snippet 91 to show the drag over effect and to drop the tree
item accordingly.
"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
>>
>
>]]>Veronika Irvine2004-11-15T16:58:11-00:00Re: Drag and Drop in a Tree with dynamic insertion mark?
https://www.eclipse.org/forums/index.php/mv/msg/141509/445985/#msg_445985
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 ();
> }]]>Paul Singleton2004-11-15T16:58:56-00:00Re: Drag and Drop in a Tree with dynamic insertion mark?
https://www.eclipse.org/forums/index.php/mv/msg/141509/445987/#msg_445987
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 ();
>> }]]>Veronika Irvine2004-11-15T17:04:34-00:00Re: Drag and Drop in a Tree with dynamic insertion mark?
https://www.eclipse.org/forums/index.php/mv/msg/141509/445996/#msg_445996
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 ();
>>> }]]>Benjamin Pasero2004-11-15T20:00:48-00:00