SWT列表拖放:在放置位置获取元素

时间:2014-12-22 19:31:13

标签: java drag-and-drop swt

您好我有一个SWT列表,我希望能够通过拖放更改列表中项目的顺序。 我有部分实现的代码。我不知道如何获得掉落发生的元素,即获取释放项目的项目(以便我可以将其与拖动的项目交换)。 这是我到目前为止的代码:

    listComposite = new Composite(parent, SWT.NONE);
    documentList = new List(listComposite, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
    Transfer[] types = new Transfer[] { TextTransfer.getInstance() };
    DragSource source = new DragSource(documentList, DND.DROP_MOVE | DND.DROP_COPY);
    source.setTransfer(types);

    source.addDragListener(new DragSourceAdapter()
    {
        @Override
        public void dragSetData(DragSourceEvent event)
        {
            // Get the selected items in the drag source
            DragSource ds = (DragSource) event.widget;
            List list = (List) ds.getControl();
            String[] selection = list.getSelection();
            event.data = selection[0];
        }
    });
    DropTarget target = new DropTarget(documentList, DND.DROP_MOVE | DND.DROP_COPY
            | DND.DROP_DEFAULT);
    target.setTransfer(types);
    target.addDropListener(new DropTargetAdapter()
    {
        @Override
        public void dragEnter(DropTargetEvent event)
        {
            if (event.detail == DND.DROP_DEFAULT)
            {
                event.detail = (event.operations & DND.DROP_COPY) != 0 ? DND.DROP_COPY
                        : DND.DROP_NONE;
            }

            // Allow dropping text only
            for (int i = 0, n = event.dataTypes.length; i < n; i++)
            {
                if (TextTransfer.getInstance().isSupportedType(event.dataTypes[i]))
                {
                    event.currentDataType = event.dataTypes[i];
                }
            }
        }

        @Override
        public void dragOver(DropTargetEvent event)
        {
            event.feedback = DND.FEEDBACK_SELECT | DND.FEEDBACK_SCROLL;
        }

        @Override
        public void drop(DropTargetEvent event)
        {
            if (TextTransfer.getInstance().isSupportedType(event.currentDataType))
            {
                  //HELP: How to get the element were the drop occurs and swap it with the draged element.
            }
        }
    });

2 个答案:

答案 0 :(得分:2)

让这件事发挥作用。可能不是最好的解决方案,但这就是我想出来的。

@Override
        public void drop(DropTargetEvent event)
        {
            String sourceItemIndex = (String) event.data;
            String targetItemIndex = null;
            if (TextTransfer.getInstance().isSupportedType(event.currentDataType))
            {
                int dropYCordinate = event.y
                        - documentList.toDisplay(documentList.getLocation()).y;
                int itemTop = 0;
                // Search for the item index where the drop took place
                for (int i = 0; i < documentList.getItemCount(); i++)
                {

                    if (dropYCordinate >= itemTop
                            && dropYCordinate <= itemTop + documentList.getItemHeight())
                    {
                        targetItemIndex = documentList.getTopIndex() + i + "";
                    }
                    itemTop += documentList.getItemHeight();
                }
            }
            swapListItems(Integer.parseInt(sourceItemIndex), Integer.parseInt(targetItemIndex));
        }
    });

基本上我得到物品掉落点的Y coodrdinate并检查与Y坐标重叠的物品。一旦我获得目标,我需要做的就是切换源和目标项。希望这有助于某些人尝试做类似的事情。

答案 1 :(得分:0)

对于表格或树

此外,如果某人遇到相同问题但使用的是表格或树:item中有DropTargetEvent个属性。

  

event.item Widget
  如果关联的控件是表或树,则此字段包含位于光标坐标处的项。

(资料来源:Eclipse Help - DropTargetEvent