Search in sources :

Example 21 with DragSource

use of org.eclipse.swt.dnd.DragSource in project eclipse.platform.swt by eclipse.

the class Bug497705_BrowserDragDetect method setDrag.

public static void setDrag(final Browser browser) {
    Transfer[] types = new Transfer[] { TextTransfer.getInstance() };
    int operations = DND.DROP_MOVE | DND.DROP_COPY | DND.DROP_LINK;
    final DragSource source = new DragSource(browser, operations);
    source.setTransfer(types);
    source.addDragListener(new DragSourceListener() {

        @Override
        public void dragStart(DragSourceEvent event) {
            event.doit = (browser.getText().length() != 0);
            // not ran.
            System.out.println("Drag started");
        }

        @Override
        public void dragSetData(DragSourceEvent event) {
            event.data = browser.getText();
        }

        @Override
        public void dragFinished(DragSourceEvent event) {
            if (event.detail == DND.DROP_MOVE)
                browser.setText("");
        }
    });
}
Also used : DragSourceEvent(org.eclipse.swt.dnd.DragSourceEvent) Transfer(org.eclipse.swt.dnd.Transfer) TextTransfer(org.eclipse.swt.dnd.TextTransfer) DragSource(org.eclipse.swt.dnd.DragSource) DragSourceListener(org.eclipse.swt.dnd.DragSourceListener)

Example 22 with DragSource

use of org.eclipse.swt.dnd.DragSource in project eclipse.platform.swt by eclipse.

the class DNDExample method createDragWidget.

private void createDragWidget(Composite parent) {
    parent.setLayout(new FormLayout());
    Combo combo = new Combo(parent, SWT.READ_ONLY);
    combo.setItems("Toggle Button", "Radio Button", "Checkbox", "Canvas", "Label", "List", "Table", "Tree", "Text", "StyledText", "Combo");
    combo.select(LABEL);
    dragControlType = combo.getSelectionIndex();
    dragControl = createWidget(dragControlType, parent, "Drag Source");
    combo.addSelectionListener(widgetSelectedAdapter(e -> {
        Object data = dragControl.getLayoutData();
        Composite dragParent = dragControl.getParent();
        dragControl.dispose();
        Combo c = (Combo) e.widget;
        dragControlType = c.getSelectionIndex();
        dragControl = createWidget(dragControlType, dragParent, "Drag Source");
        dragControl.setLayoutData(data);
        if (dragEnabled)
            createDragSource();
        dragParent.layout();
    }));
    Button b = new Button(parent, SWT.CHECK);
    b.setText("DragSource");
    b.addSelectionListener(widgetSelectedAdapter(e -> {
        Button b1 = (Button) e.widget;
        dragEnabled = b1.getSelection();
        if (dragEnabled) {
            createDragSource();
        } else {
            if (dragSource != null) {
                dragSource.dispose();
            }
            dragSource = null;
        }
    }));
    b.setSelection(true);
    dragEnabled = true;
    FormData data = new FormData();
    data.top = new FormAttachment(0, 10);
    data.bottom = new FormAttachment(combo, -10);
    data.left = new FormAttachment(0, 10);
    data.right = new FormAttachment(100, -10);
    dragControl.setLayoutData(data);
    data = new FormData();
    data.bottom = new FormAttachment(100, -10);
    data.left = new FormAttachment(0, 10);
    combo.setLayoutData(data);
    data = new FormData();
    data.bottom = new FormAttachment(100, -10);
    data.left = new FormAttachment(combo, 10);
    b.setLayoutData(data);
}
Also used : FormLayout(org.eclipse.swt.layout.FormLayout) StyledText(org.eclipse.swt.custom.StyledText) URL(java.net.URL) DND(org.eclipse.swt.dnd.DND) TableColumn(org.eclipse.swt.widgets.TableColumn) FontMetrics(org.eclipse.swt.graphics.FontMetrics) Point(org.eclipse.swt.graphics.Point) SWTError(org.eclipse.swt.SWTError) DropTargetEvent(org.eclipse.swt.dnd.DropTargetEvent) URLTransfer(org.eclipse.swt.dnd.URLTransfer) Composite(org.eclipse.swt.widgets.Composite) TreeColumn(org.eclipse.swt.widgets.TreeColumn) DropTarget(org.eclipse.swt.dnd.DropTarget) Text(org.eclipse.swt.widgets.Text) Button(org.eclipse.swt.widgets.Button) Display(org.eclipse.swt.widgets.Display) Transfer(org.eclipse.swt.dnd.Transfer) MenuItem(org.eclipse.swt.widgets.MenuItem) Tree(org.eclipse.swt.widgets.Tree) SWT(org.eclipse.swt.SWT) HTMLTransfer(org.eclipse.swt.dnd.HTMLTransfer) List(org.eclipse.swt.widgets.List) DragSourceEvent(org.eclipse.swt.dnd.DragSourceEvent) Label(org.eclipse.swt.widgets.Label) RTFTransfer(org.eclipse.swt.dnd.RTFTransfer) DragSourceListener(org.eclipse.swt.dnd.DragSourceListener) Image(org.eclipse.swt.graphics.Image) Rectangle(org.eclipse.swt.graphics.Rectangle) Table(org.eclipse.swt.widgets.Table) SelectionListener.widgetSelectedAdapter(org.eclipse.swt.events.SelectionListener.widgetSelectedAdapter) TextTransfer(org.eclipse.swt.dnd.TextTransfer) Canvas(org.eclipse.swt.widgets.Canvas) GridData(org.eclipse.swt.layout.GridData) FillLayout(org.eclipse.swt.layout.FillLayout) FileTransfer(org.eclipse.swt.dnd.FileTransfer) DragSource(org.eclipse.swt.dnd.DragSource) TableItem(org.eclipse.swt.widgets.TableItem) Combo(org.eclipse.swt.widgets.Combo) Shell(org.eclipse.swt.widgets.Shell) MalformedURLException(java.net.MalformedURLException) DropTargetListener(org.eclipse.swt.dnd.DropTargetListener) FormLayout(org.eclipse.swt.layout.FormLayout) FileDialog(org.eclipse.swt.widgets.FileDialog) FormData(org.eclipse.swt.layout.FormData) FormAttachment(org.eclipse.swt.layout.FormAttachment) Group(org.eclipse.swt.widgets.Group) TreeItem(org.eclipse.swt.widgets.TreeItem) File(java.io.File) RowLayout(org.eclipse.swt.layout.RowLayout) Menu(org.eclipse.swt.widgets.Menu) ScrolledComposite(org.eclipse.swt.custom.ScrolledComposite) Control(org.eclipse.swt.widgets.Control) GridLayout(org.eclipse.swt.layout.GridLayout) FormData(org.eclipse.swt.layout.FormData) Composite(org.eclipse.swt.widgets.Composite) ScrolledComposite(org.eclipse.swt.custom.ScrolledComposite) Button(org.eclipse.swt.widgets.Button) Combo(org.eclipse.swt.widgets.Combo) FormAttachment(org.eclipse.swt.layout.FormAttachment)

Example 23 with DragSource

use of org.eclipse.swt.dnd.DragSource in project eclipse.platform.swt by eclipse.

the class FileViewer method createTreeDragSource.

/**
 * Creates the Drag & Drop DragSource for items being dragged from the tree.
 *
 * @return the DragSource for the tree
 */
private DragSource createTreeDragSource(final Tree tree) {
    DragSource dragSource = new DragSource(tree, DND.DROP_MOVE | DND.DROP_COPY);
    dragSource.setTransfer(FileTransfer.getInstance());
    dragSource.addDragListener(new DragSourceListener() {

        TreeItem[] dndSelection = null;

        String[] sourceNames = null;

        @Override
        public void dragStart(DragSourceEvent event) {
            dndSelection = tree.getSelection();
            sourceNames = null;
            event.doit = dndSelection.length > 0;
            isDragging = true;
            processedDropFiles = null;
        }

        @Override
        public void dragFinished(DragSourceEvent event) {
            dragSourceHandleDragFinished(event, sourceNames);
            dndSelection = null;
            sourceNames = null;
            isDragging = false;
            processedDropFiles = null;
            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(TREEITEMDATA_FILE);
                sourceNames[i] = file.getAbsolutePath();
            }
            event.data = sourceNames;
        }
    });
    return dragSource;
}
Also used : DragSourceEvent(org.eclipse.swt.dnd.DragSourceEvent) TreeItem(org.eclipse.swt.widgets.TreeItem) DragSource(org.eclipse.swt.dnd.DragSource) DragSourceListener(org.eclipse.swt.dnd.DragSourceListener) File(java.io.File)

Example 24 with DragSource

use of org.eclipse.swt.dnd.DragSource in project eclipse.platform.swt by eclipse.

the class Bug66356_DND_move_reported_to_drag_source_even_though_cancelled method main.

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    Button source = new Button(shell, SWT.PUSH);
    source.setText("Source");
    DragSource dragSource = new DragSource(source, DND.DROP_MOVE);
    dragSource.setTransfer(new Transfer[] { TextTransfer.getInstance() });
    dragSource.addDragListener(new DragSourceAdapter() {

        @Override
        public void dragSetData(DragSourceEvent event) {
            event.data = "hello";
        }

        @Override
        public void dragFinished(DragSourceEvent event) {
            System.out.println("Move = " + (event.detail == DND.DROP_MOVE));
        }
    });
    Button dest = new Button(shell, SWT.PUSH);
    dest.setText("Dest");
    DropTarget dropTarget = new DropTarget(dest, DND.DROP_MOVE);
    dropTarget.setTransfer(new Transfer[] { TextTransfer.getInstance() });
    // dropTarget.addDropListener(new DropTargetAdapter() {
    // public void drop(DropTargetEvent event) {
    // event.detail = DND.DROP_NONE;
    // }
    // });
    // dropTarget.addDropListener(new DropTargetListener() {
    // 
    // @Override
    // public void dropAccept(DropTargetEvent event) {
    // // TD Auto-generated method stub
    // event.detail = DND.DROP_NONE;
    // 
    // }
    // 
    // @Override
    // public void drop(DropTargetEvent event) {
    // // TD Auto-generated method stub
    // 
    // }
    // 
    // @Override
    // public void dragOver(DropTargetEvent event) {
    // // TD Auto-generated method stub
    // 
    // }
    // 
    // @Override
    // public void dragOperationChanged(DropTargetEvent event) {
    // // TD Auto-generated method stub
    // 
    // }
    // 
    // @Override
    // public void dragLeave(DropTargetEvent event) {
    // // TD Auto-generated method stub
    // 
    // }
    // 
    // @Override
    // public void dragEnter(DropTargetEvent event) {
    // // TD Auto-generated method stub
    // 
    // }
    // });
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}
Also used : DragSourceEvent(org.eclipse.swt.dnd.DragSourceEvent) DragSourceAdapter(org.eclipse.swt.dnd.DragSourceAdapter) Shell(org.eclipse.swt.widgets.Shell) Button(org.eclipse.swt.widgets.Button) FillLayout(org.eclipse.swt.layout.FillLayout) DragSource(org.eclipse.swt.dnd.DragSource) DropTarget(org.eclipse.swt.dnd.DropTarget) Display(org.eclipse.swt.widgets.Display)

Example 25 with DragSource

use of org.eclipse.swt.dnd.DragSource in project eclipse.platform.text by eclipse.

the class AbstractTextEditor method uninstallTextDragAndDrop.

/**
 * Uninstalls text drag and drop from the given source viewer.
 *
 * @param viewer the viewer
 * @since 3.3
 */
protected void uninstallTextDragAndDrop(ISourceViewer viewer) {
    if (viewer == null || !fIsTextDragAndDropInstalled)
        return;
    final IDragAndDropService dndService = getSite().getService(IDragAndDropService.class);
    if (dndService == null)
        return;
    StyledText st = viewer.getTextWidget();
    dndService.removeMergedDropTarget(st);
    DragSource dragSource = (DragSource) st.getData(DND.DRAG_SOURCE_KEY);
    if (dragSource != null) {
        dragSource.dispose();
        st.setData(DND.DRAG_SOURCE_KEY, null);
    }
    fIsTextDragAndDropInstalled = false;
}
Also used : StyledText(org.eclipse.swt.custom.StyledText) IDragAndDropService(org.eclipse.ui.dnd.IDragAndDropService) DragSource(org.eclipse.swt.dnd.DragSource)

Aggregations

DragSource (org.eclipse.swt.dnd.DragSource)26 DragSourceListener (org.eclipse.swt.dnd.DragSourceListener)16 DragSourceEvent (org.eclipse.swt.dnd.DragSourceEvent)15 Transfer (org.eclipse.swt.dnd.Transfer)7 TransferDragSourceListener (org.eclipse.jface.util.TransferDragSourceListener)6 DropTarget (org.eclipse.swt.dnd.DropTarget)6 DropTargetEvent (org.eclipse.swt.dnd.DropTargetEvent)6 TextTransfer (org.eclipse.swt.dnd.TextTransfer)6 Point (org.eclipse.swt.graphics.Point)6 DragSourceAdapter (org.eclipse.swt.dnd.DragSourceAdapter)5 TreeItem (org.eclipse.swt.widgets.TreeItem)5 StyledText (org.eclipse.swt.custom.StyledText)4 DropTargetListener (org.eclipse.swt.dnd.DropTargetListener)4 Button (org.eclipse.swt.widgets.Button)4 Display (org.eclipse.swt.widgets.Display)4 Shell (org.eclipse.swt.widgets.Shell)4 File (java.io.File)3 MalformedURLException (java.net.MalformedURLException)2 URL (java.net.URL)2 SWTError (org.eclipse.swt.SWTError)2