/** * Returns whether 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) { return mapper.isExpanded(item); }
/** * Expands the given item if it is collapsed and has children, and returns * whether this method expanded the item. * * @param item * the item to expand * @return {@code true} if this method expanded the item, {@code false} * otherwise */ private boolean doExpand(T item) { boolean expanded = false; if (!isExpanded(item) && hasChildren(item)) { expandedItemIds.add(getDataProvider().getId(item)); expanded = true; } return expanded; }
/** * The method to recursively fetch the children of given parent. Used with * {@link Stream#flatMap} to expand a stream of parent nodes into a * flattened hierarchy. * * @param parent * the parent node * @param includeParent * {@code true} to include the parent in the stream; * {@code false} if not * @return the stream of all children under the parent */ private Stream<T> getChildrenStream(T parent, boolean includeParent) { List<T> childList = Collections.emptyList(); if (isExpanded(parent)) { childList = getDirectChildren(parent).collect(Collectors.toList()); if (childList.isEmpty()) { removeChildren(parent == null ? null : getDataProvider().getId(parent)); } else { registerChildren(parent, childList); } } return combineParentAndChildStreams(parent, childList.stream().flatMap(this::getChildrenStream), includeParent); }
/** * Collapses the given item. * * @param item * the item to collapse * @param position * the index of the item * * @return range of rows removed by collapsing the item */ public Range collapse(T item, Integer position) { Range removedRows = Range.emptyRange(); if (isExpanded(item)) { if (position != null) { removedRows = Range.withLength(position + 1, (int) getHierarchy(item, false).count()); } expandedItemIds.remove(getDataProvider().getId(item)); } return removedRows; }
@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); }