vaadin drag n drop component

时间:2016-12-03 17:31:09

标签: java drag-and-drop vaadin vaadin7

我有一个垂直布局,我需要在垂直布局中上下移动组件。我使用了DragAndDropWrapper,但我不确定在DropHandler的drop函数下实现什么。我找到了absoulte布局的例子并且有效。

class MoveHandler implements DropHandler
    {
        public AcceptCriterion getAcceptCriterion()
        {
            return AcceptAll.get();
        }

        public void drop(DragAndDropEvent event)
        {
            WrapperTransferable t = (WrapperTransferable) event.getTransferable();
            WrapperTargetDetails details = (WrapperTargetDetails) event.getTargetDetails();

            // Calculate the drag coordinate difference
            int xChange = details.getMouseEvent().getClientX() - t.getMouseDownEvent().getClientX();
            int yChange = details.getMouseEvent().getClientY() - t.getMouseDownEvent().getClientY();

            // Move the component in the absolute layout
             ComponentPosition pos = ((AbsoluteLayout)
             questionButtonLayout).getPosition(t.getSourceComponent());
             pos.setLeftValue(pos.getLeftValue() + xChange);
             pos.setTopValue(pos.getTopValue() + yChange);
        }
    }

以上是抽象布局的代码。不知道如何处理垂直布局。

1 个答案:

答案 0 :(得分:0)

vaadin 7.4.5中的拖放功能存在漏洞。如果布局中有多个文本字段位于拖放包装下,则永远不会将鼠标聚焦在它上面(意味着您无法使用鼠标选择它,在其中键入任何内容)。其固定在7.7.5。所以,我使用了以下方法

class MoveHandler implements DropHandler
{
    private static final long       serialVersionUID    = -5709370299130660699L;
    private AbstractOrderedLayout   layout;
    private Level                   level;

    public MoveHandler(AbstractOrderedLayout layout, Level level)
    {
        this.layout = layout;
        this.level = level;
    }

    public AcceptCriterion getAcceptCriterion()
    {
        return AcceptAll.get();
    }

    @SuppressWarnings("deprecation")
    public void drop(DragAndDropEvent dropEvent)
    {
        Transferable transferable = dropEvent.getTransferable();
        Component sourceComponent = transferable.getSourceComponent();
        TargetDetails dropTargetData = dropEvent.getTargetDetails();
        DropTarget target = dropTargetData.getTarget();

        // First question cann't be dragged.
        if (layout.getComponent(0).equals(sourceComponent))
            return;

        // find the location of the dragged component
        Iterator<Component> componentIterator = layout.getComponentIterator();
        Component next = null;
        int indexSourceComponent = 0;
        while (componentIterator.hasNext())
        {
            next = componentIterator.next();
            if (next == sourceComponent)
                break;
            indexSourceComponent++;
        }

        // find the location where to move the dragged component
        boolean sourceWasAfterTarget = true;
        int index = 0;
        componentIterator = layout.getComponentIterator();
        next = null;

        while (next != target && componentIterator.hasNext())
        {
            next = componentIterator.next();

            if (next != sourceComponent)
            {
                index++;
            }
            else
            {
                sourceWasAfterTarget = false;
            }
        }
        if (next == null || next != target)
        {
            // component not found - if dragging from another layout
            return;
        }

        // drop on top of target?
        if (dropTargetData.getData("verticalLocation").equals(VerticalDropLocation.MIDDLE.toString()))
        {
            if (sourceWasAfterTarget)
            {
                index--;
            }
        }

        // drop before the target?
        else if (dropTargetData.getData("verticalLocation").equals(VerticalDropLocation.TOP.toString()))
        {
            index--;
            if (index < 0)
            {
                index = 0;
            }
        }

        // Nothing can be dragged to 1st position. 1st positions is secured for default question.
        if (index == 0)
            return;

        // move component within the layout
        layout.removeComponent(sourceComponent);
        layout.addComponent(sourceComponent, index);

    }
}