/** * Creates a new {@code TreeGrid} without support for creating columns based * on property names. Use an alternative constructor, such as * {@link TreeGrid#TreeGrid(Class)}, to create a {@code TreeGrid} that * automatically sets up columns based on the type of presented data. */ public TreeGrid() { this(new HierarchicalDataCommunicator<>()); }
/** * Collapses the given item and removes its sub-hierarchy. Calling this * method will have no effect if the row is already collapsed. * * @param item * the item to collapse */ public void collapse(T item) { collapse(item, true); }
/** * Collapses the given item and removes its sub-hierarchy. Calling this * method will have no effect if the row is already collapsed. * * @param item * the item to collapse * @param index * the index of the item */ public void collapse(T item, Integer index) { doCollapse(item, index, true); }
/** * Expands the given items. * <p> * If an item is currently expanded, does nothing. If an item does not have * any children, does nothing. * * @param items * the items to expand */ public void expand(Collection<T> items) { HierarchicalDataCommunicator<T> communicator = getDataCommunicator(); items.forEach(item -> { if (!communicator.isExpanded(item) && communicator.hasChildren(item)) { communicator.expand(item); fireExpandEvent(item, false); } }); }
removeDataGenerator(mapper); mapper = createHierarchyMapper(dataProvider); mapper.setBackEndSorting(getBackEndSorting()); mapper.setInMemorySorting(getInMemorySorting()); mapper.setFilter(getFilter()); mapper.setItemCollapseAllowedProvider(getItemCollapseAllowedProvider()); addDataGenerator(mapper);
private void registerTreeGridRpc() { registerRpc((NodeCollapseRpc) (rowKey, rowIndex, collapse, userOriginated) -> { T item = getDataCommunicator().getKeyMapper().get(rowKey); if (collapse && getDataCommunicator().isExpanded(item)) { getDataCommunicator().collapse(item, rowIndex); fireCollapseEvent( getDataCommunicator().getKeyMapper().get(rowKey), userOriginated); } else if (!collapse && !getDataCommunicator().isExpanded(item)) { getDataCommunicator().expand(item, rowIndex); fireExpandEvent( getDataCommunicator().getKeyMapper().get(rowKey), userOriginated); } }); registerRpc((FocusParentRpc) (rowKey, cellIndex) -> { Integer parentIndex = getDataCommunicator().getParentIndex( getDataCommunicator().getKeyMapper().get(rowKey)); if (parentIndex != null) { getRpcProxy(FocusRpc.class).focusCell(parentIndex, cellIndex); } }); }
/** * Expands the given item. Calling this method will have no effect if the * item is already expanded or if it has no children. The index is provided * by the client-side or calculated from a full data request. * {@code syncAndRefresh} indicates whether the changes should be * synchronised to the client and the data provider be notified. * * @param item * the item to expand * @param index * the index of the item * @param syncAndRefresh * {@code true} if the changes should be synchronised to the * client and the data provider should be notified of the * changes, {@code false} otherwise. */ private void doExpand(T item, Integer index, boolean syncAndRefresh) { Range addedRows = mapper.expand(item, index); if (syncAndRefresh) { if (!reset && !addedRows.isEmpty()) { getClientRpc().insertRows(addedRows.getStart(), addedRows.length()); Stream<T> children = mapper.fetchItems(item, Range.withLength(0, addedRows.length())); pushData(addedRows.getStart(), children.collect(Collectors.toList())); } refresh(item); } }
/** * Sets the item icon generator that is used to produce custom icons for * items. The generator can return <code>null</code> for items with no icon. * * @see IconGenerator * * @param iconGenerator * the item icon generator to set, not <code>null</code> * @throws NullPointerException * if {@code itemIconGenerator} is {@code null} */ public void setItemIconGenerator(IconGenerator<T> iconGenerator) { Objects.requireNonNull(iconGenerator, "Item icon generator must not be null"); this.iconProvider = iconGenerator; treeGrid.getDataCommunicator().reset(); }
/** * Collapses the given item and removes its sub-hierarchy. Calling this * method will have no effect if the row is already collapsed. The index is * provided by the client-side or calculated from a full data request. * {@code syncAndRefresh} indicates whether the changes should be * synchronised to the client and the data provider be notified. * * @param item * the item to collapse * @param index * the index of the item * @param syncAndRefresh * {@code true} if the changes should be synchronised to the * client and the data provider should be notified of the * changes, {@code false} otherwise. */ private void doCollapse(T item, Integer index, boolean syncAndRefresh) { Range removedRows = mapper.collapse(item, index); if (syncAndRefresh) { if (!reset && !removedRows.isEmpty()) { getClientRpc().removeRows(removedRows.getStart(), removedRows.length()); } refresh(item); } }
/** * Collapse the given items. * <p> * For items that are already collapsed, does nothing. * * @param items * the collection of items to collapse */ public void collapse(Collection<T> items) { HierarchicalDataCommunicator<T> communicator = getDataCommunicator(); items.forEach(item -> { if (communicator.isExpanded(item)) { communicator.collapse(item); fireCollapseEvent(item, false); } }); }
if (communicator.hasChildren(item)) { communicator.expand(item, false);
if (communicator.hasChildren(item)) { collapseRecursively( getDataProvider().fetchChildren( depth - 1); communicator.collapse(item, false);
@Override public void refresh(T data) { if (itemInteractionAvailability.test(data)) { super.refresh(data); } } }
/** * Expands the given item. Calling this method will have no effect if the * item is already expanded or if it has no children. * * @param item * the item to expand */ public void expand(T item) { expand(item, true); }
/** * Expands the given item at the given index. Calling this method will have * no effect if the item is already expanded. * * @param item * the item to expand * @param index * the index of the item */ public void expand(T item, Integer index) { doExpand(item, index, true); }
/** * Sets the item collapse allowed provider for this * HierarchicalDataCommunicator. The provider should return {@code true} for * any item that the user can collapse. * <p> * <strong>Note:</strong> This callback will be accessed often when sending * data to the client. The callback should not do any costly operations. * * @param provider * the item collapse allowed provider, not {@code null} */ public void setItemCollapseAllowedProvider( ItemCollapseAllowedProvider<T> provider) { Objects.requireNonNull(provider, "Provider can't be null"); itemCollapseAllowedProvider = provider; // Update hierarchy mapper mapper.setItemCollapseAllowedProvider(provider); getActiveDataHandler().getActiveData().values().forEach(this::refresh); }
setCompositionRoot(treeGrid); renderer = new TreeRenderer(); treeGrid.getDataCommunicator().addDataGenerator(renderer); treeGrid.addColumn(i -> captionGenerator.apply(i), renderer) .setId("column");
/** * Sets the item caption generator that is used to produce the strings shown * as the text for each item. By default, {@link String#valueOf(Object)} is * used. * * @param captionGenerator * the item caption provider to use, not <code>null</code> */ public void setItemCaptionGenerator( ItemCaptionGenerator<T> captionGenerator) { Objects.requireNonNull(captionGenerator, "Caption generator must not be null"); this.captionGenerator = captionGenerator; treeGrid.getDataCommunicator().reset(); }
/** * Expands the given item at given index. Calling this method will have no * effect if the row is already expanded. The index is provided by the * client-side or calculated from a full data request. * * @param item * the item to expand * @param index * the index of the item * @see #expand(Object) * @deprecated use {@link #expand(Object, Integer)} instead */ @Deprecated public void doExpand(T item, Optional<Integer> index) { expand(item, index.orElse(null)); }
/** * Expands the given item. Calling this method will have no effect if the * item is already expanded or if it has no children. {@code syncAndRefresh} * indicates whether the changes should be synchronised to the client and * the data provider be notified. * * @param item * the item to expand * @param syncAndRefresh * {@code true} if the changes should be synchronised to the * client and the data provider should be notified of the * changes, {@code * false} otherwise. */ public void expand(T item, boolean syncAndRefresh) { Integer index = syncAndRefresh ? mapper.getIndexOf(item).orElse(null) : null; doExpand(item, index, syncAndRefresh); }