Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » drag & drop: source and target are the same
drag & drop: source and target are the same [message #897041] Fri, 20 July 2012 18:29 Go to next message
Andreas   is currently offline Andreas
Messages: 4
Registered: July 2012
Junior Member
Hello,

I try to learn SWT with the collection of snippets. I have a question concerning the topic of drag & drop:

In the snippets (e.g. Snippet78.java) the DragSource finishes the drag by comparing event.detail (in DragSourceListener::dragFinished()) with DND.DROP_MOVE and removes the value if the operation was a DND.DROP_MOVE. But my problem happens, when the source is the target at the same time. For example, when I try to drag the text of the left label (Snippet78.java) to itself: the text deletes itself, which should not be. In line 71 I could add something like this:
if (((String) event.data).equals(((Label)target.getControl()).getText())) {
    event.detail = DND.DROP_NONE;
}

but in this case, I would have another problem:

If I copy the text of the label in a way, that the text of the right label is the same as the text of the left label, and after this, I want to move the text of the right label to the left, the text of the right label should disappear, but it doesn't. ...?
Re: drag & drop: source and target are the same [message #897071 is a reply to message #897041] Sat, 21 July 2012 08:56 Go to previous messageGo to next message
Jan Krakora is currently offline Jan Krakora
Messages: 477
Registered: December 2009
Location: Prague
Senior Member
Hi Andreas,

if you do that as you wrote, the right label shouldn't disappear because you set the event.detail to DND.DROP_NONE and in the right label's DragSourceListener you can see
public void dragFinished(DragSourceEvent event) {
    if (event.detail == DND.DROP_MOVE)
	label.setText ("");
    }

the condition is not satisfied and so the text is left as is.
Re: drag & drop: source and target are the same [message #897078 is a reply to message #897071] Sat, 21 July 2012 09:27 Go to previous messageGo to next message
Andreas   is currently offline Andreas
Messages: 4
Registered: July 2012
Junior Member
Yes, exactly. But I would like to have this behaviour:

  • If I had "TEXT" on the left side and "TEXT" on the right side, and I move the left text to the right side, the left text should disappear.
  • If I had "TEXT" on the left side and "" on the right side, and I move the left side to the left (source == target), the text should remain.

I don't know, how to program this behaviour... Confused

P.S.: My english is really awfull, so I show you my intention with pictures. I want to change the Snippet78.java so that the program has this behaviour:

  1. index.php/fa/10800/0/
  2. index.php/fa/10801/0/
  3. index.php/fa/10802/0/

I think it should be easy, but I am too stupid Wink
  • Attachment: drag1.png
    (Size: 3.36KB, Downloaded 246 times)
  • Attachment: drag2.png
    (Size: 3.43KB, Downloaded 247 times)
  • Attachment: drag3.png
    (Size: 3.40KB, Downloaded 249 times)

[Updated on: Sun, 22 July 2012 05:56]

Report message to a moderator

Re: drag & drop: source and target are the same [message #897140 is a reply to message #897078] Sun, 22 July 2012 06:57 Go to previous messageGo to next message
Jan Krakora is currently offline Jan Krakora
Messages: 477
Registered: December 2009
Location: Prague
Senior Member
Don't worry about your English, I think it's better than mine Razz

Anyway, it seems to me impossible accomplish your needs. Since you have no information about the target control in a DragSourceListener,
you simply can't know whether the target is the source.

My suggestion is to cancel the DND operation if the target text is the same as the dropped data
target.addDropListener (new DropTargetAdapter() {
		
    public void drop(DropTargetEvent event) {
	if (event.data == null || event.data.equals(label.getText())) {
	    event.detail = DND.DROP_NONE;
	    return;
	}
	label.setText ((String) event.data);
    }
});

so it's exactly the same you have mentioned in your first post. It just doesn't met your third requirement.
Re: drag & drop: source and target are the same [message #897170 is a reply to message #897140] Sun, 22 July 2012 18:35 Go to previous messageGo to next message
Andreas   is currently offline Andreas
Messages: 4
Registered: July 2012
Junior Member
Hi Jan,

thank you for your answer. I thougt, it would be possible. Sad But if you find a way to outfox SWT, feel free to post it... Razz
Re: drag & drop: source and target are the same [message #897376 is a reply to message #897170] Mon, 23 July 2012 16:23 Go to previous message
Andreas   is currently offline Andreas
Messages: 4
Registered: July 2012
Junior Member
I changed the code to:
package org.eclipse.swt.snippets;

import org.eclipse.swt.*;
import org.eclipse.swt.dnd.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

public class Snippet78 {

public static void main(String[] args) {

    Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    final Label label1 = new Label(shell, SWT.BORDER);
    label1.setText("TEXT");
    final Label label2 = new Label(shell, SWT.BORDER);
    setDragDrop(label1);
    setDragDrop(label2);
    shell.setSize(200, 200);
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}
static Label sourceLabel;
public static void setDragDrop(final Label label) {

    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) {
            
            sourceLabel = label;
            event.data = label.getText();
        }

        public void dragFinished(DragSourceEvent event) {
            if (event.detail == DND.DROP_MOVE)
                label.setText("");
        }
    });

    DropTarget target = new DropTarget(label, operations);
    target.setTransfer(types);
    target.addDropListener(new DropTargetAdapter() {
        public void drop(DropTargetEvent event) {
            if (event.data == null || 
                
                    label == sourceLabel) {
                event.detail = DND.DROP_NONE;
                return;
            }
            label.setText((String) event.data);
        }
    });
}
}

This seems to fit my intention: All of the three conditions are met. But I am not sure, if this is really the best way.

[Updated on: Tue, 24 July 2012 09:16]

Report message to a moderator

Previous Topic: mandatory text box
Next Topic:SWT multiline table with rows of different sizes
Goto Forum:
  


Current Time: Fri Aug 29 04:17:44 EDT 2014

Powered by FUDForum. Page generated in 0.03835 seconds