tags:

views:

688

answers:

2

I've written an application recently using SWT. In one of its dialog box, I have a few widgets, one of which is Text, which is designed to support DND with other widgets. I've first added DND support for the 2 Tree widgets on the same dialog box (both drag source and drop target). Before I added DND support for that Text widget, I noticed that on Linux platform (gtk), SWT Text widget automatically get drag and drop support. That is, I can already drag from the other Tree widgets and drop on this Text (at any position to inserted there), as well as selecting and dragging any text from this Text to other Tree or Text widget.

However, this is only working on Linux platform but not on Windows. The same program, if running on Windows, will not have any DND support for that Text widget (Tree widgets of course have DND support since I specifically write for them).

So here's what I want to achieve on Windows as well:

  1. drop text at any position in Text widget.
  2. before dropping and while hovering, able to see the caret position clearly where the intended position to drop. caret position should move along with the mouse cursor.
  3. support multi-line in Text widget

SOLUTION:

DropTarget target = new DropTarget(sytledText, DND.DROP_MOVE | DND.DROP_COPY);
target.setTransfer(new Transfer[] { TextTransfer.getInstance() });
target.addDropListener(new StyleTextDropTargetEffect(sytledText));
  1. Use StyledText instead of Text widget
  2. Use StyledTextDropTargetEffect (or extend it) and add it as dr op listener
+1  A: 

Hi,

  1. &
  2. AFAIK in Windows you cannot do that using the Text widget easily. At drop you can insert text at the last cursor position or at the end of the text. You may consider using the StyledText widget which supports dropping at any position and 'caret hovering' as well.
  3. Both Text and StyledText support the SWT.MULTI style for being multi-line.

You can find many great code snippets at the SWT snippets page including examples for drag and drop using the StyledText widget. Also there is a good introduction for DND in SWT.

Csaba_H
A: 

If you need to drag components around a SWT panel, there is a good tutorial of that here:

http://ivolo.mit.edu/post/Drag-Components-around-an-SWT-Panel-using-the-Mouse.aspx

Ilya