-1

I have a vertical layout and I need to move the components in that vertical layout, up and down. I used DragAndDropWrapper, but I am not sure what to implement under DropHandler's drop function. I found examples for absoulte layout and it worked.

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);
        }
    }

Above is the code for the abstract layout. Not sure what to do for vertical layout.

pbaris
  • 4,525
  • 5
  • 37
  • 61
user1631306
  • 4,350
  • 8
  • 39
  • 74

1 Answers1

0

there is bug in drag n drop functionality in vaadin 7.4.5. If you have more than one textfields inside the layout which is under drag n drop wrapper, it would never get mouse focus on it ( means you wont be able to select it using mouse, to type anything in it). Its fixed in 7.7.5. So, I used the following method

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);

    }
}
user1631306
  • 4,350
  • 8
  • 39
  • 74