drag & drop: source and target are the same [message #897041] |
Fri, 20 July 2012 22:29 |
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 #897078 is a reply to message #897071] |
Sat, 21 July 2012 13:27 |
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...
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:
-
-
-
I think it should be easy, but I am too stupid
-
Attachment: drag1.png
(Size: 3.36KB, Downloaded 544 times) -
Attachment: drag2.png
(Size: 3.43KB, Downloaded 539 times) -
Attachment: drag3.png
(Size: 3.40KB, Downloaded 537 times)
[Updated on: Sun, 22 July 2012 09:56] Report message to a moderator
|
|
|
|
|
Re: drag & drop: source and target are the same [message #897376 is a reply to message #897170] |
Mon, 23 July 2012 20:23 |
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) {
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 ||
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 13:16] Report message to a moderator
|
|
|
Powered by
FUDForum. Page generated in 0.03592 seconds