Search in sources :

Example 1 with TreeDropTargetEffect

use of org.eclipse.swt.dnd.TreeDropTargetEffect in project tdi-studio-se by Talend.

the class SchemasTreeDnD method addDragAndDrop.

public void addDragAndDrop() {
    // drag
    DragSource source = new DragSource(getTree(), operations);
    source.setTransfer(transferTypes);
    source.addDragListener(new TreeDragSourceEffect(getTree()));
    // drop
    DropTarget target = new DropTarget(getTree(), operations);
    target.setTransfer(transferTypes);
    target.addDropListener(new TreeDropTargetEffect(getTree()) {

        private SchemasKeyData source;

        @Override
        public void dragEnter(DropTargetEvent event) {
            super.dragEnter(event);
            Widget item = event.item;
            if (item != null) {
                TreeItem treeItem = (TreeItem) event.item;
                Object data = treeItem.getData();
                if (data != null && data instanceof SchemasKeyData) {
                    this.source = (SchemasKeyData) data;
                    event.feedback = DND.FEEDBACK_EXPAND;
                    return;
                }
            }
            event.detail = DND.DROP_NONE;
        }

        @Override
        public void drop(DropTargetEvent event) {
            super.drop(event);
            if (event.item != null && source != null) {
                TreeItem item = (TreeItem) event.item;
                Object data = item.getData();
                if (data != null && data instanceof SchemasKeyData) {
                    SchemasKeyData target = (SchemasKeyData) data;
                    target.addChild(source);
                    schemaTreeViewer.refresh();
                }
            }
            source = null;
        }
    });
}
Also used : TreeItem(org.eclipse.swt.widgets.TreeItem) Widget(org.eclipse.swt.widgets.Widget) DropTargetEvent(org.eclipse.swt.dnd.DropTargetEvent) DragSource(org.eclipse.swt.dnd.DragSource) DropTarget(org.eclipse.swt.dnd.DropTarget) TreeDragSourceEffect(org.eclipse.swt.dnd.TreeDragSourceEffect) TreeDropTargetEffect(org.eclipse.swt.dnd.TreeDropTargetEffect) SchemasKeyData(org.talend.designer.filemultischemas.data.SchemasKeyData)

Example 2 with TreeDropTargetEffect

use of org.eclipse.swt.dnd.TreeDropTargetEffect 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

DragSource (org.eclipse.swt.dnd.DragSource)2 DropTarget (org.eclipse.swt.dnd.DropTarget)2 DropTargetEvent (org.eclipse.swt.dnd.DropTargetEvent)2 TreeDragSourceEffect (org.eclipse.swt.dnd.TreeDragSourceEffect)2 TreeDropTargetEffect (org.eclipse.swt.dnd.TreeDropTargetEffect)2 TreeItem (org.eclipse.swt.widgets.TreeItem)2 AUndertaking (edu.cmu.cs.hcii.cogtool.model.AUndertaking)1 Design (edu.cmu.cs.hcii.cogtool.model.Design)1 TaskApplication (edu.cmu.cs.hcii.cogtool.model.TaskApplication)1 ByteArrayTransfer (org.eclipse.swt.dnd.ByteArrayTransfer)1 DragSourceEvent (org.eclipse.swt.dnd.DragSourceEvent)1 DragSourceListener (org.eclipse.swt.dnd.DragSourceListener)1 Transfer (org.eclipse.swt.dnd.Transfer)1 Point (org.eclipse.swt.graphics.Point)1 TreeColumn (org.eclipse.swt.widgets.TreeColumn)1 Widget (org.eclipse.swt.widgets.Widget)1 SchemasKeyData (org.talend.designer.filemultischemas.data.SchemasKeyData)1