使用Vaadin 7在两棵树之间拖放

时间:2015-06-01 10:18:02

标签: vaadin vaadin7

我有两棵树:tree1,tree2,我想从tree1拖放到tree2。 问题是,当我想移动一个完整的文件夹时,他的孩子们没有在tree2中占据正确的位置。它们只是在tree2中添加但是没有得到它们真正的父(我拖动的文件夹)。实际上我没有找到如何知道tree2中文件夹的新id。 这是我的代码:

private void Drag() {

    tree1().setDragMode(TreeDragMode.NODE);             
    // Allow all nodes to have children
            for (final Object itemId : tree2().getItemIds()) {
                tree2().setChildrenAllowed(itemId, true);
            }

            tree2().setDragMode(TreeDragMode.NODE);

            tree2().setDropHandler(new TreeSortDropHandler(tree2(), tree1()));


}

public class TreeSortDropHandler implements DropHandler {
private final Tree tree2;
private final Tree tree1;

/**
 * Tree must use {@link HierarchicalContainer}.
 *
 * @param tree
 */
public TreeSortDropHandler(final Tree tree2,
        final Tree tree1) {
    this.tree2 = tree2;
    this.tree1 = tree1;
}

@Override
public AcceptCriterion getAcceptCriterion() {
    // TODO Auto-generated method stub
    return AcceptAll.get();
}


@Override
public void drop(DragAndDropEvent dropEvent) {
    // TODO Auto-generated method stub

     final Transferable t = dropEvent.getTransferable();

     // see the comment in getAcceptCriterion()
     if (t.getSourceComponent() != tree1
             || !(t instanceof DataBoundTransferable)) {
         return;
     }

     final TreeTargetDetails dropData = ((TreeTargetDetails) dropEvent
             .getTargetDetails());

     final Object sourceItemId = ((DataBoundTransferable) t).getItemId();
     // FIXME: Why "over", should be "targetItemId" or just
     // "getItemId"
     final Object targetItemId = dropData.getItemIdOver();

     // Location describes on which part of the node the drop took
     // place
     final VerticalDropLocation location = dropData.getDropLocation();

     moveNode(sourceItemId, targetItemId, location);

}


 private void moveNode(final Object sourceItemId,
         Object targetItemId, final VerticalDropLocation location) {
     final HierarchicalContainer container = (HierarchicalContainer) tree1
             .getContainerDataSource();
     if (location == VerticalDropLocation.MIDDLE) {

        if (tree1.hasChildren(sourceItemId))
        {
            Collection<?> children=tree1.getChildren(sourceItemId);

         tree2.addItem(tree1.getItemCaption(sourceItemId));
         tree2.setParent(tree1.getItemCaption(sourceItemId),targetItemId);

         targetItemId=tree2.getItem(tree1.getItemCaption(sourceItemId));
         tree2.addItems(children);
         tree2.setParent(children,tree2.getItemCaption(sourceItemId));

                  System.out.print(tree2.getItem(tree1.getItemCaption(sourceItemId)));
             System.out.print("//added");
             }else {
                 tree2.addItem(tree1.getItemCaption(sourceItemId));
                 tree2.setParent(tree1.getItemCaption(sourceItemId),targetItemId);
                 System.out.print("added");
             }

0 个答案:

没有答案