Search in sources :

Example 1 with GroupCondition

use of com.haulmont.cuba.gui.components.filter.condition.GroupCondition in project cuba by cuba-platform.

the class AppliedFilter method recursivelyCreateConditionCaption.

protected void recursivelyCreateConditionCaption(Node<AbstractCondition> node, StringBuilder sb) {
    AbstractCondition condition = node.getData();
    if (condition.getHidden())
        return;
    if (condition.isGroup()) {
        GroupType groupType = ((GroupCondition) condition).getGroupType();
        sb.append(groupType.getLocCaption()).append("(");
        List<Node<AbstractCondition>> visibleChildNodes = new ArrayList<>();
        for (Node<AbstractCondition> childNode : node.getChildren()) {
            AbstractCondition childCondition = childNode.getData();
            if (!childCondition.getHidden() && (childCondition.isGroup() || childCondition.getParam() != null && childCondition.getParam().getValue() != null))
                visibleChildNodes.add(childNode);
        }
        Iterator<Node<AbstractCondition>> iterator = visibleChildNodes.iterator();
        while (iterator.hasNext()) {
            Node<AbstractCondition> childNode = iterator.next();
            recursivelyCreateConditionCaption(childNode, sb);
            if (iterator.hasNext())
                sb.append(", ");
        }
        sb.append(")");
    } else {
        Param param = condition.getParam();
        sb.append(condition.getLocCaption()).append(" ");
        if (condition.getOperator() == Op.NOT_EMPTY) {
            if (BooleanUtils.isTrue((Boolean) param.getValue())) {
                sb.append(messages.getMainMessage("Op.NOT_EMPTY"));
            } else {
                sb.append(messages.getMainMessage("Op.EMPTY"));
            }
        } else {
            sb.append(condition.getOperationCaption()).append(" ").append(formatParamValue(param));
        }
    }
}
Also used : Node(com.haulmont.bali.datastruct.Node) ArrayList(java.util.ArrayList) GroupCondition(com.haulmont.cuba.gui.components.filter.condition.GroupCondition) AbstractCondition(com.haulmont.cuba.gui.components.filter.condition.AbstractCondition)

Example 2 with GroupCondition

use of com.haulmont.cuba.gui.components.filter.condition.GroupCondition 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)2 AbstractCondition (com.haulmont.cuba.gui.components.filter.condition.AbstractCondition)2 GroupCondition (com.haulmont.cuba.gui.components.filter.condition.GroupCondition)2 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 ArrayList (java.util.ArrayList)1