Search in sources :

Example 6 with DragSourceListener

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;
                }
            }
        }
    });
}
Also used : MalformedURLException(java.net.MalformedURLException) TreeItem(org.eclipse.swt.widgets.TreeItem) TableItem(org.eclipse.swt.widgets.TableItem) Label(org.eclipse.swt.widgets.Label) Combo(org.eclipse.swt.widgets.Combo) DragSourceListener(org.eclipse.swt.dnd.DragSourceListener) RTFTransfer(org.eclipse.swt.dnd.RTFTransfer) URL(java.net.URL) Button(org.eclipse.swt.widgets.Button) Tree(org.eclipse.swt.widgets.Tree) List(org.eclipse.swt.widgets.List) SWTError(org.eclipse.swt.SWTError) StyledText(org.eclipse.swt.custom.StyledText) Table(org.eclipse.swt.widgets.Table) URLTransfer(org.eclipse.swt.dnd.URLTransfer) FileTransfer(org.eclipse.swt.dnd.FileTransfer) StyledText(org.eclipse.swt.custom.StyledText) Text(org.eclipse.swt.widgets.Text) HTMLTransfer(org.eclipse.swt.dnd.HTMLTransfer) DragSource(org.eclipse.swt.dnd.DragSource) Point(org.eclipse.swt.graphics.Point) Point(org.eclipse.swt.graphics.Point) URLTransfer(org.eclipse.swt.dnd.URLTransfer) Transfer(org.eclipse.swt.dnd.Transfer) HTMLTransfer(org.eclipse.swt.dnd.HTMLTransfer) RTFTransfer(org.eclipse.swt.dnd.RTFTransfer) TextTransfer(org.eclipse.swt.dnd.TextTransfer) FileTransfer(org.eclipse.swt.dnd.FileTransfer) DragSourceEvent(org.eclipse.swt.dnd.DragSourceEvent) TextTransfer(org.eclipse.swt.dnd.TextTransfer)

Example 7 with DragSourceListener

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;
}
Also used : DragSourceEvent(org.eclipse.swt.dnd.DragSourceEvent) TableItem(org.eclipse.swt.widgets.TableItem) DragSource(org.eclipse.swt.dnd.DragSource) DragSourceListener(org.eclipse.swt.dnd.DragSourceListener) File(java.io.File)

Example 8 with DragSourceListener

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) {
        }
    });
}
Also used : DragSourceEvent(org.eclipse.swt.dnd.DragSourceEvent) DragSource(org.eclipse.swt.dnd.DragSource) DragSourceListener(org.eclipse.swt.dnd.DragSourceListener)

Example 9 with DragSourceListener

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();
}
Also used : SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) DropTargetEvent(org.eclipse.swt.dnd.DropTargetEvent) DragSource(org.eclipse.swt.dnd.DragSource) DragSourceListener(org.eclipse.swt.dnd.DragSourceListener) Point(org.eclipse.swt.graphics.Point) DragSourceEvent(org.eclipse.swt.dnd.DragSourceEvent) Shell(org.eclipse.swt.widgets.Shell) GridLayout(org.eclipse.swt.layout.GridLayout) DropTargetListener(org.eclipse.swt.dnd.DropTargetListener) Button(org.eclipse.swt.widgets.Button) RowLayout(org.eclipse.swt.layout.RowLayout) SelectionEvent(org.eclipse.swt.events.SelectionEvent) Transfer(org.eclipse.swt.dnd.Transfer) TextTransfer(org.eclipse.swt.dnd.TextTransfer) DropTarget(org.eclipse.swt.dnd.DropTarget) Display(org.eclipse.swt.widgets.Display)

Example 10 with DragSourceListener

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);
}
Also used : DragSourceEvent(org.eclipse.swt.dnd.DragSourceEvent) DragSource(org.eclipse.swt.dnd.DragSource) DragSourceListener(org.eclipse.swt.dnd.DragSourceListener)

Aggregations

DragSourceListener (org.eclipse.swt.dnd.DragSourceListener)25 DragSource (org.eclipse.swt.dnd.DragSource)22 DragSourceEvent (org.eclipse.swt.dnd.DragSourceEvent)19 Transfer (org.eclipse.swt.dnd.Transfer)13 TextTransfer (org.eclipse.swt.dnd.TextTransfer)8 TransferDragSourceListener (org.eclipse.jface.util.TransferDragSourceListener)6 DropTarget (org.eclipse.swt.dnd.DropTarget)6 DropTargetEvent (org.eclipse.swt.dnd.DropTargetEvent)6 DropTargetListener (org.eclipse.swt.dnd.DropTargetListener)5 SelectionAdapter (org.eclipse.swt.events.SelectionAdapter)5 SelectionEvent (org.eclipse.swt.events.SelectionEvent)5 Point (org.eclipse.swt.graphics.Point)5 Button (org.eclipse.swt.widgets.Button)5 TreeItem (org.eclipse.swt.widgets.TreeItem)5 Composite (org.eclipse.swt.widgets.Composite)4 Display (org.eclipse.swt.widgets.Display)4 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)3 KeyAdapter (org.eclipse.swt.events.KeyAdapter)3 KeyEvent (org.eclipse.swt.events.KeyEvent)3 File (java.io.File)2