/** * Returns whether given item has children. * * @param item * the node to test * @return {@code true} if node has children; {@code false} if not */ public boolean hasChildren(T item) { return getDataProvider().hasChildren(item); }
/** * Fetches data from this HierarchicalDataProvider using given * {@code query}. Only the immediate children of * {@link HierarchicalQuery#getParent()} will be returned. * * @param query * given query to request data with * @return a stream of data objects resulting from the query * * @throws IllegalArgumentException * if the query is not of type HierarchicalQuery */ @Override public default Stream<T> fetch(Query<T, F> query) { if (query instanceof HierarchicalQuery<?, ?>) { return fetchChildren((HierarchicalQuery<T, F>) query); } throw new IllegalArgumentException( "Hierarchical data provider doesn't support non-hierarchical queries"); }
/** * Get the number of immediate child data items for the parent item returned * by a given query. * * @param query * given query to request the count for * @return the count of child data items for the data item * {@link HierarchicalQuery#getParent()} * * @throws IllegalArgumentException * if the query is not of type HierarchicalQuery */ @Override public default int size(Query<T, F> query) { if (query instanceof HierarchicalQuery<?, ?>) { return getChildCount((HierarchicalQuery<T, F>) query); } throw new IllegalArgumentException( "Hierarchical data provider doesn't support non-hierarchical queries"); }
getDataProvider().fetchChildren( new HierarchicalQuery<>(null, item)), depth - 1); getDataProvider().refreshAll();
@Override public Range collapse(T item, Integer position) { int removedChildren; if (itemInteractionAvailability.test(item)) { final Map<T, Set<T>> relationMap = accessChildMap(); final Set<T> children = relationMap.get(item); removedChildren = (int) children.stream().filter(child -> !itemInteractionAvailability.test(child)).count(); } else { getDataProvider().refreshAll(); return Range.emptyRange(); } final Range toCollapse = super.collapse(item, position); return Range.between(toCollapse.getStart(), toCollapse.getEnd() + removedChildren); }
/** * Find parent for the given item among open folders. * * @param item * the item * @return parent item or {@code null} for root items or if the parent is * closed */ protected T getParentOfItem(T item) { Objects.requireNonNull(item, "Can not find the parent of null"); return parentIdMap.get(getDataProvider().getId(item)); }
@Override protected void writeData(Element body, DesignContext designContext) { getDataProvider().fetch(new HierarchicalQuery<>(null, null)) .forEach(item -> writeRow(body, item, null, designContext)); }
if (communicator.hasChildren(item)) { collapseRecursively( getDataProvider().fetchChildren( new HierarchicalQuery<>(null, item)), depth - 1); getDataProvider().refreshAll();
@Override public void clear() { this.getTreeData().clear(); this.getDataProvider().refreshAll(); }
/** * Returns whether the given item is expanded. * * @param item * the item to test * @return {@code true} if item is expanded; {@code false} if not */ public boolean isExpanded(T item) { if (item == null) { // Root nodes are always visible. return true; } return expandedItemIds.contains(getDataProvider().getId(item)); }
private void writeItems(Element design, DesignContext designContext) { getDataProvider().fetch(new HierarchicalQuery<>(null, null)) .forEach(item -> writeItem(design, designContext, item, null)); }
@Override public void generateData(T item, JsonObject jsonObject) { JsonObject hierarchyData = Json.createObject(); int depth = getDepth(item); if (depth >= 0) { hierarchyData.put(HierarchicalDataCommunicatorConstants.ROW_DEPTH, depth); } boolean isLeaf = !getDataProvider().hasChildren(item); if (isLeaf) { hierarchyData.put(HierarchicalDataCommunicatorConstants.ROW_LEAF, true); } else { hierarchyData.put( HierarchicalDataCommunicatorConstants.ROW_COLLAPSED, !isExpanded(item)); hierarchyData.put(HierarchicalDataCommunicatorConstants.ROW_LEAF, false); hierarchyData.put( HierarchicalDataCommunicatorConstants.ROW_COLLAPSE_ALLOWED, getItemCollapseAllowedProvider().test(item)); } // add hierarchy information to row as metadata jsonObject.put( HierarchicalDataCommunicatorConstants.ROW_HIERARCHY_DESCRIPTION, hierarchyData); }
this.treeGrid.getEditor().addSaveListener(e -> { this.treePresenter.saveInlineEditingResults(e.getBean()); this.treeGrid.getDataProvider().refreshAll(); }); this.treePresenter.createColumns(treeGrid);
/** * Fetch all the descendants of the given parent item from the given data * provider. * * @since 8.1 * @param parent * the parent item to fetch descendants for * @param dataProvider * the data provider to fetch from * @return the stream of all descendant items */ private Stream<T> fetchAllDescendants(T parent, HierarchicalDataProvider<T, ?> dataProvider) { List<T> children = dataProvider .fetchChildren(new HierarchicalQuery<>(null, parent)) .collect(Collectors.toList()); if (children.isEmpty()) { return Stream.empty(); } return children.stream() .flatMap(child -> Stream.concat(Stream.of(child), fetchAllDescendants(child, dataProvider))); }
/** * Gets the stream of direct children for given node. * * @param parent * the parent node * @return the stream of direct children */ private Stream<T> getDirectChildren(T parent) { return doFetchDirectChildren(parent, Range.between(0, getDataProvider() .getChildCount(new HierarchicalQuery<>(filter, parent)))); }
/** * Register parent and children items into inner structures. May be * overridden in subclasses. * * @param parent * the parent item * @param childList * list of parents children to be registered. */ protected void registerChildren(T parent, List<T> childList) { childMap.put(parent, new HashSet<>(childList)); childList.forEach( x -> parentIdMap.put(getDataProvider().getId(x), parent)); }
private void writeRow(Element container, T item, T parent, DesignContext context) { Element tableRow = container.appendElement("tr"); tableRow.attr("item", serializeDeclarativeRepresentation(item)); if (parent != null) { tableRow.attr("parent", serializeDeclarativeRepresentation(parent)); } if (getSelectionModel().isSelected(item)) { tableRow.attr("selected", true); } for (Column<T, ?> column : getColumns()) { Object value = column.getValueProvider().apply(item); tableRow.appendElement("td") .append(Optional.ofNullable(value).map(Object::toString) .map(DesignFormatter::encodeForTextNode) .orElse("")); } getDataProvider().fetch(new HierarchicalQuery<>(null, item)).forEach( childItem -> writeRow(container, childItem, item, context)); }
@Override public boolean hasChildren(T item) { return dataProvider().hasChildren(item); }
/** * Generic method for finding direct children of a given parent, limited by * given range. * * @param parent * the parent * @param range * the range of direct children to return * @return the requested children of the given parent */ @SuppressWarnings({ "rawtypes", "unchecked" }) private Stream<T> doFetchDirectChildren(T parent, Range range) { return getDataProvider().fetchChildren(new HierarchicalQuery( range.getStart(), range.length(), getBackEndSorting(), getInMemorySorting(), getFilter(), parent)); }
@Override public int getChildCount(HierarchicalQuery<T, F> query) { return dataProvider().getChildCount(new HierarchicalQuery<>( query.getOffset(), query.getLimit(), query.getSortOrders(), query.getInMemorySorting(), getFilter(query), query.getParent())); }