vaadin 拖放组件
vaadin drag n drop component
我有一个垂直布局,我需要在该垂直布局中上下移动组件。我用的是DragAndDropWrapper,但是不知道在DropHandler的drop函数下要实现什么。我找到了绝对布局的示例并且它有效。
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);
}
}
以上是抽象布局的代码。不确定如何处理垂直布局。
vaadin 7.4.5 中的拖放功能存在错误。如果您在拖放包装器下的布局内有多个文本字段,它永远不会获得鼠标焦点(意味着您将无法使用鼠标 select 它,无法在其中键入任何内容)。它已在 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);
}
}
我有一个垂直布局,我需要在该垂直布局中上下移动组件。我用的是DragAndDropWrapper,但是不知道在DropHandler的drop函数下要实现什么。我找到了绝对布局的示例并且它有效。
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);
}
}
以上是抽象布局的代码。不确定如何处理垂直布局。
vaadin 7.4.5 中的拖放功能存在错误。如果您在拖放包装器下的布局内有多个文本字段,它永远不会获得鼠标焦点(意味着您将无法使用鼠标 select 它,无法在其中键入任何内容)。它已在 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);
}
}