Search in sources :

Example 1 with HierarchicalTestBean

use of com.vaadin.flow.data.bean.HierarchicalTestBean in project flow-components by vaadin.

the class LazyHierarchicalDataProvider method fetchChildrenFromBackEnd.

@Override
protected Stream<HierarchicalTestBean> fetchChildrenFromBackEnd(HierarchicalQuery<HierarchicalTestBean, Void> query) {
    final int depth = query.getParentOptional().isPresent() ? query.getParent().getDepth() + 1 : 0;
    final Optional<String> parentKey = query.getParentOptional().flatMap(parent -> Optional.of(parent.getId()));
    List<HierarchicalTestBean> list = new ArrayList<>();
    int limit = Math.min(query.getLimit(), nodesPerLevel);
    for (int i = 0; i < limit; i++) {
        list.add(new HierarchicalTestBean(parentKey.orElse(null), depth, i + query.getOffset()));
    }
    query.getSortingComparator().ifPresent(sorting -> {
        list.sort(sorting);
    });
    return list.stream();
}
Also used : HierarchicalTestBean(com.vaadin.flow.data.bean.HierarchicalTestBean) ArrayList(java.util.ArrayList)

Example 2 with HierarchicalTestBean

use of com.vaadin.flow.data.bean.HierarchicalTestBean in project flow-components by vaadin.

the class TreeGridPreloadPage method setParameter.

@Override
public void setParameter(BeforeEvent event, @OptionalParameter String parameter) {
    Location location = event.getLocation();
    QueryParameters queryParameters = location.getQueryParameters();
    // query parameter: pageSize
    List<String> pageSize = queryParameters.getParameters().get("pageSize");
    if (pageSize != null) {
        grid.setPageSize(Integer.parseInt(pageSize.get(0)));
    }
    // query parameter: nodesPerLevel
    List<String> nodesPerLevel = queryParameters.getParameters().get("nodesPerLevel");
    // query parameter: depth
    List<String> depth = queryParameters.getParameters().get("depth");
    int dpNodesPerLevel = nodesPerLevel == null ? 3 : Integer.parseInt(nodesPerLevel.get(0));
    int dpDepth = depth == null ? 4 : Integer.parseInt(depth.get(0));
    setDataProvider(dpNodesPerLevel, dpDepth);
    // query parameter: expandedRootIndexes
    List<String> expandedRootIndexes = queryParameters.getParameters().get("expandedRootIndexes");
    if (expandedRootIndexes != null) {
        List<HierarchicalTestBean> expandedRootItems = Arrays.stream(expandedRootIndexes.get(0).split(",")).map(Integer::parseInt).map(expandedRootIndex -> new HierarchicalTestBean(null, 0, expandedRootIndex)).collect(java.util.stream.Collectors.toList());
        grid.expandRecursively(expandedRootItems, Integer.MAX_VALUE);
    }
    // query parameter: sortDirection
    List<String> sortDirection = queryParameters.getParameters().get("sortDirection");
    if (sortDirection != null) {
        SortDirection direction = SortDirection.valueOf(sortDirection.get(0).toUpperCase());
        GridSortOrderBuilder<HierarchicalTestBean> sorting = new GridSortOrderBuilder<HierarchicalTestBean>();
        Column<HierarchicalTestBean> column = grid.getColumns().get(0);
        if (direction == SortDirection.ASCENDING) {
            grid.sort(sorting.thenAsc(column).build());
        } else {
            grid.sort(sorting.thenDesc(column).build());
        }
    }
}
Also used : HierarchicalTestBean(com.vaadin.flow.data.bean.HierarchicalTestBean) Arrays(java.util.Arrays) SortDirection(com.vaadin.flow.data.provider.SortDirection) HasUrlParameter(com.vaadin.flow.router.HasUrlParameter) TextArea(com.vaadin.flow.component.textfield.TextArea) LitRenderer(com.vaadin.flow.data.renderer.LitRenderer) HorizontalLayout(com.vaadin.flow.component.orderedlayout.HorizontalLayout) VerticalLayout(com.vaadin.flow.component.orderedlayout.VerticalLayout) VaadinRequest(com.vaadin.flow.server.VaadinRequest) GridSortOrderBuilder(com.vaadin.flow.component.grid.GridSortOrderBuilder) BeforeEvent(com.vaadin.flow.router.BeforeEvent) HierarchicalQuery(com.vaadin.flow.data.provider.hierarchy.HierarchicalQuery) TreeGrid(com.vaadin.flow.component.treegrid.TreeGrid) Route(com.vaadin.flow.router.Route) OptionalParameter(com.vaadin.flow.router.OptionalParameter) List(java.util.List) Button(com.vaadin.flow.component.button.Button) Column(com.vaadin.flow.component.grid.Grid.Column) Stream(java.util.stream.Stream) VaadinService(com.vaadin.flow.server.VaadinService) Location(com.vaadin.flow.router.Location) HierarchicalTestBean(com.vaadin.flow.data.bean.HierarchicalTestBean) TextField(com.vaadin.flow.component.textfield.TextField) QueryParameters(com.vaadin.flow.router.QueryParameters) QueryParameters(com.vaadin.flow.router.QueryParameters) GridSortOrderBuilder(com.vaadin.flow.component.grid.GridSortOrderBuilder) SortDirection(com.vaadin.flow.data.provider.SortDirection) Location(com.vaadin.flow.router.Location)

Example 3 with HierarchicalTestBean

use of com.vaadin.flow.data.bean.HierarchicalTestBean in project flow-components by vaadin.

the class TreeGridBasicFeaturesPage method createExpandMenu.

private void createExpandMenu() {
    addAction("Expand 0 | 0", () -> grid.expand(new HierarchicalTestBean(null, 0, 0)));
    addAction("Expand 1 | 1", () -> grid.expand(new HierarchicalTestBean("/0/0", 1, 1)));
    addAction("Expand 2 | 1", () -> grid.expand(new HierarchicalTestBean("/0/0/1/1", 2, 1)));
    addAction("Expand 0 | 0 recursively", () -> grid.expandRecursively(Arrays.asList(new HierarchicalTestBean(null, 0, 0)), 1));
}
Also used : HierarchicalTestBean(com.vaadin.flow.data.bean.HierarchicalTestBean)

Example 4 with HierarchicalTestBean

use of com.vaadin.flow.data.bean.HierarchicalTestBean in project flow-components by vaadin.

the class TreeGridBasicFeaturesPage method createCollapseMenu.

private void createCollapseMenu() {
    addAction("Collapse 0 | 0", () -> grid.collapse(new HierarchicalTestBean(null, 0, 0)));
    addAction("Collapse 1 | 1", () -> grid.collapse(new HierarchicalTestBean("/0/0", 1, 1)));
    addAction("Collapse 2 | 1", () -> grid.collapse(new HierarchicalTestBean("/0/0/1/1", 2, 1)));
    addAction("Collapse 0 | 0 recursively", () -> grid.collapseRecursively(Arrays.asList(new HierarchicalTestBean(null, 0, 0)), 2));
}
Also used : HierarchicalTestBean(com.vaadin.flow.data.bean.HierarchicalTestBean)

Example 5 with HierarchicalTestBean

use of com.vaadin.flow.data.bean.HierarchicalTestBean in project flow-components by vaadin.

the class TreeGridBasicFeaturesPage method initializeDataProviders.

private void initializeDataProviders() {
    TreeData<HierarchicalTestBean> data = new TreeData<>();
    List<Integer> ints = Arrays.asList(0, 1, 2);
    ints.stream().forEach(index -> {
        HierarchicalTestBean bean = new HierarchicalTestBean(null, 0, index);
        data.addItem(null, bean);
        ints.stream().forEach(childIndex -> {
            HierarchicalTestBean childBean = new HierarchicalTestBean(bean.getId(), 1, childIndex);
            data.addItem(bean, childBean);
            ints.stream().forEach(grandChildIndex -> data.addItem(childBean, new HierarchicalTestBean(childBean.getId(), 2, grandChildIndex)));
        });
    });
    inMemoryDataProvider = new CustomTreeDataProvider(data);
    lazyDataProvider = new LazyHierarchicalDataProvider(3, 2);
    loggingDataProvider = new CustomTreeDataProvider(data) {

        @Override
        public Stream<HierarchicalTestBean> fetchChildren(HierarchicalQuery<HierarchicalTestBean, SerializablePredicate<HierarchicalTestBean>> query) {
            Optional<HierarchicalTestBean> parentOptional = query.getParentOptional();
            if (parentOptional.isPresent()) {
                log("Children request: " + parentOptional.get() + " ; " + Range.withLength(query.getOffset(), query.getLimit()));
            } else {
                log("Root node request: " + Range.withLength(query.getOffset(), query.getLimit()));
            }
            return super.fetchChildren(query);
        }
    };
}
Also used : HierarchicalTestBean(com.vaadin.flow.data.bean.HierarchicalTestBean) Optional(java.util.Optional) TreeData(com.vaadin.flow.data.provider.hierarchy.TreeData) SerializablePredicate(com.vaadin.flow.function.SerializablePredicate) Stream(java.util.stream.Stream)

Aggregations

HierarchicalTestBean (com.vaadin.flow.data.bean.HierarchicalTestBean)6 TreeData (com.vaadin.flow.data.provider.hierarchy.TreeData)2 Stream (java.util.stream.Stream)2 Button (com.vaadin.flow.component.button.Button)1 Column (com.vaadin.flow.component.grid.Grid.Column)1 GridSortOrderBuilder (com.vaadin.flow.component.grid.GridSortOrderBuilder)1 HorizontalLayout (com.vaadin.flow.component.orderedlayout.HorizontalLayout)1 VerticalLayout (com.vaadin.flow.component.orderedlayout.VerticalLayout)1 TextArea (com.vaadin.flow.component.textfield.TextArea)1 TextField (com.vaadin.flow.component.textfield.TextField)1 TreeGrid (com.vaadin.flow.component.treegrid.TreeGrid)1 SortDirection (com.vaadin.flow.data.provider.SortDirection)1 HierarchicalQuery (com.vaadin.flow.data.provider.hierarchy.HierarchicalQuery)1 LitRenderer (com.vaadin.flow.data.renderer.LitRenderer)1 SerializablePredicate (com.vaadin.flow.function.SerializablePredicate)1 BeforeEvent (com.vaadin.flow.router.BeforeEvent)1 HasUrlParameter (com.vaadin.flow.router.HasUrlParameter)1 Location (com.vaadin.flow.router.Location)1 OptionalParameter (com.vaadin.flow.router.OptionalParameter)1 QueryParameters (com.vaadin.flow.router.QueryParameters)1