use of org.eclipse.swt.dnd.DragSourceListener in project eclipse.platform.swt by eclipse.
the class DNDExample method createDragSource.
private void createDragSource() {
if (dragSource != null)
dragSource.dispose();
dragSource = new DragSource(dragControl, dragOperation);
dragSource.setTransfer(dragTypes);
dragSource.addDragListener(new DragSourceListener() {
@Override
public void dragFinished(org.eclipse.swt.dnd.DragSourceEvent event) {
dragConsole.append(">>dragFinished\n");
printEvent(event);
dragDataText = dragDataRTF = dragDataHTML = dragDataURL = null;
dragDataFiles = null;
if (event.detail == DND.DROP_MOVE) {
switch(dragControlType) {
case BUTTON_CHECK:
case BUTTON_TOGGLE:
case BUTTON_RADIO:
{
Button b = (Button) dragControl;
b.setText("");
break;
}
case STYLED_TEXT:
{
StyledText text = (StyledText) dragControl;
text.insert("");
break;
}
case TABLE:
{
Table table = (Table) dragControl;
TableItem[] items = table.getSelection();
for (TableItem item : items) {
item.dispose();
}
break;
}
case TEXT:
{
Text text = (Text) dragControl;
text.insert("");
break;
}
case TREE:
{
Tree tree = (Tree) dragControl;
TreeItem[] items = tree.getSelection();
for (TreeItem item : items) {
item.dispose();
}
break;
}
case CANVAS:
{
dragControl.setData("STRINGS", null);
dragControl.redraw();
break;
}
case LABEL:
{
Label label = (Label) dragControl;
label.setText("");
break;
}
case LIST:
{
List list = (List) dragControl;
int[] indices = list.getSelectionIndices();
list.remove(indices);
break;
}
case COMBO:
{
Combo combo = (Combo) dragControl;
combo.setText("");
break;
}
}
}
}
@Override
public void dragSetData(org.eclipse.swt.dnd.DragSourceEvent event) {
dragConsole.append(">>dragSetData\n");
printEvent(event);
if (TextTransfer.getInstance().isSupportedType(event.dataType)) {
event.data = dragDataText;
}
if (RTFTransfer.getInstance().isSupportedType(event.dataType)) {
event.data = dragDataRTF;
}
if (HTMLTransfer.getInstance().isSupportedType(event.dataType)) {
event.data = dragDataHTML;
}
if (URLTransfer.getInstance().isSupportedType(event.dataType)) {
event.data = dragDataURL;
}
if (FileTransfer.getInstance().isSupportedType(event.dataType)) {
event.data = dragDataFiles;
}
}
@Override
public void dragStart(org.eclipse.swt.dnd.DragSourceEvent event) {
dragConsole.append(">>dragStart\n");
printEvent(event);
dragDataFiles = fileList.getItems();
switch(dragControlType) {
case BUTTON_CHECK:
case BUTTON_TOGGLE:
case BUTTON_RADIO:
{
Button b = (Button) dragControl;
dragDataText = b.getSelection() ? "true" : "false";
break;
}
case STYLED_TEXT:
{
StyledText text = (StyledText) dragControl;
String s = text.getSelectionText();
if (s.length() == 0) {
event.doit = false;
} else {
dragDataText = s;
}
break;
}
case TABLE:
{
Table table = (Table) dragControl;
TableItem[] items = table.getSelection();
if (items.length == 0) {
event.doit = false;
} else {
StringBuilder buffer = new StringBuilder();
for (int i = 0; i < items.length; i++) {
buffer.append(items[i].getText());
if (items.length > 1 && i < items.length - 1) {
buffer.append("\n");
}
}
dragDataText = buffer.toString();
}
break;
}
case TEXT:
{
Text text = (Text) dragControl;
String s = text.getSelectionText();
if (s.length() == 0) {
event.doit = false;
} else {
dragDataText = s;
}
break;
}
case TREE:
{
Tree tree = (Tree) dragControl;
TreeItem[] items = tree.getSelection();
if (items.length == 0) {
event.doit = false;
} else {
StringBuilder buffer = new StringBuilder();
for (int i = 0; i < items.length; i++) {
buffer.append(items[i].getText());
if (items.length > 1 && i < items.length - 1) {
buffer.append("\n");
}
}
dragDataText = buffer.toString();
}
break;
}
case CANVAS:
{
String[] strings = (String[]) dragControl.getData("STRINGS");
if (strings == null || strings.length == 0) {
event.doit = false;
} else {
StringBuilder buffer = new StringBuilder();
for (int i = 0; i < strings.length; i++) {
buffer.append(strings[i]);
if (strings.length > 1 && i < strings.length - 1) {
buffer.append("\n");
}
}
dragDataText = buffer.toString();
}
break;
}
case LABEL:
{
Label label = (Label) dragControl;
String string = label.getText();
if (string.length() == 0) {
event.doit = false;
} else {
dragDataText = string;
}
break;
}
case LIST:
{
List list = (List) dragControl;
String[] selection = list.getSelection();
if (selection.length == 0) {
event.doit = false;
} else {
StringBuilder buffer = new StringBuilder();
for (int i = 0; i < selection.length; i++) {
buffer.append(selection[i]);
if (selection.length > 1 && i < selection.length - 1) {
buffer.append("\n");
}
}
dragDataText = buffer.toString();
}
break;
}
case COMBO:
{
Combo combo = (Combo) dragControl;
String string = combo.getText();
Point selection = combo.getSelection();
if (selection.x == selection.y) {
event.doit = false;
} else {
dragDataText = string.substring(selection.x, selection.y);
}
break;
}
default:
throw new SWTError(SWT.ERROR_NOT_IMPLEMENTED);
}
if (dragDataText != null) {
dragDataRTF = "{\\rtf1{\\colortbl;\\red255\\green0\\blue0;}\\cf1\\b " + dragDataText + "}";
dragDataHTML = "<b>" + dragDataText + "</b>";
dragDataURL = "http://" + dragDataText.replace(' ', '.');
try {
new URL(dragDataURL);
} catch (MalformedURLException e) {
dragDataURL = null;
}
}
for (Transfer dragType : dragTypes) {
if (dragType instanceof TextTransfer && dragDataText == null) {
event.doit = false;
}
if (dragType instanceof RTFTransfer && dragDataRTF == null) {
event.doit = false;
}
if (dragType instanceof HTMLTransfer && dragDataHTML == null) {
event.doit = false;
}
if (dragType instanceof URLTransfer && dragDataURL == null) {
event.doit = false;
}
if (dragType instanceof FileTransfer && (dragDataFiles == null || dragDataFiles.length == 0)) {
event.doit = false;
}
}
}
});
}
use of org.eclipse.swt.dnd.DragSourceListener in project eclipse.platform.swt by eclipse.
the class FileViewer method createTableDragSource.
/**
* Creates the Drag & Drop DragSource for items being dragged from the table.
*
* @return the DragSource for the table
*/
private DragSource createTableDragSource(final Table table) {
DragSource dragSource = new DragSource(table, DND.DROP_MOVE | DND.DROP_COPY);
dragSource.setTransfer(FileTransfer.getInstance());
dragSource.addDragListener(new DragSourceListener() {
TableItem[] dndSelection = null;
String[] sourceNames = null;
@Override
public void dragStart(DragSourceEvent event) {
dndSelection = table.getSelection();
sourceNames = null;
event.doit = dndSelection.length > 0;
isDragging = true;
}
@Override
public void dragFinished(DragSourceEvent event) {
dragSourceHandleDragFinished(event, sourceNames);
dndSelection = null;
sourceNames = null;
isDragging = false;
handleDeferredRefresh();
}
@Override
public void dragSetData(DragSourceEvent event) {
if (dndSelection == null || dndSelection.length == 0)
return;
if (!FileTransfer.getInstance().isSupportedType(event.dataType))
return;
sourceNames = new String[dndSelection.length];
for (int i = 0; i < dndSelection.length; i++) {
File file = (File) dndSelection[i].getData(TABLEITEMDATA_FILE);
sourceNames[i] = file.getAbsolutePath();
}
event.data = sourceNames;
}
});
return dragSource;
}
use of org.eclipse.swt.dnd.DragSourceListener in project eclipse.platform.swt by eclipse.
the class Bug307441_DnDOverlappingControls method setDragSource.
public static void setDragSource(final Label label) {
int operations = DND.DROP_MOVE | DND.DROP_COPY | DND.DROP_LINK;
final DragSource source = new DragSource(label, operations);
source.setTransfer(new Transfer[] { URLTransfer.getInstance() });
source.addDragListener(new DragSourceListener() {
@Override
public void dragStart(DragSourceEvent e) {
}
@Override
public void dragSetData(DragSourceEvent e) {
e.data = label.getText();
}
@Override
public void dragFinished(DragSourceEvent event) {
}
});
}
use of org.eclipse.swt.dnd.DragSourceListener in project eclipse.platform.swt by eclipse.
the class Bug186038_DNDActivateEvent method main.
public static void main(String[] args) {
Display display = new Display();
final Shell shell = new Shell(display);
GridLayout layout = new GridLayout();
layout.numColumns = 2;
shell.setLayout(layout);
// create the drop down widget.shell
final Shell dropDownShell = new Shell(shell, SWT.ON_TOP | SWT.DROP_DOWN);
dropDownShell.setLayout(new RowLayout());
dropDownShell.setVisible(false);
dropDownShell.addListener(SWT.Activate, event -> System.out.println("dropDownShell gets Activate event!"));
dropDownShell.addListener(SWT.Deactivate, event -> {
System.out.println("dropDownShell entering Deactivate event handler and will hide the dropdown widget.shell");
hideDropDown(dropDownShell);
});
dropDownShell.addListener(SWT.Close, event -> hideDropDown(dropDownShell));
// create the button1 and when it is hovered, display the dropdown
final Button button1 = new Button(shell, SWT.PUSH);
button1.setText("Drop target");
button1.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
if (!dropDownShell.isVisible()) {
showDropDown(button1, dropDownShell);
}
}
});
int operations = DND.DROP_COPY | DND.DROP_DEFAULT;
DropTarget target = new DropTarget(button1, operations);
// Provide data in Text format
Transfer[] types = new Transfer[] { TextTransfer.getInstance() };
target.setTransfer(types);
target.addDropListener(new DropTargetListener() {
@Override
public void dragEnter(DropTargetEvent event) {
if (event.detail == DND.DROP_DEFAULT) {
if ((event.operations & DND.DROP_COPY) != 0) {
event.detail = DND.DROP_COPY;
} else {
event.detail = DND.DROP_NONE;
}
}
for (int i = 0; i < event.dataTypes.length; i++) {
if (TextTransfer.getInstance().isSupportedType(event.dataTypes[i])) {
event.currentDataType = event.dataTypes[i];
if (event.detail != DND.DROP_COPY) {
event.detail = DND.DROP_NONE;
}
break;
}
}
}
@Override
public void dragOver(DropTargetEvent event) {
event.feedback = DND.FEEDBACK_SELECT;
if (!dropDownShell.isVisible()) {
showDropDown(button1, dropDownShell);
}
}
@Override
public void dragOperationChanged(DropTargetEvent event) {
}
@Override
public void dragLeave(DropTargetEvent event) {
}
@Override
public void dropAccept(DropTargetEvent event) {
}
@Override
public void drop(DropTargetEvent event) {
if (TextTransfer.getInstance().isSupportedType(event.currentDataType)) {
String text = (String) event.data;
System.out.println(text);
}
}
});
// create the button2 as the drag source
final Button button2 = new Button(shell, SWT.PUSH);
button2.setText("Drag source");
operations = DND.DROP_COPY;
DragSource source = new DragSource(button2, operations);
// Provide data in Text format
source.setTransfer(types);
source.addDragListener(new DragSourceListener() {
@Override
public void dragStart(DragSourceEvent event) {
if (button2.getText().length() == 0) {
event.doit = false;
}
}
@Override
public void dragSetData(DragSourceEvent event) {
// Provide the data of the requested type.
if (TextTransfer.getInstance().isSupportedType(event.dataType)) {
event.data = button2.getText();
}
}
@Override
public void dragFinished(DragSourceEvent event) {
}
});
shell.setSize(300, 300);
shell.addDisposeListener(e -> {
if (dropDownShell != null && !dropDownShell.isDisposed()) {
dropDownShell.dispose();
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
use of org.eclipse.swt.dnd.DragSourceListener in project nebula.widgets.nattable by eclipse.
the class NatTable method addDragSupport.
// DND /////////////////////////////////////////////////////////////////
/**
* Adds support for dragging items out of this control via a user
* drag-and-drop operation.
*
* @param operations
* a bitwise OR of the supported drag and drop operation types (
* <code>DROP_COPY</code>,<code>DROP_LINK</code>, and
* <code>DROP_MOVE</code>)
* @param transferTypes
* the transfer types that are supported by the drag operation
* @param listener
* the callback that will be invoked to set the drag data and to
* cleanup after the drag and drop operation finishes
* @see org.eclipse.swt.dnd.DND
*/
public void addDragSupport(final int operations, final Transfer[] transferTypes, final DragSourceListener listener) {
final DragSource dragSource = new DragSource(this, operations);
dragSource.setTransfer(transferTypes);
DragSourceListener wrapper = new DragSourceListener() {
@Override
public void dragStart(DragSourceEvent event) {
listener.dragStart(event);
}
@Override
public void dragSetData(DragSourceEvent event) {
listener.dragSetData(event);
}
@Override
public void dragFinished(DragSourceEvent event) {
listener.dragFinished(event);
// ensure to stop any current active internal drag mode
NatTable.this.modeSupport.switchMode(Mode.NORMAL_MODE);
}
};
dragSource.addDragListener(wrapper);
}
Aggregations