Search in sources :

Example 1 with TreeData

use of com.vaadin.flow.data.provider.hierarchy.TreeData in project flow-components by vaadin.

the class TreeComponentColumnsPage method addTreeGrid.

private void addTreeGrid(boolean addGridBefore) {
    TreeGrid<String> grid = new TreeGrid<>();
    if (addGridBefore) {
        grid.setId("grid-then-comp");
        add(grid);
    }
    ComponentRenderer<TextField, String> componentRenderer = new ComponentRenderer<>(TextField::new, (component, item) -> {
        component.setReadOnly(true);
        component.setValue(item);
    });
    grid.addComponentHierarchyColumn(this::createTextField).setHeader("Header A").setId("textfield");
    grid.addColumn(componentRenderer).setHeader("Header B");
    ComponentRenderer<Button, String> componentRendererBtn = new ComponentRenderer<>(() -> new Button("btn"), ((button, s) -> {
        button.setText(s);
        button.setThemeName(ButtonVariant.LUMO_ERROR.getVariantName());
    }));
    grid.addColumn(componentRendererBtn).setHeader("Header C");
    TreeData<String> data = new TreeData<>();
    final Map<String, String> parentPathMap = new HashMap<>();
    addRootItems("Granddad", 3, data, parentPathMap).forEach(granddad -> addItems("Dad", 3, granddad, data, parentPathMap).forEach(dad -> addItems("Son", 100, dad, data, parentPathMap)));
    grid.setDataProvider(new TreeDataProvider<>(data));
    if (!addGridBefore) {
        grid.setId("comp-then-grid");
        add(grid);
    }
}
Also used : ComponentRenderer(com.vaadin.flow.data.renderer.ComponentRenderer) TreeGridHugeTreePage.addItems(com.vaadin.flow.component.treegrid.it.TreeGridHugeTreePage.addItems) ButtonVariant(com.vaadin.flow.component.button.ButtonVariant) TreeData(com.vaadin.flow.data.provider.hierarchy.TreeData) TreeGridHugeTreePage.addRootItems(com.vaadin.flow.component.treegrid.it.TreeGridHugeTreePage.addRootItems) Div(com.vaadin.flow.component.html.Div) NativeButton(com.vaadin.flow.component.html.NativeButton) HashMap(java.util.HashMap) TreeGrid(com.vaadin.flow.component.treegrid.TreeGrid) TreeDataProvider(com.vaadin.flow.data.provider.hierarchy.TreeDataProvider) Route(com.vaadin.flow.router.Route) Button(com.vaadin.flow.component.button.Button) Map(java.util.Map) Command(com.vaadin.flow.server.Command) TextField(com.vaadin.flow.component.textfield.TextField) HashMap(java.util.HashMap) ComponentRenderer(com.vaadin.flow.data.renderer.ComponentRenderer) NativeButton(com.vaadin.flow.component.html.NativeButton) Button(com.vaadin.flow.component.button.Button) TreeGrid(com.vaadin.flow.component.treegrid.TreeGrid) TextField(com.vaadin.flow.component.textfield.TextField) TreeData(com.vaadin.flow.data.provider.hierarchy.TreeData)

Example 2 with TreeData

use of com.vaadin.flow.data.provider.hierarchy.TreeData 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)

Example 3 with TreeData

use of com.vaadin.flow.data.provider.hierarchy.TreeData in project flow-components by vaadin.

the class TreeGridOrderColumnsPage 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)));
        });
    });
}
Also used : HierarchicalTestBean(com.vaadin.flow.data.bean.HierarchicalTestBean) TreeData(com.vaadin.flow.data.provider.hierarchy.TreeData)

Example 4 with TreeData

use of com.vaadin.flow.data.provider.hierarchy.TreeData in project flow-components by vaadin.

the class GridBenchmark method setParameter.

@Override
public void setParameter(BeforeEvent event, @OptionalParameter String parameter) {
    Location location = event.getLocation();
    QueryParameters queryParameters = location.getQueryParameters();
    Map<String, List<String>> parametersMap = queryParameters.getParameters();
    if (!parametersMap.containsKey("variant") || !parametersMap.containsKey("metric")) {
        add(new Text("Provide query parameters: variant and metric"));
        return;
    }
    String metric = parametersMap.get("metric").get(0);
    String variant = parametersMap.get("variant").get(0);
    LoggerFactory.getLogger(GridBenchmark.class).info("Sample: " + variant + "-" + metric);
    switch(variant) {
        case "simple":
            grid = getGrid();
            addColumns(grid, 5, false);
            break;
        case "multicolumn":
            grid = getGrid();
            addColumns(grid, 50, false);
            break;
        case "componentrenderers":
            grid = getGrid();
            addColumns(grid, 5, true);
            break;
        case "detailsopened":
            grid = getGrid();
            addColumns(grid, 5, false);
            grid.setItemDetailsRenderer(new ComponentRenderer<>(item -> new Text(item.toString())));
            items.forEach(item -> grid.setDetailsVisible(item, true));
            break;
        case "tree":
            grid = getTreeGrid();
            ((TreeGrid<String>) grid).addHierarchyColumn(i -> i);
            addColumns(grid, 5, false);
            break;
        case "mixed":
            grid = getTreeGrid();
            ((TreeGrid<String>) grid).addHierarchyColumn(i -> i);
            addColumns(grid, 50, true);
            grid.setItemDetailsRenderer(new ComponentRenderer<>(item -> new Text(item.toString())));
            treeData.getRootItems().forEach(item -> grid.setDetailsVisible(item, true));
            break;
        default:
            break;
    }
    switch(metric) {
        case "verticalscrollframetime":
            add(grid);
            whenRendered(grid).then(v -> grid.getElement().executeJs("window.measureScrollFrameTime(this, false)"));
            break;
        case "horizontalscrollframetime":
            add(grid);
            whenRendered(grid).then(v -> grid.getElement().executeJs("window.measureScrollFrameTime(this, true)"));
            break;
        case "rendertime":
            measureRendered(grid);
            UI.getCurrent().getElement().executeJs("return window.startWhenReady()").then(v -> add(grid));
            break;
        case "expandtime":
            add(grid);
            startWhenRendered(grid).then(v -> {
                measureRendered(grid);
                TreeGrid<String> treeGrid = (TreeGrid<String>) grid;
                TreeData<String> data = ((TreeDataProvider<String>) treeGrid.getDataProvider()).getTreeData();
                treeGrid.expandRecursively(data.getRootItems(), 5);
            });
            break;
        default:
            break;
    }
}
Also used : IntStream(java.util.stream.IntStream) Text(com.vaadin.flow.component.Text) ComponentRenderer(com.vaadin.flow.data.renderer.ComponentRenderer) Grid(com.vaadin.flow.component.grid.Grid) HasUrlParameter(com.vaadin.flow.router.HasUrlParameter) TreeData(com.vaadin.flow.data.provider.hierarchy.TreeData) LoggerFactory(org.slf4j.LoggerFactory) Div(com.vaadin.flow.component.html.Div) NativeButton(com.vaadin.flow.component.html.NativeButton) Collectors(java.util.stream.Collectors) BeforeEvent(com.vaadin.flow.router.BeforeEvent) TreeGrid(com.vaadin.flow.component.treegrid.TreeGrid) TreeDataProvider(com.vaadin.flow.data.provider.hierarchy.TreeDataProvider) Route(com.vaadin.flow.router.Route) OptionalParameter(com.vaadin.flow.router.OptionalParameter) PendingJavaScriptResult(com.vaadin.flow.component.page.PendingJavaScriptResult) List(java.util.List) Map(java.util.Map) Location(com.vaadin.flow.router.Location) UI(com.vaadin.flow.component.UI) Collections(java.util.Collections) JsModule(com.vaadin.flow.component.dependency.JsModule) QueryParameters(com.vaadin.flow.router.QueryParameters) Text(com.vaadin.flow.component.Text) QueryParameters(com.vaadin.flow.router.QueryParameters) TreeDataProvider(com.vaadin.flow.data.provider.hierarchy.TreeDataProvider) TreeGrid(com.vaadin.flow.component.treegrid.TreeGrid) List(java.util.List) Location(com.vaadin.flow.router.Location)

Aggregations

TreeData (com.vaadin.flow.data.provider.hierarchy.TreeData)4 Div (com.vaadin.flow.component.html.Div)2 NativeButton (com.vaadin.flow.component.html.NativeButton)2 TreeGrid (com.vaadin.flow.component.treegrid.TreeGrid)2 HierarchicalTestBean (com.vaadin.flow.data.bean.HierarchicalTestBean)2 TreeDataProvider (com.vaadin.flow.data.provider.hierarchy.TreeDataProvider)2 ComponentRenderer (com.vaadin.flow.data.renderer.ComponentRenderer)2 Route (com.vaadin.flow.router.Route)2 Map (java.util.Map)2 Text (com.vaadin.flow.component.Text)1 UI (com.vaadin.flow.component.UI)1 Button (com.vaadin.flow.component.button.Button)1 ButtonVariant (com.vaadin.flow.component.button.ButtonVariant)1 JsModule (com.vaadin.flow.component.dependency.JsModule)1 Grid (com.vaadin.flow.component.grid.Grid)1 PendingJavaScriptResult (com.vaadin.flow.component.page.PendingJavaScriptResult)1 TextField (com.vaadin.flow.component.textfield.TextField)1 TreeGridHugeTreePage.addItems (com.vaadin.flow.component.treegrid.it.TreeGridHugeTreePage.addItems)1 TreeGridHugeTreePage.addRootItems (com.vaadin.flow.component.treegrid.it.TreeGridHugeTreePage.addRootItems)1 SerializablePredicate (com.vaadin.flow.function.SerializablePredicate)1