我有一个Text
我想成为的控件DragSource
。如果您突出显示一些文本然后拖动您的选择,它会起作用。但是,我希望能够拖动Text
控件的全部内容,而不是突出显示所有内容然后拖动它。例如,用户应该能够从小Text
部件的背景单击和拖动。
下面是一个最小的例子:
import org.eclipse.swt.SWT;
import org.eclipse.swt.dnd.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
public class BrowseDragExample {
public static void main(String[] args) {
// put together the SWT main loop
final Display display = Display.getDefault();
display.syncExec(new Runnable() {
@Override
public void run() {
Shell shell = new Shell(display, SWT.SHELL_TRIM);
initializeGui(shell);
//open the shell
shell.open();
//run the event loop
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
});
}
// creates the gui
private static void initializeGui(Composite parent) {
GridLayout layout = new GridLayout(3, false);
parent.setLayout(layout);
Label infoLbl = new Label(parent, SWT.WRAP);
GridData gd = new GridData();
gd.grabExcessHorizontalSpace = true;
gd.horizontalAlignment = SWT.FILL;
gd.horizontalSpan = 3;
infoLbl.setLayoutData(gd);
infoLbl.setText("You should be able to drag text from one box to another, using either the Text, the ToolBar, or the Label.");
addToolBarPair(parent);
addToolBarPair(parent);
}
private static void addToolBarPair(Composite parent) {
// make the text element
final Text text = new Text(parent, SWT.SINGLE | SWT.BORDER);
GridData gd = new GridData();
gd.grabExcessHorizontalSpace = true;
gd.horizontalAlignment = SWT.FILL;
text.setLayoutData(gd);
// make the toolbar element
ToolBar toolbar = new ToolBar(parent, SWT.NONE);
ToolItem toolItem = new ToolItem(toolbar, SWT.PUSH);
toolItem.setText("Toolbar DND");
// make the label element
Label label = new Label(parent, SWT.NONE);
label.setText("Label DND");
// listener for drags
DragSourceListener dragListener = new DragSourceListener() {
@Override public void dragStart(DragSourceEvent e) {}
@Override public void dragFinished(DragSourceEvent e) {}
@Override
public void dragSetData(DragSourceEvent e) {
e.data = text.getText();
}
};
// listener for drops
DropTargetListener dropListener = new DropTargetListener() {
@Override public void dragEnter(DropTargetEvent e) {
e.detail = DND.DROP_COPY;
}
@Override public void dragLeave(DropTargetEvent e) {}
@Override public void dragOperationChanged(DropTargetEvent e) {}
@Override public void dragOver(DropTargetEvent e) {}
@Override public void dropAccept(DropTargetEvent e) {}
@Override public void drop(DropTargetEvent e) {
String data = (String) e.data;
text.setText(data);
}
};
// assign these listeners for each control
addDragSupport(text, dragListener);
addDragSupport(toolbar, dragListener);
addDragSupport(label, dragListener);
addDropSupport(text, dropListener);
addDropSupport(toolbar, dropListener);
addDropSupport(label, dropListener);
}
private static void addDragSupport(Control control, DragSourceListener listener) {
DragSource dragSource = new DragSource(control, DND.DROP_COPY);
dragSource.setTransfer(new Transfer[]{TextTransfer.getInstance()});
dragSource.addDragListener(listener);
}
private static void addDropSupport(Control control, DropTargetListener listener) {
DropTarget dropTarget = new DropTarget(control, DND.DROP_COPY);
dropTarget.setTransfer(new Transfer[]{TextTransfer.getInstance()});
dropTarget.addDropListener(listener);
}
}