|
30 | 30 | import com.vaadin.flow.component.ComponentUtil;
|
31 | 31 | import com.vaadin.flow.component.dependency.JsModule;
|
32 | 32 | import com.vaadin.flow.component.grid.Grid;
|
33 |
| -import com.vaadin.flow.component.grid.Grid.Column; |
34 | 33 | import com.vaadin.flow.component.grid.GridArrayUpdater;
|
35 | 34 | import com.vaadin.flow.component.grid.dataview.GridDataView;
|
36 | 35 | import com.vaadin.flow.component.grid.dataview.GridLazyDataView;
|
37 | 36 | import com.vaadin.flow.component.grid.dataview.GridListDataView;
|
38 | 37 | import com.vaadin.flow.component.internal.AllowInert;
|
39 | 38 | import com.vaadin.flow.data.binder.PropertyDefinition;
|
40 |
| -import com.vaadin.flow.data.provider.ArrayUpdater; |
41 | 39 | import com.vaadin.flow.data.provider.BackEndDataProvider;
|
42 | 40 | import com.vaadin.flow.data.provider.CallbackDataProvider;
|
43 | 41 | import com.vaadin.flow.data.provider.CompositeDataGenerator;
|
|
50 | 48 | import com.vaadin.flow.data.provider.hierarchy.HierarchicalDataProvider.HierarchyFormat;
|
51 | 49 | import com.vaadin.flow.data.provider.hierarchy.HierarchicalQuery;
|
52 | 50 | import com.vaadin.flow.data.provider.hierarchy.TreeData;
|
| 51 | +import com.vaadin.flow.data.provider.hierarchy.TreeDataProvider; |
53 | 52 | import com.vaadin.flow.data.renderer.ComponentRenderer;
|
54 | 53 | import com.vaadin.flow.data.renderer.LitRenderer;
|
55 | 54 | import com.vaadin.flow.data.renderer.Renderer;
|
@@ -214,43 +213,6 @@ public TreeGrid(HierarchicalDataProvider<T, ?> dataProvider) {
|
214 | 213 | setDataProvider(dataProvider);
|
215 | 214 | }
|
216 | 215 |
|
217 |
| - private static class TreeGridDataCommunicator<T> |
218 |
| - extends HierarchicalDataCommunicator<T> { |
219 |
| - private Element element; |
220 |
| - |
221 |
| - public TreeGridDataCommunicator(Element element, |
222 |
| - CompositeDataGenerator<T> dataGenerator, |
223 |
| - ArrayUpdater arrayUpdater, |
224 |
| - SerializableSupplier<ValueProvider<T, String>> uniqueKeyProviderSupplier) { |
225 |
| - super(dataGenerator, arrayUpdater, element.getNode(), |
226 |
| - uniqueKeyProviderSupplier); |
227 |
| - this.element = element; |
228 |
| - } |
229 |
| - |
230 |
| - @Override |
231 |
| - public void reset() { |
232 |
| - super.reset(); |
233 |
| - if (element != null) { |
234 |
| - element.callJsFunction("$connector.reset"); |
235 |
| - } |
236 |
| - } |
237 |
| - |
238 |
| - @Override |
239 |
| - protected List<T> preloadFlatRangeForward(int start, int length) { |
240 |
| - return super.preloadFlatRangeForward(start, length); |
241 |
| - } |
242 |
| - |
243 |
| - @Override |
244 |
| - protected List<T> preloadFlatRangeBackward(int start, int length) { |
245 |
| - return super.preloadFlatRangeBackward(start, length); |
246 |
| - } |
247 |
| - |
248 |
| - @Override |
249 |
| - protected int resolveIndexPath(int... path) { |
250 |
| - return super.resolveIndexPath(path); |
251 |
| - } |
252 |
| - } |
253 |
| - |
254 | 216 | private static class TreeDataCommunicatorBuilder<T>
|
255 | 217 | extends DataCommunicatorBuilder<T, GridArrayUpdater> {
|
256 | 218 |
|
@@ -1102,7 +1064,7 @@ int setViewportRangeByIndexPath(int[] path, int padding) {
|
1102 | 1064 | // page-aligned.
|
1103 | 1065 | dataCommunicator.preloadFlatRangeForward(flatIndex, padding + pageSize);
|
1104 | 1066 |
|
1105 |
| - // Repeat the process backward to preload enough items behing the |
| 1067 | + // Repeat the process backward to preload enough items behind the |
1106 | 1068 | // resolved flat index. Adding the page size is essential. Without it,
|
1107 | 1069 | // the following call to dataCommunicator.setViewportRange will try to
|
1108 | 1070 | // load uncovered expanded items forward, shifting the range and causing
|
@@ -1130,21 +1092,69 @@ int setViewportRangeByIndexPath(int[] path, int padding) {
|
1130 | 1092 | }
|
1131 | 1093 |
|
1132 | 1094 | /**
|
1133 |
| - * TreeGrid does not support scrolling to a given item. Use |
1134 |
| - * {@link #scrollToIndex(int...)} instead. |
| 1095 | + * Scrolls to an item within the tree. If the ancestors of the item are not |
| 1096 | + * expanded, this method expands them before scrolling. Does not fire any |
| 1097 | + * {@link ExpandEvent}s for the ancestors expanded during scrolling. |
1135 | 1098 | * <p>
|
1136 |
| - * This method is inherited from Grid and has been marked as deprecated to |
1137 |
| - * indicate that it is not supported. This method will throw an |
1138 |
| - * {@link UnsupportedOperationException}. |
| 1099 | + * In order to be able to use this method, the data provider should |
| 1100 | + * implement {@link HierarchicalDataProvider#getParent(T)} and |
| 1101 | + * {@link HierarchicalDataProvider#getItemIndex(T, HierarchicalQuery)}. The |
| 1102 | + * following table shows which methods have to be explicitly implemented |
| 1103 | + * based on the data provider types. |
| 1104 | + * <table> |
| 1105 | + * <tr> |
| 1106 | + * <th>HierarchicalDataProvider</th> |
| 1107 | + * <th>{@link HierarchicalDataProvider#isInMemory() isInMemory()}</th> |
| 1108 | + * <th>{@link HierarchicalDataProvider#getItemIndex(T, HierarchicalQuery) |
| 1109 | + * getItemIndex()}</th> |
| 1110 | + * <th>{@link HierarchicalDataProvider#getParent(T) getParent()}</th> |
| 1111 | + * </tr> |
| 1112 | + * <tr> |
| 1113 | + * <td>{@link HierarchyFormat#NESTED HierarchyFormat.NESTED}</td> |
| 1114 | + * <td>true</td> |
| 1115 | + * <td>not required</td> |
| 1116 | + * <td>required</td> |
| 1117 | + * </tr> |
| 1118 | + * <tr> |
| 1119 | + * <td>{@link HierarchyFormat#NESTED HierarchyFormat.NESTED}</td> |
| 1120 | + * <td>false</td> |
| 1121 | + * <td>required</td> |
| 1122 | + * <td>required</td> |
| 1123 | + * </tr> |
| 1124 | + * <tr> |
| 1125 | + * <td>{@link HierarchyFormat#FLATTENED HierarchyFormat.FLATTENED}</td> |
| 1126 | + * <td>true</td> |
| 1127 | + * <td>not required</td> |
| 1128 | + * <td>required</td> |
| 1129 | + * </tr> |
| 1130 | + * <tr> |
| 1131 | + * <td>{@link HierarchyFormat#FLATTENED HierarchyFormat.FLATTENED}</td> |
| 1132 | + * <td>false</td> |
| 1133 | + * <td>required</td> |
| 1134 | + * <td>required</td> |
| 1135 | + * </tr> |
| 1136 | + * <tr> |
| 1137 | + * <td>{@link TreeDataProvider}</td> |
| 1138 | + * <td>true</td> |
| 1139 | + * <td>not required</td> |
| 1140 | + * <td>not required</td> |
| 1141 | + * </tr> |
| 1142 | + * </table> |
1139 | 1143 | *
|
1140 | 1144 | * @param item
|
1141 | 1145 | * the item to scroll to
|
1142 |
| - * @deprecated |
| 1146 | + * @throws IllegalArgumentException |
| 1147 | + * if the item does not belong to the tree |
1143 | 1148 | */
|
1144 |
| - @Deprecated |
1145 | 1149 | @Override
|
1146 | 1150 | public void scrollToItem(T item) {
|
1147 |
| - throw new UnsupportedOperationException( |
1148 |
| - "scrollToItem method is not supported in TreeGrid"); |
| 1151 | + Objects.requireNonNull(item, "Item to scroll to cannot be null."); |
| 1152 | + var indexPath = ((TreeGridDataCommunicator<T>) getDataCommunicator()) |
| 1153 | + .resolveItem(item); |
| 1154 | + if (indexPath.length == 1) { |
| 1155 | + scrollToIndex(indexPath[0]); |
| 1156 | + } else { |
| 1157 | + scrollToIndex(indexPath); |
| 1158 | + } |
1149 | 1159 | }
|
1150 | 1160 | }
|
0 commit comments