Search in sources :

Example 16 with Node

use of com.haulmont.bali.datastruct.Node in project cuba by cuba-platform.

the class RelatedEntitiesBean method getRelatedEntitiesFilterXml.

protected String getRelatedEntitiesFilterXml(MetaClass relatedMetaCLass, Collection<? extends Entity> selectedEntities, Filter component, MetaDataDescriptor descriptor) {
    ConditionsTree tree = new ConditionsTree();
    String filterComponentPath = ComponentsHelper.getFilterComponentPath(component);
    String[] strings = ValuePathHelper.parse(filterComponentPath);
    String filterComponentName = ValuePathHelper.format(Arrays.copyOfRange(strings, 1, strings.length));
    String relatedPrimaryKey = metadataTools.getPrimaryKeyName(relatedMetaCLass);
    AbstractCondition condition = getOptimizedCondition(getParentIds(selectedEntities), component.getDatasource(), filterComponentName, relatedPrimaryKey, descriptor);
    if (condition == null) {
        condition = getNonOptimizedCondition(relatedMetaCLass, getRelatedIds(selectedEntities, descriptor), component, filterComponentName, relatedPrimaryKey);
    }
    tree.setRootNodes(Collections.singletonList(new Node<>(condition)));
    return filterParser.getXml(tree, Param.ValueProperty.VALUE);
}
Also used : Node(com.haulmont.bali.datastruct.Node) AbstractCondition(com.haulmont.cuba.gui.components.filter.condition.AbstractCondition) ConditionsTree(com.haulmont.cuba.gui.components.filter.ConditionsTree)

Example 17 with Node

use of com.haulmont.bali.datastruct.Node in project cuba by cuba-platform.

the class WebFilterHelper method initConditionsDragAndDrop.

@Override
public void initConditionsDragAndDrop(final Tree tree, final ConditionsTree conditions) {
    final com.vaadin.ui.Tree vTree = tree.unwrap(com.vaadin.ui.Tree.class);
    vTree.setDragMode(com.vaadin.ui.Tree.TreeDragMode.NODE);
    vTree.setDropHandler(new DropHandler() {

        @Override
        public void drop(DragAndDropEvent event) {
            Transferable t = event.getTransferable();
            if (t.getSourceComponent() != vTree)
                return;
            com.vaadin.ui.Tree.TreeTargetDetails target = (com.vaadin.ui.Tree.TreeTargetDetails) event.getTargetDetails();
            VerticalDropLocation location = target.getDropLocation();
            Object sourceItemId = t.getData("itemId");
            Object targetItemId = target.getItemIdOver();
            if (targetItemId == null)
                return;
            CollectionDatasource datasource = tree.getDatasource();
            AbstractCondition sourceCondition = (AbstractCondition) datasource.getItem(sourceItemId);
            AbstractCondition targetCondition = (AbstractCondition) datasource.getItem(targetItemId);
            Node<AbstractCondition> sourceNode = conditions.getNode(sourceCondition);
            Node<AbstractCondition> targetNode = conditions.getNode(targetCondition);
            if (isAncestorOf(targetNode, sourceNode))
                return;
            boolean moveToTheSameParent = Objects.equals(sourceNode.getParent(), targetNode.getParent());
            if (location == VerticalDropLocation.MIDDLE) {
                if (sourceNode.getParent() == null) {
                    conditions.getRootNodes().remove(sourceNode);
                } else {
                    sourceNode.getParent().getChildren().remove(sourceNode);
                }
                targetNode.addChild(sourceNode);
                refreshConditionsDs();
                tree.expand(targetCondition.getId());
            } else {
                List<Node<AbstractCondition>> siblings;
                if (targetNode.getParent() == null)
                    siblings = conditions.getRootNodes();
                else
                    siblings = targetNode.getParent().getChildren();
                int targetIndex = siblings.indexOf(targetNode);
                if (location == VerticalDropLocation.BOTTOM)
                    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 (targetNode.getParent() == null) {
                    sourceNode.parent = null;
                    conditions.getRootNodes().add(targetIndex, sourceNode);
                } else {
                    targetNode.getParent().insertChildAt(targetIndex, sourceNode);
                }
                refreshConditionsDs();
            }
        }

        protected boolean isAncestorOf(Node childNode, Node possibleParentNode) {
            while (childNode.getParent() != null) {
                if (childNode.getParent().equals(possibleParentNode))
                    return true;
                childNode = childNode.getParent();
            }
            return false;
        }

        protected void refreshConditionsDs() {
            tree.getDatasource().refresh(Collections.singletonMap("conditions", conditions));
        }

        @Override
        public AcceptCriterion getAcceptCriterion() {
            return new Or(new AbstractSelect.TargetItemIs(vTree, getGroupConditionIds().toArray()), new Not(AbstractSelect.VerticalLocationIs.MIDDLE));
        }

        protected List<UUID> getGroupConditionIds() {
            List<UUID> groupConditions = new ArrayList<>();
            List<AbstractCondition> list = conditions.toConditionsList();
            for (AbstractCondition condition : list) {
                if (condition instanceof GroupCondition)
                    groupConditions.add(condition.getId());
            }
            return groupConditions;
        }
    });
}
Also used : Or(com.vaadin.event.dd.acceptcriteria.Or) VerticalDropLocation(com.vaadin.shared.ui.dd.VerticalDropLocation) DragAndDropEvent(com.vaadin.event.dd.DragAndDropEvent) CollectionDatasource(com.haulmont.cuba.gui.data.CollectionDatasource) Node(com.haulmont.bali.datastruct.Node) Transferable(com.vaadin.event.Transferable) Not(com.vaadin.event.dd.acceptcriteria.Not) ConditionsTree(com.haulmont.cuba.gui.components.filter.ConditionsTree) GroupCondition(com.haulmont.cuba.gui.components.filter.condition.GroupCondition) DropHandler(com.vaadin.event.dd.DropHandler) AbstractCondition(com.haulmont.cuba.gui.components.filter.condition.AbstractCondition) AcceptCriterion(com.vaadin.event.dd.acceptcriteria.AcceptCriterion)

Aggregations

Node (com.haulmont.bali.datastruct.Node)17 ConditionsTree (com.haulmont.cuba.gui.components.filter.ConditionsTree)5 AbstractCondition (com.haulmont.cuba.gui.components.filter.condition.AbstractCondition)5 Tree (com.haulmont.bali.datastruct.Tree)4 MetaClass (com.haulmont.chile.core.model.MetaClass)3 ArrayList (java.util.ArrayList)3 MetaProperty (com.haulmont.chile.core.model.MetaProperty)2 MetaPropertyPath (com.haulmont.chile.core.model.MetaPropertyPath)2 EntityPropertyDiff (com.haulmont.cuba.core.entity.diff.EntityPropertyDiff)2 BasicPermissionTarget (com.haulmont.cuba.gui.app.security.entity.BasicPermissionTarget)2 GroupCondition (com.haulmont.cuba.gui.components.filter.condition.GroupCondition)2 GroupConditionDescriptor (com.haulmont.cuba.gui.components.filter.descriptor.GroupConditionDescriptor)2 CollectionDatasource (com.haulmont.cuba.gui.data.CollectionDatasource)2 UIPerformanceLogger (com.haulmont.cuba.gui.logging.UIPerformanceLogger)2 Collectors (java.util.stream.Collectors)2 Element (org.dom4j.Element)2 StopWatch (org.perf4j.StopWatch)2 Slf4JStopWatch (org.perf4j.slf4j.Slf4JStopWatch)2 ClientConfig (com.haulmont.cuba.client.ClientConfig)1 Entity (com.haulmont.cuba.core.entity.Entity)1