Search in sources :

Example 21 with DropTargetEvent

use of org.eclipse.swt.dnd.DropTargetEvent in project bndtools by bndtools.

the class JarListWizardPage method createControl.

@Override
public void createControl(final Composite parent) {
    setTitle("Select JARs");
    final Composite composite = new Composite(parent, SWT.NONE);
    Label lblHint = new Label(composite, SWT.WRAP);
    lblHint.setText("Selected files (hint: drag files from an external application into this list):");
    final Table table = new Table(composite, SWT.FULL_SELECTION | SWT.MULTI | SWT.BORDER);
    viewer = new TableViewer(table);
    viewer.setContentProvider(new ArrayContentProvider());
    viewer.setLabelProvider(new ClassPathLabelProvider());
    btnAdd = new Button(composite, SWT.PUSH);
    btnAdd.setText("Add");
    btnAddExternal = new Button(composite, SWT.PUSH);
    btnAddExternal.setText("Add External");
    btnRemove = new Button(composite, SWT.PUSH);
    btnRemove.setText("Remove");
    viewer.setInput(paths);
    update();
    // Listeners
    ViewerDropAdapter dropAdapter = new ViewerDropAdapter(viewer) {

        @Override
        public void dragEnter(DropTargetEvent event) {
            super.dragEnter(event);
            event.detail = DND.DROP_COPY;
        }

        @Override
        public boolean validateDrop(Object target, int operation, TransferData transferType) {
            return true;
        }

        @Override
        public boolean performDrop(Object data) {
            if (data instanceof String[]) {
                String[] newPaths = (String[]) data;
                List<IPath> added = new ArrayList<IPath>(newPaths.length);
                for (String path : newPaths) {
                    added.add(new Path(path));
                }
                if (!added.isEmpty()) {
                    addToPaths(added);
                    viewer.add(added.toArray());
                    update();
                }
            }
            return true;
        }
    };
    dropAdapter.setFeedbackEnabled(false);
    dropAdapter.setSelectionFeedbackEnabled(false);
    viewer.addDropSupport(DND.DROP_COPY | DND.DROP_MOVE, new Transfer[] { FileTransfer.getInstance() }, dropAdapter);
    viewer.addSelectionChangedListener(new ISelectionChangedListener() {

        @Override
        public void selectionChanged(final SelectionChangedEvent event) {
            update();
        }
    });
    btnAdd.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            // IResource newFile = ResourcesPlugin.getWorkspace().getRoot();
            // if(newFile != null) {
            ElementTreeSelectionDialog dialog = new ElementTreeSelectionDialog(getShell(), new WorkbenchLabelProvider(), new WorkbenchContentProvider());
            dialog.setValidator(new ISelectionStatusValidator() {

                @Override
                public IStatus validate(Object[] selection) {
                    if (selection.length > 0 && selection[0] instanceof IFile) {
                        //$NON-NLS-1$
                        return new Status(IStatus.OK, Plugin.PLUGIN_ID, IStatus.OK, "", null);
                    }
                    //$NON-NLS-1$
                    return new Status(IStatus.ERROR, Plugin.PLUGIN_ID, IStatus.ERROR, "", null);
                }
            });
            dialog.setAllowMultiple(true);
            dialog.setTitle("JAR File Selection");
            dialog.setMessage("Select one or more JAR files.");
            //$NON-NLS-1$
            dialog.addFilter(new FileExtensionFilter("jar"));
            dialog.setInput(ResourcesPlugin.getWorkspace());
            if (dialog.open() == Window.OK) {
                Object[] files = dialog.getResult();
                List<IPath> added = new ArrayList<IPath>(files.length);
                for (Object file : files) {
                    added.add(((IResource) file).getFullPath().makeRelative());
                }
                if (!added.isEmpty()) {
                    addToPaths(added);
                    viewer.add(added.toArray());
                }
            }
            // }
            update();
        }
    });
    btnAddExternal.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            FileDialog dialog = new FileDialog(getShell(), SWT.OPEN | SWT.MULTI);
            dialog.setFilterExtensions(new String[] { //$NON-NLS-1$
            "*.jar" });
            String res = dialog.open();
            if (res != null) {
                IPath filterPath = new Path(dialog.getFilterPath());
                String[] fileNames = dialog.getFileNames();
                List<IPath> added = new ArrayList<IPath>(fileNames.length);
                for (String fileName : fileNames) {
                    added.add(filterPath.append(fileName));
                }
                if (!added.isEmpty()) {
                    addToPaths(added);
                    viewer.add(added.toArray());
                }
            }
            update();
        }
    });
    btnRemove.addSelectionListener(new SelectionAdapter() {

        @SuppressWarnings("unchecked")
        @Override
        public void widgetSelected(SelectionEvent e) {
            removeFromPaths(((IStructuredSelection) viewer.getSelection()).toList());
            viewer.remove(((IStructuredSelection) viewer.getSelection()).toArray());
            update();
        }
    });
    // Layout
    composite.setLayout(new GridLayout(2, false));
    table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 3));
    btnAdd.setLayoutData(new GridData(SWT.FILL, SWT.TOP, false, false));
    btnAddExternal.setLayoutData(new GridData(SWT.FILL, SWT.TOP, false, false));
    btnRemove.setLayoutData(new GridData(SWT.FILL, SWT.TOP, false, false));
    lblHint.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, false, false, 2, 1));
    setControl(composite);
}
Also used : WorkbenchLabelProvider(org.eclipse.ui.model.WorkbenchLabelProvider) IFile(org.eclipse.core.resources.IFile) Label(org.eclipse.swt.widgets.Label) ArrayList(java.util.ArrayList) SelectionChangedEvent(org.eclipse.jface.viewers.SelectionChangedEvent) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) ClassPathLabelProvider(bndtools.utils.ClassPathLabelProvider) GridLayout(org.eclipse.swt.layout.GridLayout) Button(org.eclipse.swt.widgets.Button) TransferData(org.eclipse.swt.dnd.TransferData) SelectionEvent(org.eclipse.swt.events.SelectionEvent) FileExtensionFilter(bndtools.utils.FileExtensionFilter) ISelectionStatusValidator(org.eclipse.ui.dialogs.ISelectionStatusValidator) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) Table(org.eclipse.swt.widgets.Table) Composite(org.eclipse.swt.widgets.Composite) IPath(org.eclipse.core.runtime.IPath) ISelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) ViewerDropAdapter(org.eclipse.jface.viewers.ViewerDropAdapter) DropTargetEvent(org.eclipse.swt.dnd.DropTargetEvent) WorkbenchContentProvider(org.eclipse.ui.model.WorkbenchContentProvider) ElementTreeSelectionDialog(org.eclipse.ui.dialogs.ElementTreeSelectionDialog) ArrayContentProvider(org.eclipse.jface.viewers.ArrayContentProvider) GridData(org.eclipse.swt.layout.GridData) TableViewer(org.eclipse.jface.viewers.TableViewer) FileDialog(org.eclipse.swt.widgets.FileDialog)

Example 22 with DropTargetEvent

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

the class DNDExample method createDropTarget.

private void createDropTarget() {
    if (dropTarget != null)
        dropTarget.dispose();
    dropTarget = new DropTarget(dropControl, dropOperation);
    dropTarget.setTransfer(dropTypes);
    dropTarget.addDropListener(new DropTargetListener() {

        @Override
        public void dragEnter(DropTargetEvent event) {
            dropConsole.append(">>dragEnter\n");
            printEvent(event);
            if (event.detail == DND.DROP_DEFAULT) {
                event.detail = dropDefaultOperation;
            }
            event.feedback = dropFeedback;
        }

        @Override
        public void dragLeave(DropTargetEvent event) {
            dropConsole.append(">>dragLeave\n");
            printEvent(event);
        }

        @Override
        public void dragOperationChanged(DropTargetEvent event) {
            dropConsole.append(">>dragOperationChanged\n");
            printEvent(event);
            if (event.detail == DND.DROP_DEFAULT) {
                event.detail = dropDefaultOperation;
            }
            event.feedback = dropFeedback;
        }

        @Override
        public void dragOver(DropTargetEvent event) {
            dropConsole.append(">>dragOver\n");
            printEvent(event);
            event.feedback = dropFeedback;
        }

        @Override
        public void drop(DropTargetEvent event) {
            dropConsole.append(">>drop\n");
            printEvent(event);
            String[] strings = null;
            if (TextTransfer.getInstance().isSupportedType(event.currentDataType) || RTFTransfer.getInstance().isSupportedType(event.currentDataType) || HTMLTransfer.getInstance().isSupportedType(event.currentDataType) || URLTransfer.getInstance().isSupportedType(event.currentDataType)) {
                strings = new String[] { (String) event.data };
            }
            if (FileTransfer.getInstance().isSupportedType(event.currentDataType)) {
                strings = (String[]) event.data;
            }
            if (strings == null || strings.length == 0) {
                dropConsole.append("!!Invalid data dropped");
                return;
            }
            if (strings.length == 1 && (dropControlType == TABLE || dropControlType == TREE || dropControlType == LIST)) {
                // convert string separated by "\n" into an array of strings
                String string = strings[0];
                int count = 0;
                int offset = string.indexOf("\n", 0);
                while (offset > 0) {
                    count++;
                    offset = string.indexOf("\n", offset + 1);
                }
                if (count > 0) {
                    strings = new String[count + 1];
                    int start = 0;
                    int end = string.indexOf("\n");
                    int index = 0;
                    while (start < end) {
                        strings[index++] = string.substring(start, end);
                        start = end + 1;
                        end = string.indexOf("\n", start);
                        if (end == -1)
                            end = string.length();
                    }
                }
            }
            switch(dropControlType) {
                case BUTTON_CHECK:
                case BUTTON_TOGGLE:
                case BUTTON_RADIO:
                    {
                        Button b = (Button) dropControl;
                        b.setText(strings[0]);
                        break;
                    }
                case STYLED_TEXT:
                    {
                        StyledText text = (StyledText) dropControl;
                        for (String string : strings) {
                            text.insert(string);
                        }
                        break;
                    }
                case TABLE:
                    {
                        Table table = (Table) dropControl;
                        Point p = event.display.map(null, table, event.x, event.y);
                        TableItem dropItem = table.getItem(p);
                        int index = dropItem == null ? table.getItemCount() : table.indexOf(dropItem);
                        for (String string : strings) {
                            TableItem item = new TableItem(table, SWT.NONE, index);
                            item.setText(0, string);
                            item.setText(1, "dropped item");
                        }
                        TableColumn[] columns = table.getColumns();
                        for (TableColumn column : columns) {
                            column.pack();
                        }
                        break;
                    }
                case TEXT:
                    {
                        Text text = (Text) dropControl;
                        for (String string : strings) {
                            text.append(string + "\n");
                        }
                        break;
                    }
                case TREE:
                    {
                        Tree tree = (Tree) dropControl;
                        Point p = event.display.map(null, tree, event.x, event.y);
                        TreeItem parentItem = tree.getItem(p);
                        for (String string : strings) {
                            TreeItem item = parentItem != null ? new TreeItem(parentItem, SWT.NONE) : new TreeItem(tree, SWT.NONE);
                            item.setText(string);
                        }
                        break;
                    }
                case CANVAS:
                    {
                        dropControl.setData("STRINGS", strings);
                        dropControl.redraw();
                        break;
                    }
                case LABEL:
                    {
                        Label label = (Label) dropControl;
                        label.setText(strings[0]);
                        break;
                    }
                case LIST:
                    {
                        List list = (List) dropControl;
                        for (String string : strings) {
                            list.add(string);
                        }
                        break;
                    }
                case COMBO:
                    {
                        Combo combo = (Combo) dropControl;
                        combo.setText(strings[0]);
                        break;
                    }
                default:
                    throw new SWTError(SWT.ERROR_NOT_IMPLEMENTED);
            }
        }

        @Override
        public void dropAccept(DropTargetEvent event) {
            dropConsole.append(">>dropAccept\n");
            printEvent(event);
        }
    });
}
Also used : SWTError(org.eclipse.swt.SWTError) StyledText(org.eclipse.swt.custom.StyledText) Table(org.eclipse.swt.widgets.Table) TreeItem(org.eclipse.swt.widgets.TreeItem) TableItem(org.eclipse.swt.widgets.TableItem) Label(org.eclipse.swt.widgets.Label) DropTargetEvent(org.eclipse.swt.dnd.DropTargetEvent) StyledText(org.eclipse.swt.custom.StyledText) Text(org.eclipse.swt.widgets.Text) Combo(org.eclipse.swt.widgets.Combo) Point(org.eclipse.swt.graphics.Point) TableColumn(org.eclipse.swt.widgets.TableColumn) Point(org.eclipse.swt.graphics.Point) DropTargetListener(org.eclipse.swt.dnd.DropTargetListener) Button(org.eclipse.swt.widgets.Button) Tree(org.eclipse.swt.widgets.Tree) List(org.eclipse.swt.widgets.List) DropTarget(org.eclipse.swt.dnd.DropTarget)

Example 23 with DropTargetEvent

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

the class FileViewer method createTreeDropTarget.

/**
 * Creates the Drag & Drop DropTarget for items being dropped onto the tree.
 *
 * @return the DropTarget for the tree
 */
private DropTarget createTreeDropTarget(final Tree tree) {
    DropTarget dropTarget = new DropTarget(tree, DND.DROP_MOVE | DND.DROP_COPY);
    dropTarget.setTransfer(FileTransfer.getInstance());
    dropTarget.addDropListener(new DropTargetAdapter() {

        @Override
        public void dragEnter(DropTargetEvent event) {
            isDropping = true;
        }

        @Override
        public void dragLeave(DropTargetEvent event) {
            isDropping = false;
            handleDeferredRefresh();
        }

        @Override
        public void dragOver(DropTargetEvent event) {
            dropTargetValidate(event, getTargetFile(event));
            event.feedback |= DND.FEEDBACK_EXPAND | DND.FEEDBACK_SCROLL;
        }

        @Override
        public void drop(DropTargetEvent event) {
            File targetFile = getTargetFile(event);
            if (dropTargetValidate(event, targetFile))
                dropTargetHandleDrop(event, targetFile);
        }

        private File getTargetFile(DropTargetEvent event) {
            // Determine the target File for the drop
            TreeItem item = tree.getItem(tree.toControl(new Point(event.x, event.y)));
            File targetFile = null;
            if (item != null) {
                // We are over a particular item in the tree, use the item's file
                targetFile = (File) item.getData(TREEITEMDATA_FILE);
            }
            return targetFile;
        }
    });
    return dropTarget;
}
Also used : DropTargetAdapter(org.eclipse.swt.dnd.DropTargetAdapter) TreeItem(org.eclipse.swt.widgets.TreeItem) DropTargetEvent(org.eclipse.swt.dnd.DropTargetEvent) DropTarget(org.eclipse.swt.dnd.DropTarget) Point(org.eclipse.swt.graphics.Point) File(java.io.File)

Example 24 with DropTargetEvent

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

the class FileViewer method createTableDropTarget.

/**
 * Creates the Drag & Drop DropTarget for items being dropped onto the table.
 *
 * @return the DropTarget for the table
 */
private DropTarget createTableDropTarget(final Table table) {
    DropTarget dropTarget = new DropTarget(table, DND.DROP_MOVE | DND.DROP_COPY);
    dropTarget.setTransfer(FileTransfer.getInstance());
    dropTarget.addDropListener(new DropTargetAdapter() {

        @Override
        public void dragEnter(DropTargetEvent event) {
            isDropping = true;
        }

        @Override
        public void dragLeave(DropTargetEvent event) {
            isDropping = false;
            handleDeferredRefresh();
        }

        @Override
        public void dragOver(DropTargetEvent event) {
            dropTargetValidate(event, getTargetFile(event));
            event.feedback |= DND.FEEDBACK_EXPAND | DND.FEEDBACK_SCROLL;
        }

        @Override
        public void drop(DropTargetEvent event) {
            File targetFile = getTargetFile(event);
            if (dropTargetValidate(event, targetFile))
                dropTargetHandleDrop(event, targetFile);
        }

        private File getTargetFile(DropTargetEvent event) {
            // Determine the target File for the drop
            TableItem item = table.getItem(table.toControl(new Point(event.x, event.y)));
            File targetFile = null;
            if (item == null) {
                // If it is a COPY, we can use the table's root file.
                if (event.detail == DND.DROP_COPY) {
                    targetFile = (File) table.getData(TABLEDATA_DIR);
                }
            } else {
                // We are over a particular item in the table, use the item's file
                targetFile = (File) item.getData(TABLEITEMDATA_FILE);
            }
            return targetFile;
        }
    });
    return dropTarget;
}
Also used : DropTargetAdapter(org.eclipse.swt.dnd.DropTargetAdapter) TableItem(org.eclipse.swt.widgets.TableItem) DropTargetEvent(org.eclipse.swt.dnd.DropTargetEvent) DropTarget(org.eclipse.swt.dnd.DropTarget) Point(org.eclipse.swt.graphics.Point) File(java.io.File)

Example 25 with DropTargetEvent

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

the class Bug307441_DnDOverlappingControls method setDropTargetForLabel.

public static void setDropTargetForLabel(final Label control) {
    int operations = DND.DROP_MOVE | DND.DROP_COPY | DND.DROP_LINK;
    DropTarget target = new DropTarget(control, operations);
    target.setTransfer(new Transfer[] { URLTransfer.getInstance() });
    target.addDropListener(new DropTargetListener() {

        @Override
        public void dragEnter(DropTargetEvent e) {
            System.out.println("dragEnter=" + control.getText());
            if (e.detail == DND.DROP_NONE)
                e.detail = DND.DROP_LINK;
        }

        @Override
        public void dragOperationChanged(DropTargetEvent e) {
            if (e.detail == DND.DROP_NONE)
                e.detail = DND.DROP_LINK;
        }

        @Override
        public void drop(DropTargetEvent event) {
            if (event.data == null) {
                event.detail = DND.DROP_NONE;
                return;
            }
            control.setText(((String) event.data));
        }

        @Override
        public void dragLeave(DropTargetEvent arg0) {
            // TODO Auto-generated method stub
            System.out.println("dragLeave=" + control.getText());
        }

        @Override
        public void dragOver(DropTargetEvent arg0) {
        // TODO Auto-generated method stub
        }

        @Override
        public void dropAccept(DropTargetEvent arg0) {
        // TODO Auto-generated method stub
        }
    });
}
Also used : DropTargetListener(org.eclipse.swt.dnd.DropTargetListener) DropTargetEvent(org.eclipse.swt.dnd.DropTargetEvent) DropTarget(org.eclipse.swt.dnd.DropTarget)

Aggregations

DropTargetEvent (org.eclipse.swt.dnd.DropTargetEvent)28 DropTarget (org.eclipse.swt.dnd.DropTarget)20 DropTargetAdapter (org.eclipse.swt.dnd.DropTargetAdapter)13 Point (org.eclipse.swt.graphics.Point)9 DropTargetListener (org.eclipse.swt.dnd.DropTargetListener)8 Transfer (org.eclipse.swt.dnd.Transfer)7 TextTransfer (org.eclipse.swt.dnd.TextTransfer)6 DragSource (org.eclipse.swt.dnd.DragSource)5 DragSourceEvent (org.eclipse.swt.dnd.DragSourceEvent)5 TreeItem (org.eclipse.swt.widgets.TreeItem)5 File (java.io.File)4 ArrayList (java.util.ArrayList)4 ISelectionChangedListener (org.eclipse.jface.viewers.ISelectionChangedListener)4 SelectionChangedEvent (org.eclipse.jface.viewers.SelectionChangedEvent)4 GridData (org.eclipse.swt.layout.GridData)4 GridLayout (org.eclipse.swt.layout.GridLayout)3 RepositoryBundle (bndtools.model.repo.RepositoryBundle)2 RepositoryBundleVersion (bndtools.model.repo.RepositoryBundleVersion)2 URI (java.net.URI)2 List (java.util.List)2