Search in sources :

Example 1 with CubaTree

use of com.haulmont.cuba.web.widgets.CubaTree in project cuba by cuba-platform.

the class WebFilterHelper method initConditionsDragAndDrop.

@SuppressWarnings("unchecked")
@Override
public void initConditionsDragAndDrop(final Tree tree, final ConditionsTree conditions) {
    CubaTree vTree = tree.unwrapOrNull(CubaTree.class);
    if (vTree == null) {
        return;
    }
    TreeGridDragSource<AbstractCondition> treeGridDragSource = new TreeGridDragSource<>(vTree.getCompositionRoot());
    treeGridDragSource.setDragDataGenerator(TREE_DRAGGED_ITEM_ID, item -> item.getId().toString());
    TreeGridDropTarget<AbstractCondition> treeGridDropTarget = new TreeGridDropTarget<>(vTree.getCompositionRoot(), DropMode.ON_TOP_OR_BETWEEN);
    treeGridDropTarget.addTreeGridDropListener(event -> {
        if (!event.getDragSourceComponent().isPresent() || event.getDragSourceComponent().get() != vTree.getCompositionRoot()) {
            return;
        }
        String sourceId = event.getDataTransferData(TREE_DRAGGED_ITEM_ID).isPresent() ? event.getDataTransferData(TREE_DRAGGED_ITEM_ID).get() : null;
        if (sourceId == null) {
            return;
        }
        Object sourceItemId = UUID.fromString(sourceId);
        Object targetItemId = event.getDropTargetRow().isPresent() ? event.getDropTargetRow().get().getId() : null;
        if (targetItemId == null) {
            return;
        }
        // if we drop to itself
        if (targetItemId.equals(sourceItemId)) {
            return;
        }
        AbstractCondition sourceCondition = (AbstractCondition) tree.getItems().getItem(sourceItemId);
        AbstractCondition targetCondition = (AbstractCondition) tree.getItems().getItem(targetItemId);
        Node<AbstractCondition> sourceNode = conditions.getNode(sourceCondition);
        Node<AbstractCondition> targetNode = conditions.getNode(targetCondition);
        // if we drop parent to its child
        if (isAncestorOf(targetNode, sourceNode)) {
            return;
        }
        boolean moveToTheSameParent = Objects.equals(sourceNode.getParent(), targetNode.getParent());
        DropLocation location = event.getDropLocation();
        if (location == DropLocation.ON_TOP) {
            // prevent drop to not group condition
            if (!(targetCondition instanceof GroupCondition)) {
                return;
            }
            if (sourceNode.getParent() == null) {
                conditions.getRootNodes().remove(sourceNode);
            } else {
                sourceNode.getParent().getChildren().remove(sourceNode);
            }
            targetNode.addChild(sourceNode);
            refreshConditionsDs(tree, conditions);
            tree.expand(targetCondition);
        } else {
            List<Node<AbstractCondition>> siblings;
            if (targetNode.getParent() == null)
                siblings = conditions.getRootNodes();
            else
                siblings = targetNode.getParent().getChildren();
            int targetIndex = siblings.indexOf(targetNode);
            if (location == DropLocation.BELOW)
                targetIndex++;
            int sourceNodeIndex;
            if (sourceNode.getParent() == null) {
                sourceNodeIndex = conditions.getRootNodes().indexOf(sourceNode);
                conditions.getRootNodes().remove(sourceNode);
            } else {
                sourceNodeIndex = sourceNode.getParent().getChildren().indexOf(sourceNode);
                sourceNode.getParent().getChildren().remove(sourceNode);
            }
            // decrease drop position index if dragging from top to bottom inside the same parent node
            if (moveToTheSameParent && (sourceNodeIndex < targetIndex))
                targetIndex--;
            // if we drop source accurate below expanded target
            if (tree.isExpanded(targetItemId) && location == DropLocation.BELOW) {
                targetNode.insertChildAt(0, sourceNode);
            } else if (targetNode.getParent() == null) {
                sourceNode.parent = null;
                conditions.getRootNodes().add(targetIndex, sourceNode);
            } else {
                targetNode.getParent().insertChildAt(targetIndex, sourceNode);
            }
            refreshConditionsDs(tree, conditions);
        }
    });
}
Also used : TreeGridDragSource(com.vaadin.ui.components.grid.TreeGridDragSource) Node(com.haulmont.bali.datastruct.Node) CubaTree(com.haulmont.cuba.web.widgets.CubaTree) TreeGridDropTarget(com.vaadin.ui.components.grid.TreeGridDropTarget) GroupCondition(com.haulmont.cuba.gui.components.filter.condition.GroupCondition) AbstractCondition(com.haulmont.cuba.gui.components.filter.condition.AbstractCondition) DropLocation(com.vaadin.shared.ui.grid.DropLocation)

Aggregations

Node (com.haulmont.bali.datastruct.Node)1 AbstractCondition (com.haulmont.cuba.gui.components.filter.condition.AbstractCondition)1 GroupCondition (com.haulmont.cuba.gui.components.filter.condition.GroupCondition)1 CubaTree (com.haulmont.cuba.web.widgets.CubaTree)1 DropLocation (com.vaadin.shared.ui.grid.DropLocation)1 TreeGridDragSource (com.vaadin.ui.components.grid.TreeGridDragSource)1 TreeGridDropTarget (com.vaadin.ui.components.grid.TreeGridDropTarget)1