Skip to content

Commit 508fbd3

Browse files
vursensissbruecker
andauthored
refactor: throw when using scrollToIndex(int...) with flat data provider (#8090)
Co-authored-by: Sascha Ißbrücker <[email protected]>
1 parent cbc0f79 commit 508fbd3

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

vaadin-grid-flow-parent/vaadin-grid-flow/src/main/java/com/vaadin/flow/component/treegrid/TreeGrid.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1020,14 +1020,29 @@ public void scrollToIndex(int index) {
10201020
* component will first try to scroll to the item at index 2 in the root
10211021
* level. If that item is expanded, it will then try to scroll to the item
10221022
* at index 1 among its children, and so forth.
1023+
* <p>
1024+
* This method is only supported for data providers that use
1025+
* {@link HierarchyFormat#NESTED}. For {@link HierarchyFormat#FLATTENED},
1026+
* use {@link #scrollToIndex(int)} with a flat index instead.
10231027
*
10241028
* @param path
10251029
* an array of indexes representing the path to the target item
10261030
* @throws IllegalArgumentException
10271031
* if the path is empty
1028-
* @see TreeGrid#scrollToIndex(int)
1032+
* @throws UnsupportedOperationException
1033+
* if the data provider uses a hierarchy format other than
1034+
* {@link HierarchyFormat#NESTED}
10291035
*/
10301036
public void scrollToIndex(int... path) {
1037+
if (!getDataProvider().getHierarchyFormat()
1038+
.equals(HierarchyFormat.NESTED)) {
1039+
throw new UnsupportedOperationException(
1040+
"""
1041+
scrollToIndex(int...) is supported only for data providers that use HierarchyFormat.NESTED. \
1042+
For HierarchyFormat.FLATTENED, use scrollToIndex(int) with a flat index instead.
1043+
""");
1044+
}
1045+
10311046
if (path.length == 0) {
10321047
throw new IllegalArgumentException(
10331048
"At least one index should be provided.");
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2000-2025 Vaadin Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package com.vaadin.flow.component.treegrid;
17+
18+
import org.junit.Assert;
19+
import org.junit.Test;
20+
21+
import com.vaadin.flow.data.provider.hierarchy.HierarchicalDataProvider.HierarchyFormat;
22+
import com.vaadin.flow.data.provider.hierarchy.TreeData;
23+
import com.vaadin.flow.data.provider.hierarchy.TreeDataProvider;
24+
25+
public class ScrollToIndexTest {
26+
private TreeGrid<String> treeGrid = new TreeGrid<>();
27+
private TreeData<String> treeData = new TreeData<>();
28+
29+
@Test
30+
public void nestedHierarchyFormat_scrollToIndexPath_doesNotThrow() {
31+
treeGrid.setDataProvider(
32+
new TreeDataProvider<>(treeData, HierarchyFormat.NESTED));
33+
treeGrid.scrollToIndex(0, 0);
34+
}
35+
36+
@Test
37+
public void flattenedHierarchyFormat_scrollToIndexPath_throws() {
38+
treeGrid.setDataProvider(
39+
new TreeDataProvider<>(treeData, HierarchyFormat.FLATTENED));
40+
Assert.assertThrows(UnsupportedOperationException.class,
41+
() -> treeGrid.scrollToIndex(0, 0));
42+
}
43+
}

0 commit comments

Comments
 (0)