Search in sources :

Example 61 with CollectionDatasource

use of com.haulmont.cuba.gui.data.CollectionDatasource 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)

Example 62 with CollectionDatasource

use of com.haulmont.cuba.gui.data.CollectionDatasource in project cuba by cuba-platform.

the class DataGridLoader method loadComponent.

@Override
public void loadComponent() {
    assignXmlDescriptor(resultComponent, element);
    assignFrame(resultComponent);
    loadEnable(resultComponent, element);
    loadVisible(resultComponent, element);
    loadSettingsEnabled(resultComponent, element);
    loadAlign(resultComponent, element);
    loadStyleName(resultComponent, element);
    loadHeight(resultComponent, element);
    loadWidth(resultComponent, element);
    loadIcon(resultComponent, element);
    loadCaption(resultComponent, element);
    loadDescription(resultComponent, element);
    loadEditorEnabled(resultComponent, element);
    loadEditorBuffered(resultComponent, element);
    loadEditorSaveCaption(resultComponent, element);
    loadEditorCancelCaption(resultComponent, element);
    loadActions(resultComponent, element);
    loadContextMenuEnabled(resultComponent, element);
    loadColumnsHidingAllowed(resultComponent, element);
    loadColumnResizeMode(resultComponent, element);
    loadSortable(resultComponent, element);
    loadResponsive(resultComponent, element);
    loadReorderingAllowed(resultComponent, element);
    loadHeaderVisible(resultComponent, element);
    loadTextSelectionEnabled(resultComponent, element);
    Element columnsElement = element.element("columns");
    loadButtonsPanel(resultComponent);
    // must be before datasource setting
    loadRowsCount(resultComponent, element);
    String datasource = element.attributeValue("datasource");
    if (StringUtils.isBlank(datasource)) {
        throw new GuiDevelopmentException("DataGrid element doesn't have 'datasource' attribute", context.getCurrentFrameId(), "DataGrid ID", element.attributeValue("id"));
    }
    Datasource ds = context.getDsContext().get(datasource);
    if (ds == null) {
        throw new GuiDevelopmentException("Can't find datasource by name: " + datasource, context.getCurrentFrameId());
    }
    if (!(ds instanceof CollectionDatasource)) {
        throw new GuiDevelopmentException("Not a CollectionDatasource: " + datasource, context.getCurrentFrameId());
    }
    CollectionDatasource cds = (CollectionDatasource) ds;
    List<Column> availableColumns;
    if (columnsElement != null) {
        availableColumns = loadColumns(resultComponent, columnsElement, cds);
    } else {
        availableColumns = new ArrayList<>();
    }
    addDynamicAttributes(resultComponent, ds, availableColumns);
    resultComponent.setDatasource(cds);
    loadSelectionMode(resultComponent, element);
    loadFrozenColumnCount(resultComponent, element);
    loadTabIndex(resultComponent, element);
}
Also used : Datasource(com.haulmont.cuba.gui.data.Datasource) CollectionDatasource(com.haulmont.cuba.gui.data.CollectionDatasource) Column(com.haulmont.cuba.gui.components.DataGrid.Column) CollectionDatasource(com.haulmont.cuba.gui.data.CollectionDatasource) Element(org.dom4j.Element) GuiDevelopmentException(com.haulmont.cuba.gui.GuiDevelopmentException)

Example 63 with CollectionDatasource

use of com.haulmont.cuba.gui.data.CollectionDatasource in project cuba by cuba-platform.

the class FilterLoader method loadComponent.

@Override
public void loadComponent() {
    assignXmlDescriptor(resultComponent, element);
    assignFrame(resultComponent);
    loadAlign(resultComponent, element);
    loadVisible(resultComponent, element);
    loadEnable(resultComponent, element);
    loadStyleName(resultComponent, element);
    loadMargin(resultComponent, element);
    loadIcon(resultComponent, element);
    loadCaption(resultComponent, element);
    loadDescription(resultComponent, element);
    loadWidth(resultComponent, element, "100%");
    loadCollapsible(resultComponent, element, true);
    loadSettingsEnabled(resultComponent, element);
    loadBorderVisible(resultComponent, element);
    String useMaxResults = element.attributeValue("useMaxResults");
    resultComponent.setUseMaxResults(useMaxResults == null || Boolean.parseBoolean(useMaxResults));
    String textMaxResults = element.attributeValue("textMaxResults");
    resultComponent.setTextMaxResults(Boolean.parseBoolean(textMaxResults));
    final String manualApplyRequired = element.attributeValue("manualApplyRequired");
    resultComponent.setManualApplyRequired(BooleanUtils.toBooleanObject(manualApplyRequired));
    String editable = element.attributeValue("editable");
    resultComponent.setEditable(editable == null || Boolean.parseBoolean(editable));
    String columnsCount = element.attributeValue("columnsCount");
    if (!Strings.isNullOrEmpty(columnsCount)) {
        resultComponent.setColumnsCount(Integer.parseInt(columnsCount));
    }
    String folderActionsEnabled = element.attributeValue("folderActionsEnabled");
    if (folderActionsEnabled != null) {
        resultComponent.setFolderActionsEnabled(Boolean.parseBoolean(folderActionsEnabled));
    }
    String datasource = element.attributeValue("datasource");
    if (!StringUtils.isBlank(datasource)) {
        CollectionDatasource ds = (CollectionDatasource) context.getDsContext().get(datasource);
        if (ds == null) {
            throw new GuiDevelopmentException("Can't find datasource by name: " + datasource, context.getCurrentFrameId());
        }
        resultComponent.setDatasource(ds);
    }
    Frame frame = context.getFrame();
    String applyTo = element.attributeValue("applyTo");
    if (!StringUtils.isEmpty(applyTo)) {
        context.addPostInitTask((context1, window) -> {
            Component c = frame.getComponent(applyTo);
            if (c == null) {
                throw new GuiDevelopmentException("Can't apply component to component with ID: " + applyTo, context1.getFullFrameId());
            }
            resultComponent.setApplyTo(c);
        });
    }
    String modeSwitchVisible = element.attributeValue("modeSwitchVisible");
    resultComponent.setModeSwitchVisible(modeSwitchVisible == null || Boolean.parseBoolean(modeSwitchVisible));
    context.addPostInitTask((context1, window) -> {
        ((FilterImplementation) resultComponent).loadFiltersAndApplyDefault();
        String defaultMode = element.attributeValue("defaultMode");
        if (defaultMode != null && "fts".equals(defaultMode)) {
            resultComponent.switchFilterMode(FilterDelegate.FilterMode.FTS_MODE);
        }
    });
}
Also used : Frame(com.haulmont.cuba.gui.components.Frame) CollectionDatasource(com.haulmont.cuba.gui.data.CollectionDatasource) GuiDevelopmentException(com.haulmont.cuba.gui.GuiDevelopmentException) Component(com.haulmont.cuba.gui.components.Component) FilterImplementation(com.haulmont.cuba.gui.components.FilterImplementation)

Example 64 with CollectionDatasource

use of com.haulmont.cuba.gui.data.CollectionDatasource in project cuba by cuba-platform.

the class LookupFieldLoader method loadDatasource.

@Override
protected void loadDatasource(DatasourceComponent component, Element element) {
    super.loadDatasource(component, element);
    String datasource = element.attributeValue("optionsDatasource");
    if (!StringUtils.isEmpty(datasource)) {
        Datasource ds = context.getDsContext().get(datasource);
        ((LookupField) component).setOptionsDatasource((CollectionDatasource) ds);
    }
}
Also used : Datasource(com.haulmont.cuba.gui.data.Datasource) CollectionDatasource(com.haulmont.cuba.gui.data.CollectionDatasource) LookupField(com.haulmont.cuba.gui.components.LookupField)

Example 65 with CollectionDatasource

use of com.haulmont.cuba.gui.data.CollectionDatasource in project cuba by cuba-platform.

the class TwinColumnLoader method loadDatasource.

@Override
protected void loadDatasource(DatasourceComponent component, Element element) {
    String datasource = element.attributeValue("optionsDatasource");
    if (!StringUtils.isEmpty(datasource)) {
        Datasource ds = context.getDsContext().get(datasource);
        ((TwinColumn) component).setOptionsDatasource((CollectionDatasource) ds);
    }
    super.loadDatasource(component, element);
}
Also used : Datasource(com.haulmont.cuba.gui.data.Datasource) CollectionDatasource(com.haulmont.cuba.gui.data.CollectionDatasource) TwinColumn(com.haulmont.cuba.gui.components.TwinColumn)

Aggregations

CollectionDatasource (com.haulmont.cuba.gui.data.CollectionDatasource)67 Datasource (com.haulmont.cuba.gui.data.Datasource)39 UUID (java.util.UUID)24 Group (com.haulmont.cuba.security.entity.Group)23 User (com.haulmont.cuba.security.entity.User)23 ArrayList (java.util.ArrayList)23 Ignore (org.junit.Ignore)23 Test (org.junit.Test)23 Component (com.haulmont.cuba.gui.components.Component)20 List (java.util.List)19 Assert.assertEquals (org.junit.Assert.assertEquals)19 Assert.assertTrue (org.junit.Assert.assertTrue)19 Entity (com.haulmont.cuba.core.entity.Entity)15 MetaClass (com.haulmont.chile.core.model.MetaClass)10 MetaProperty (com.haulmont.chile.core.model.MetaProperty)10 LookupField (com.haulmont.cuba.gui.components.LookupField)8 GuiDevelopmentException (com.haulmont.cuba.gui.GuiDevelopmentException)7 LookupPickerField (com.haulmont.cuba.gui.components.LookupPickerField)7 Element (org.dom4j.Element)7 Assert.assertNotNull (org.junit.Assert.assertNotNull)7