diff options
author | Adam Wagner <wbadam@users.noreply.github.com> | 2018-02-08 11:20:32 +0200 |
---|---|---|
committer | Teemu Suo-Anttila <tsuoanttila@users.noreply.github.com> | 2018-02-08 11:20:32 +0200 |
commit | 1817a82ed8f3c8839c18d9ce6b68e5838940a22c (patch) | |
tree | 3a110ba043aa4bea415b4060c4eba4439703711b /server/src/main/java/com/vaadin/data/provider | |
parent | 299520fac11dac9b31d6fd762a3eb76ef827ec5b (diff) | |
download | vaadin-framework-1817a82ed8f3c8839c18d9ce6b68e5838940a22c.tar.gz vaadin-framework-1817a82ed8f3c8839c18d9ce6b68e5838940a22c.zip |
Add recursive expand and collapse method to TreeGrid and Tree (#10283)
Diffstat (limited to 'server/src/main/java/com/vaadin/data/provider')
-rw-r--r-- | server/src/main/java/com/vaadin/data/provider/HierarchicalDataCommunicator.java | 157 | ||||
-rw-r--r-- | server/src/main/java/com/vaadin/data/provider/HierarchyMapper.java | 79 |
2 files changed, 191 insertions, 45 deletions
diff --git a/server/src/main/java/com/vaadin/data/provider/HierarchicalDataCommunicator.java b/server/src/main/java/com/vaadin/data/provider/HierarchicalDataCommunicator.java index ffdcf042c9..e09681978d 100644 --- a/server/src/main/java/com/vaadin/data/provider/HierarchicalDataCommunicator.java +++ b/server/src/main/java/com/vaadin/data/provider/HierarchicalDataCommunicator.java @@ -156,33 +156,83 @@ public class HierarchicalDataCommunicator<T> extends DataCommunicator<T> { } /** - * Collapses given item, removing all its subtrees. Calling this method will - * have no effect if the row is already collapsed. + * 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) { - if (mapper.isExpanded(item)) { - doCollapse(item, mapper.getIndexOf(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. + * {@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 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 collapse(T item, boolean syncAndRefresh) { + Integer index = syncAndRefresh ? mapper.getIndexOf(item).orElse(null) : null; + doCollapse(item, index, syncAndRefresh); } /** - * Collapses given item, removing all its subtrees. 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. + * Collapses the given item and removes its sub-hierarchy. Calling this + * method will have no effect if the row is already collapsed. * - * @see #collapse(Object) + * @param item + * the item to collapse + * @param index + * the index of the item + */ + public void collapse(T item, Integer index) { + doCollapse(item, index, true); + } + + /** + * Collapses 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. * + * * @param item * the item to collapse * @param index * the index of the item + * @deprecated Use {@link #collapse(Object, Integer)} instead. */ + @Deprecated public void doCollapse(T item, Optional<Integer> index) { - if (mapper.isExpanded(item)) { - Range removedRows = mapper.doCollapse(item, index); + doCollapse(item, index.orElse(null), true); + } + + /** + * 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()); @@ -193,44 +243,97 @@ public class HierarchicalDataCommunicator<T> extends DataCommunicator<T> { /** * Expands the given item. Calling this method will have no effect if the - * row is already expanded. + * item is already expanded or if it has no children. * * @param item * the item to expand */ public void expand(T item) { - if (!mapper.isExpanded(item) && mapper.hasChildren(item)) { - doExpand(item, mapper.getIndexOf(item)); - } + expand(item, true); } /** - * 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. + * 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. * - * @see #expand(Object) + * @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); + } + + /** + * 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 doExpand(T item, Optional<Integer> index) { - if (!mapper.isExpanded(item)) { - Range addedRows = mapper.doExpand(item, index); + public void expand(T item, Integer index) { + doExpand(item, index, true); + } + + /** + * 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()) { - int start = addedRows.getStart(); - getClientRpc().insertRows(start, addedRows.length()); - Stream<T> children = mapper.fetchItems(item, - Range.withLength(0, addedRows.length())); - pushData(start, children.collect(Collectors.toList())); + 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); } } /** + * 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)); + } + + /** * Returns whether given item has children. * * @param item diff --git a/server/src/main/java/com/vaadin/data/provider/HierarchyMapper.java b/server/src/main/java/com/vaadin/data/provider/HierarchyMapper.java index e918c4a209..f6f9f8b9a4 100644 --- a/server/src/main/java/com/vaadin/data/provider/HierarchyMapper.java +++ b/server/src/main/java/com/vaadin/data/provider/HierarchyMapper.java @@ -123,46 +123,89 @@ public class HierarchyMapper<T, F> implements DataGenerator<T> { * @param item * the item to expand * @param position + * the index of the item + * @return range of rows added by expanding the item + */ + public Range expand(T item, Integer position) { + if (doExpand(item) && position != null) { + return Range.withLength(position + 1, + (int) getHierarchy(item, false).count()); + } + + return Range.emptyRange(); + } + + /** + * Expands the given item. + * + * @param item + * the item to expand + * @param position * the index of item * @return range of rows added by expanding the item + * @deprecated Use {@link #expand(Object, Integer)} instead. */ + @Deprecated public Range doExpand(T item, Optional<Integer> position) { - Range rows = Range.withLength(0, 0); + return expand(item, position.orElse(null)); + } + + /** + * 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)) { - Object id = getDataProvider().getId(item); - expandedItemIds.add(id); - if (position.isPresent()) { - rows = Range.withLength(position.get() + 1, - (int) getHierarchy(item, false).count()); - } + expandedItemIds.add(getDataProvider().getId(item)); + expanded = true; } - return rows; + return expanded; } /** * Collapses the given item. * * @param item - * the item to expand + * the item to collapse * @param position - * the index of item + * the index of the item * * @return range of rows removed by collapsing the item */ - public Range doCollapse(T item, Optional<Integer> position) { - Range removedRows = Range.withLength(0, 0); + public Range collapse(T item, Integer position) { + Range removedRows = Range.emptyRange(); if (isExpanded(item)) { - Object id = getDataProvider().getId(item); - if (position.isPresent()) { - long childCount = getHierarchy(item, false).count(); - removedRows = Range.withLength(position.get() + 1, - (int) childCount); + if (position != null) { + removedRows = Range.withLength(position + 1, + (int) getHierarchy(item, false).count()); } - expandedItemIds.remove(id); + expandedItemIds.remove(getDataProvider().getId(item)); } return removedRows; } + /** + * Collapses the given item. + * + * @param item + * the item to collapse + * @param position + * the index of item + * + * @return range of rows removed by collapsing the item + * @deprecated Use {@link #collapse(Object, Integer)} instead. + */ + @Deprecated + public Range doCollapse(T item, Optional<Integer> position) { + return collapse(item, position.orElse(null)); + } + @Override public void generateData(T item, JsonObject jsonObject) { JsonObject hierarchyData = Json.createObject(); |