Search in sources :

Example 11 with DropTargetEvent

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

the class RepositoryBundleSelectionPart method createSection.

void createSection(Section section, FormToolkit toolkit) {
    // Toolbar buttons
    ToolBar toolbar = new ToolBar(section, SWT.FLAT);
    section.setTextClient(toolbar);
    fillToolBar(toolbar);
    Composite composite = toolkit.createComposite(section);
    section.setClient(composite);
    table = toolkit.createTable(composite, SWT.FULL_SELECTION | SWT.MULTI | SWT.BORDER | SWT.H_SCROLL);
    viewer = new TableViewer(table);
    viewer.setContentProvider(new ArrayContentProvider());
    viewer.setLabelProvider(getLabelProvider());
    // Listeners
    viewer.addSelectionChangedListener(new ISelectionChangedListener() {

        @Override
        public void selectionChanged(SelectionChangedEvent event) {
            ToolItem remove = getRemoveItemTool();
            if (remove != null)
                remove.setEnabled(isRemovable(event.getSelection()));
        }
    });
    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) {
            if (FileTransfer.getInstance().isSupportedType(transferType))
                return true;
            if (ResourceTransfer.getInstance().isSupportedType(transferType))
                return true;
            if (URLTransfer.getInstance().isSupportedType(transferType))
                return true;
            ISelection selection = LocalSelectionTransfer.getTransfer().getSelection();
            if (selection.isEmpty() || !(selection instanceof IStructuredSelection)) {
                return false;
            }
            Iterator<?> iterator = ((IStructuredSelection) selection).iterator();
            while (iterator.hasNext()) {
                if (!selectionIsDroppable(iterator.next())) {
                    return false;
                }
            }
            return true;
        }

        private boolean selectionIsDroppable(Object element) {
            return element instanceof RepositoryBundle || element instanceof RepositoryBundleVersion || element instanceof ProjectBundle || element instanceof RepositoryResourceElement;
        }

        @Override
        public boolean performDrop(Object data) {
            TransferData transfer = getCurrentEvent().currentDataType;
            if (URLTransfer.getInstance().isSupportedType(transfer)) {
                String urlStr = (String) URLTransfer.getInstance().nativeToJava(transfer);
                return handleURLDrop(urlStr);
            } else if (data instanceof String[]) {
                return handleFileNameDrop((String[]) data);
            } else if (data instanceof IResource[]) {
                return handleResourceDrop((IResource[]) data);
            } else {
                return handleSelectionDrop();
            }
        }

        private boolean handleResourceDrop(IResource[] resources) {
            File[] files = new File[resources.length];
            for (int i = 0; i < resources.length; i++) {
                files[i] = resources[i].getLocation().toFile();
            }
            return handleFileDrop(files);
        }

        private boolean handleFileNameDrop(String[] paths) {
            File[] files = new File[paths.length];
            for (int i = 0; i < paths.length; i++) {
                files[i] = new File(paths[i]);
            }
            return handleFileDrop(files);
        }

        private boolean handleFileDrop(File[] files) {
            if (files.length > 0) {
                BndPreferences prefs = new BndPreferences();
                boolean hideWarning = prefs.getHideWarningExternalFile();
                if (!hideWarning) {
                    MessageDialogWithToggle dialog = MessageDialogWithToggle.openWarning(getSection().getShell(), "Add External Files", "External files cannot be directly added to a project, they must be added to a local repository first.", "Do not show this warning again.", false, null, null);
                    if (Window.CANCEL == dialog.getReturnCode())
                        return false;
                    if (dialog.getToggleState()) {
                        prefs.setHideWarningExternalFile(true);
                    }
                }
                AddFilesToRepositoryWizard wizard = new AddFilesToRepositoryWizard(null, files);
                WizardDialog dialog = new WizardDialog(getSection().getShell(), wizard);
                if (Window.OK == dialog.open()) {
                    List<Pair<String, String>> addingBundles = wizard.getSelectedBundles();
                    List<VersionedClause> addingClauses = new ArrayList<VersionedClause>(addingBundles.size());
                    for (Pair<String, String> addingBundle : addingBundles) {
                        Attrs attribs = new Attrs();
                        attribs.put(Constants.VERSION_ATTRIBUTE, addingBundle.getSecond());
                        addingClauses.add(new VersionedClause(addingBundle.getFirst(), attribs));
                    }
                    handleAdd(addingClauses);
                }
                return true;
            }
            return false;
        }

        private boolean handleSelectionDrop() {
            ISelection selection = LocalSelectionTransfer.getTransfer().getSelection();
            if (selection.isEmpty() || !(selection instanceof IStructuredSelection)) {
                return false;
            }
            List<VersionedClause> adding = new LinkedList<VersionedClause>();
            Iterator<?> iterator = ((IStructuredSelection) selection).iterator();
            while (iterator.hasNext()) {
                Object item = iterator.next();
                if (item instanceof RepositoryBundle) {
                    VersionedClause newClause = RepositoryBundleUtils.convertRepoBundle((RepositoryBundle) item);
                    adding.add(newClause);
                } else if (item instanceof RepositoryBundleVersion) {
                    RepositoryBundleVersion bundleVersion = (RepositoryBundleVersion) item;
                    VersionedClause newClause = RepositoryBundleUtils.convertRepoBundleVersion(bundleVersion, phase);
                    adding.add(newClause);
                } else if (item instanceof RepositoryResourceElement) {
                    RepositoryResourceElement elt = (RepositoryResourceElement) item;
                    VersionedClause newClause = RepositoryBundleUtils.convertRepoBundleVersion(elt.getRepositoryBundleVersion(), phase);
                    adding.add(newClause);
                }
            }
            handleAdd(adding);
            return true;
        }

        private boolean handleURLDrop(String urlStr) {
            try {
                URI uri = new URI(sanitizeUrl(urlStr));
                AddJpmDependenciesWizard wizard = new AddJpmDependenciesWizard(uri);
                WizardDialog dialog = new WizardDialog(getSection().getShell(), wizard);
                if (dialog.open() == Window.OK) {
                    Set<ResourceDescriptor> resources = wizard.getResult();
                    List<VersionedClause> newBundles = new ArrayList<VersionedClause>(resources.size());
                    for (ResourceDescriptor resource : resources) {
                        Attrs attrs = new Attrs();
                        attrs.put(Constants.VERSION_ATTRIBUTE, resource.version != null ? resource.version.toString() : Version.emptyVersion.toString());
                        VersionedClause clause = new VersionedClause(resource.bsn, attrs);
                        newBundles.add(clause);
                    }
                    handleAdd(newBundles);
                    return true;
                }
                return false;
            } catch (URISyntaxException e) {
                MessageDialog.openError(getSection().getShell(), "Error", "The dropped URL was invalid: " + urlStr);
                return false;
            }
        }

        private String sanitizeUrl(String urlStr) {
            int newline = urlStr.indexOf('\n');
            if (newline > -1)
                return urlStr.substring(0, newline).trim();
            return urlStr;
        }

        private void handleAdd(Collection<VersionedClause> newClauses) {
            if (newClauses == null || newClauses.isEmpty())
                return;
            List<VersionedClause> toAdd = new LinkedList<VersionedClause>();
            for (VersionedClause newClause : newClauses) {
                boolean found = false;
                for (ListIterator<VersionedClause> iter = bundles.listIterator(); iter.hasNext(); ) {
                    VersionedClause existing = iter.next();
                    if (newClause.getName().equals(existing.getName())) {
                        int index = iter.previousIndex();
                        iter.set(newClause);
                        viewer.replace(newClause, index);
                        found = true;
                        break;
                    }
                }
                if (!found)
                    toAdd.add(newClause);
            }
            bundles.addAll(toAdd);
            viewer.add(toAdd.toArray());
            markDirty();
        }
    };
    dropAdapter.setFeedbackEnabled(false);
    dropAdapter.setExpandEnabled(false);
    viewer.addDropSupport(DND.DROP_COPY | DND.DROP_MOVE, new Transfer[] { LocalSelectionTransfer.getTransfer(), FileTransfer.getInstance(), ResourceTransfer.getInstance(), URLTransfer.getInstance() }, dropAdapter);
    table.addKeyListener(new KeyAdapter() {

        @Override
        public void keyReleased(KeyEvent e) {
            if (e.character == SWT.DEL) {
                doRemove();
            } else if (e.character == '+') {
                doAdd();
            }
        }
    });
    // Layout
    GridLayout layout = new GridLayout(1, false);
    layout.horizontalSpacing = 0;
    layout.verticalSpacing = 0;
    layout.marginHeight = 0;
    layout.marginWidth = 0;
    composite.setLayout(layout);
    GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
    gd.widthHint = 50;
    gd.heightHint = getTableHeightHint();
    table.setLayoutData(gd);
}
Also used : RepositoryBundle(bndtools.model.repo.RepositoryBundle) RepositoryBundleVersion(bndtools.model.repo.RepositoryBundleVersion) BndPreferences(bndtools.preferences.BndPreferences) VersionedClause(aQute.bnd.build.model.clauses.VersionedClause) KeyAdapter(org.eclipse.swt.events.KeyAdapter) ArrayList(java.util.ArrayList) SelectionChangedEvent(org.eclipse.jface.viewers.SelectionChangedEvent) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) ProjectBundle(bndtools.model.repo.ProjectBundle) MessageDialogWithToggle(org.eclipse.jface.dialogs.MessageDialogWithToggle) ISelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener) DropTargetEvent(org.eclipse.swt.dnd.DropTargetEvent) LinkedList(java.util.LinkedList) Collection(java.util.Collection) File(java.io.File) AddFilesToRepositoryWizard(bndtools.wizards.workspace.AddFilesToRepositoryWizard) Attrs(aQute.bnd.header.Attrs) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) KeyEvent(org.eclipse.swt.events.KeyEvent) GridLayout(org.eclipse.swt.layout.GridLayout) TransferData(org.eclipse.swt.dnd.TransferData) ISelection(org.eclipse.jface.viewers.ISelection) ToolItem(org.eclipse.swt.widgets.ToolItem) Pair(bndtools.types.Pair) Composite(org.eclipse.swt.widgets.Composite) ViewerDropAdapter(org.eclipse.jface.viewers.ViewerDropAdapter) RepositoryResourceElement(bndtools.model.repo.RepositoryResourceElement) ToolBar(org.eclipse.swt.widgets.ToolBar) ArrayContentProvider(org.eclipse.jface.viewers.ArrayContentProvider) GridData(org.eclipse.swt.layout.GridData) AddJpmDependenciesWizard(org.bndtools.core.ui.wizards.jpm.AddJpmDependenciesWizard) TableViewer(org.eclipse.jface.viewers.TableViewer) WizardDialog(org.eclipse.jface.wizard.WizardDialog) IResource(org.eclipse.core.resources.IResource) ResourceDescriptor(aQute.bnd.service.repository.SearchableRepository.ResourceDescriptor)

Example 12 with DropTargetEvent

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

the class Bug307441_DnDOverlappingControls method setDropTarget.

public static void setDropTarget(final Control 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=composite");
            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.setBackground(new Color(null, 0, 0, 255));
        }

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

        @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) Color(org.eclipse.swt.graphics.Color) DropTargetEvent(org.eclipse.swt.dnd.DropTargetEvent) DropTarget(org.eclipse.swt.dnd.DropTarget)

Example 13 with DropTargetEvent

use of org.eclipse.swt.dnd.DropTargetEvent 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 14 with DropTargetEvent

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

the class Bug497705_BrowserDragDetect method setDrop.

public static void setDrop(final Label label) {
    Transfer[] types = new Transfer[] { TextTransfer.getInstance() };
    int operations = DND.DROP_MOVE | DND.DROP_COPY | DND.DROP_LINK;
    DropTarget target = new DropTarget(label, operations);
    target.setTransfer(types);
    target.addDropListener(new DropTargetAdapter() {

        @Override
        public void drop(DropTargetEvent event) {
            if (event.data == null) {
                event.detail = DND.DROP_NONE;
                return;
            }
            label.setText((String) event.data);
        }
    });
}
Also used : DropTargetAdapter(org.eclipse.swt.dnd.DropTargetAdapter) Transfer(org.eclipse.swt.dnd.Transfer) TextTransfer(org.eclipse.swt.dnd.TextTransfer) DropTargetEvent(org.eclipse.swt.dnd.DropTargetEvent) DropTarget(org.eclipse.swt.dnd.DropTarget)

Example 15 with DropTargetEvent

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

the class ProjectUI method setUpDragAndDrop.

// See http://www.eclipse.org/articles/Article-SWT-DND/DND-in-SWT.html
// for more documentation of SWT drag-and-drop support.
protected void setUpDragAndDrop() {
    DragSource treeAsSource = new DragSource(tree, DND.DROP_MOVE | DND.DROP_COPY);
    TaskDnDTransfer taskTransfer = TaskDnDTransfer.getInstance();
    TaskAppDnDTransfer taskAppTransfer = TaskAppDnDTransfer.getInstance();
    Transfer[] types = new Transfer[] { taskTransfer, taskAppTransfer };
    treeAsSource.setTransfer(types);
    // DropSourceEvent fields:
    // dataType:
    //   the Transfer type of the data the target prefers to receive;
    //   useful in dragSetData
    // detail:
    //   the operation the target performed; one of:
    //      DROP_MOVE - move from source to target; remove from source
    //      DROP_COPY - copy the source to target; leave the source
    //      DROP_LINK - create a link of the source at the target
    //   useful in dragFinished in case the source needs to be removed
    // doit:
    //   in dragStart, determines if the operation should proceed
    //   in dragFinished, may be set to indicate if the operation succeeded
    // image:
    //   may be set to the Image displayed during drag
    // x, y: position within the Tree
    DragSourceListener srcListener = new TreeDragSourceEffect(tree) {

        @Override
        public void dragStart(DragSourceEvent evt) {
            // If the Transfer type cannot be determined until the drag
            // starts, the setTransfer() call can be invoked here.
            // Set evt.doit to false here if action is inappropriate.
            // Reset, just in case no drag-and-drop should happen
            currentDnDSource = null;
            // Must be in first column!
            TreeColumn column = findColumn(evt.x);
            TreeItem row = tree.getItem(new Point(evt.x, evt.y));
            if ((column != null) && (column.getData() == null)) {
                if ((row != null) && (row.getData() != null)) {
                    if (((AUndertaking) row.getData()).isSpawned()) {
                        evt.doit = false;
                        return;
                    }
                }
                if (selection.getSelectedTaskCount() == 0) {
                    if (row != null) {
                        selection.setSelectedItem(row);
                        currentDnDSource = tree;
                        currentDnDColumn = 0;
                    }
                } else {
                    currentDnDSource = tree;
                    currentDnDColumn = 0;
                }
            } else {
                // Must be in cell with a valid TaskApplication!
                if ((column != null) && (column.getData() != null)) {
                    if ((row != null) && (row.getData() != null)) {
                        Design design = (Design) column.getData();
                        AUndertaking task = (AUndertaking) row.getData();
                        TaskApplication taskApp = project.getTaskApplication(task, design);
                        if (taskApp != null) {
                            if (!taskApp.getDemonstration().isEditable()) {
                                evt.doit = false;
                                return;
                            }
                            // set some highlighting of the source cell
                            selection.setSelectedCell(row, column);
                            contextSelection.setSelectedDesign(design);
                            contextSelection.addSelectedTask(task);
                            currentDnDRow = row;
                            currentDnDSource = tree;
                            currentDnDColumn = tree.indexOf(column);
                            // do not do superclass work!
                            return;
                        }
                    }
                }
                evt.doit = false;
            }
            super.dragStart(evt);
        }

        @Override
        public void dragSetData(DragSourceEvent evt) {
            // Based on the requested Transfer data type, set evt.data
            //                    if (taskTransfer.isSupportedType(evt.dataType)) {
            //                        evt.data = "This is the requested data";
            //                    }
            super.dragSetData(evt);
        }

        @Override
        public void dragFinished(DragSourceEvent evt) {
            // Operation was performed by the drop target; clean up
            // If needed, evt.detail should be the operation performed.
            super.dragFinished(evt);
            currentDnDSource = null;
            currentDnDColumn = -1;
            currentDnDRow = null;
            currentDndTaskAppDropRow = null;
        }
    };
    treeAsSource.addDragListener(srcListener);
    DropTarget treeAsTarget = new DropTarget(tree, DND.DROP_MOVE | DND.DROP_COPY);
    treeAsTarget.setTransfer(types);
    // DropTargetEvent fields:
    // currentDataType:
    //   the Transfer type of the data the target prefers to receive;
    //   can be set -- see the method comments below
    // dataTypes:
    //   the array of Transfer types the source can "send"
    // detail:
    //   the operation the user is trying to perform; one of:
    //      DROP_MOVE - move from source to target; remove from source
    //      DROP_COPY - copy the source to target; leave the source
    //      DROP_LINK - create a link of the source at the target
    //      DROP_DEFAULT - indicator that target must choose operation
    //      DROP_NONE - indicator that user is trying an unsupported op
    //   may be set to the operation the target feels is correct
    //   (thus, if initially DEFAULT, then the operation that would be
    //   performed; if initially DEFAULT and not changed, it will appear
    //   to the user as a MOVE -- also, set to NONE if target determines
    //   operation is not permitted)
    // feedback:
    //   bitwise OR'ing of feedback effects displayed to the user;
    //   can be set using the following constants:
    //      FEEDBACK_SELECT - item under cursor is selected
    //      FEEDBACK_SCROLL - allows scrolling to make items visible
    //      FEEDBACK_EXPAND - allows tree items to be expanded
    //      FEEDBACK_INSERT_BEFORE - insertion mark before item under cursor
    //      FEEDBACK_INSERT_AFTER - insertion mark after item under cursor
    //      FEEDBACK_NONE - no feedback
    // item:
    //   TreeItem or TableItem under the cursor, if applicable
    // operations:
    //   bitwise OR'ing of the operations that the DragSource can support
    treeAsTarget.addDropListener(new TreeDropTargetEffect(tree) {

        protected static final int DRAG_FEEDBACK = DND.FEEDBACK_EXPAND | DND.FEEDBACK_INSERT_BEFORE | DND.FEEDBACK_SCROLL;

        protected static final int DRAG_APP_FEEDBACK = DND.FEEDBACK_EXPAND | DND.FEEDBACK_SCROLL;

        protected int requestedOp = DND.DROP_MOVE;

        @Override
        public void dragEnter(DropTargetEvent evt) {
            // Set evt.detail to DND.DROP_NONE when the operation is a no-op
            // or if the presented type is unacceptable.  Other choices
            // that make sense: DND.DROP_MOVE, DND.DROP_COPY
            // evt.currentDataType is the type preferred by the target.
            // evt.dataTypes contains types provided by the source.
            super.dragEnter(evt);
            if (currentDnDSource != getControl()) {
                evt.detail = DND.DROP_NONE;
            } else {
                requestedOp = evt.detail;
            }
        }

        @Override
        public void dragLeave(DropTargetEvent evt) {
            if (currentDndTaskAppDropRow != null) {
                currentDndTaskAppDropRow.setBackground(currentDnDColumn, ProjectUIModel.unselectedTaskBackgroundColor);
            }
            super.dragLeave(evt);
        }

        @Override
        public void dragOperationChanged(DropTargetEvent evt) {
            // change evt.currentDataType if desired here.
            if ((evt.detail != DND.DROP_MOVE) && (evt.detail != DND.DROP_COPY)) {
                evt.detail = DND.DROP_NONE;
            }
            requestedOp = evt.detail;
            super.dragOperationChanged(evt);
        }

        @Override
        public void dragOver(DropTargetEvent evt) {
            if (currentDndTaskAppDropRow != null) {
                currentDndTaskAppDropRow.setBackground(currentDnDColumn, ProjectUIModel.unselectedTaskBackgroundColor);
            }
            Point toTreeEvtLoc = tree.toControl(evt.x, evt.y);
            //System.out.println("dragOver; set feedback here?");
            if (currentDnDSource != getControl()) {
                evt.detail = DND.DROP_NONE;
                evt.feedback = DND.FEEDBACK_NONE;
            } else if (currentDnDColumn == 0) {
                // Moving tasks
                evt.feedback = DRAG_FEEDBACK;
                evt.detail = requestedOp;
                TreeItem row = tree.getItem(toTreeEvtLoc);
                if ((row != null) && (row.getData() != null)) {
                    if (((AUndertaking) row.getData()).isSpawned()) {
                        evt.detail = DND.DROP_NONE;
                        evt.feedback = DND.FEEDBACK_NONE;
                    }
                }
            } else {
                // Moving task applications
                evt.feedback = DRAG_APP_FEEDBACK;
                TreeColumn column = findColumn(toTreeEvtLoc.x);
                if (column == null) {
                    evt.detail = DND.DROP_NONE;
                } else {
                    Design design = (Design) column.getData();
                    if (design != contextSelection.getSelectedDesign()) {
                        evt.detail = DND.DROP_NONE;
                    } else {
                        TreeItem row = tree.getItem(toTreeEvtLoc);
                        if ((row == null) || (row.getData() == null)) {
                            evt.detail = DND.DROP_NONE;
                        } else {
                            AUndertaking task = (AUndertaking) row.getData();
                            if (task.isTaskGroup() || task.isSpawned() || contextSelection.isTaskSelected(task)) {
                                evt.detail = DND.DROP_NONE;
                            } else {
                                evt.detail = requestedOp;
                                currentDndTaskAppDropRow = row;
                                currentDndTaskAppDropRow.setBackground(currentDnDColumn, CONTEXT_COLOR);
                            }
                        }
                    }
                }
            }
            super.dragOver(evt);
        }

        @Override
        public void dropAccept(DropTargetEvent evt) {
            // Can change evt.detail if desired here.
            // Provide one last chance to define the type of data that
            // will be returned in the drop event; thus, change
            // evt.currentDataType if desired here
            super.dropAccept(evt);
        }

        @Override
        public void drop(DropTargetEvent evt) {
            // When the drop operation is completed, update the
            // evt.detail field with the operation performed.
            // Do the operation!
            AUndertaking beforeTask = null;
            if (evt.item != null) {
                beforeTask = (AUndertaking) evt.item.getData();
            }
            if (requestedOp == DND.DROP_COPY) {
                if (currentDnDColumn == 0) {
                    ProjectUI.ChangeTaskPositionParms parms = new ProjectUI.ChangeTaskPositionParms(selection, beforeTask, true);
                    if (performAction(ProjectLID.DuplicateTaskFull, parms, true)) {
                        evt.detail = DND.DROP_COPY;
                    }
                } else {
                    AUndertaking fromTask = (AUndertaking) currentDnDRow.getData();
                    AUndertaking toTask = (AUndertaking) currentDndTaskAppDropRow.getData();
                    TreeColumn column = tree.getColumn(currentDnDColumn);
                    Design design = (Design) column.getData();
                    ProjectUI.MoveCopyTaskApplicationParms parms = new ProjectUI.MoveCopyTaskApplicationParms(fromTask, toTask, design);
                    selection.setSelectedCell(currentDndTaskAppDropRow, column);
                    if (performAction(ProjectLID.DuplicateTaskApplication, parms, true)) {
                        uiModel.redisplayAllResults();
                        evt.detail = DND.DROP_COPY;
                    }
                }
            } else if (requestedOp == DND.DROP_MOVE) {
                if (currentDnDColumn == 0) {
                    ProjectUI.ChangeTaskPositionParms parms = new ProjectUI.ChangeTaskPositionParms(selection, beforeTask, false);
                    if (performAction(ProjectLID.ChangeTaskPosition, parms, true)) {
                        evt.detail = DND.DROP_MOVE;
                    }
                } else {
                    AUndertaking fromTask = (AUndertaking) currentDnDRow.getData();
                    AUndertaking toTask = (AUndertaking) currentDndTaskAppDropRow.getData();
                    TreeColumn column = tree.getColumn(currentDnDColumn);
                    Design design = (Design) column.getData();
                    ProjectUI.MoveCopyTaskApplicationParms parms = new ProjectUI.MoveCopyTaskApplicationParms(fromTask, toTask, design);
                    selection.setSelectedCell(currentDndTaskAppDropRow, column);
                    if (performAction(ProjectLID.MoveTaskApplication, parms, true)) {
                        uiModel.redisplayAllResults();
                        evt.detail = DND.DROP_MOVE;
                    }
                }
            }
            super.drop(evt);
        }
    });
}
Also used : TreeItem(org.eclipse.swt.widgets.TreeItem) DropTargetEvent(org.eclipse.swt.dnd.DropTargetEvent) DragSource(org.eclipse.swt.dnd.DragSource) Point(org.eclipse.swt.graphics.Point) DragSourceListener(org.eclipse.swt.dnd.DragSourceListener) TreeDropTargetEffect(org.eclipse.swt.dnd.TreeDropTargetEffect) Point(org.eclipse.swt.graphics.Point) DragSourceEvent(org.eclipse.swt.dnd.DragSourceEvent) Design(edu.cmu.cs.hcii.cogtool.model.Design) TreeColumn(org.eclipse.swt.widgets.TreeColumn) AUndertaking(edu.cmu.cs.hcii.cogtool.model.AUndertaking) Transfer(org.eclipse.swt.dnd.Transfer) ByteArrayTransfer(org.eclipse.swt.dnd.ByteArrayTransfer) TaskApplication(edu.cmu.cs.hcii.cogtool.model.TaskApplication) DropTarget(org.eclipse.swt.dnd.DropTarget) TreeDragSourceEffect(org.eclipse.swt.dnd.TreeDragSourceEffect)

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