Skip to content

Commit 3d73abc

Browse files
google-labs-jules[bot]paodb
authored andcommitted
fix: ensure that Grid columns are configured before setting a footer toolbar
Close #129
1 parent ea5ddb5 commit 3d73abc

File tree

3 files changed

+28
-19
lines changed

3 files changed

+28
-19
lines changed

src/main/java/com/flowingcode/vaadin/addons/gridhelpers/FooterToolbarGridHelper.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* #%L
33
* Grid Helpers Add-on
44
* %%
5-
* Copyright (C) 2022 - 2024 Flowing Code
5+
* Copyright (C) 2022 - 2025 Flowing Code
66
* %%
77
* Licensed under the Apache License, Version 2.0 (the "License");
88
* you may not use this file except in compliance with the License.
@@ -36,6 +36,9 @@ class FooterToolbarGridHelper implements Serializable {
3636

3737
public void setFooterToolbar(Component toolBar) {
3838
Grid<?> grid = helper.getGrid();
39+
if (grid.getColumns().isEmpty()) {
40+
throw new IllegalStateException("Cannot set footer toolbar: Grid columns have not been configured.");
41+
}
3942
if (grid.getFooterRows().isEmpty()) {
4043
// create a fake footer and hide it (workaround:
4144
// https://github.com/vaadin/flow-components/issues/1558#issuecomment-987783794)

src/main/java/com/flowingcode/vaadin/addons/gridhelpers/GridHelper.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* #%L
33
* Grid Helpers Add-on
44
* %%
5-
* Copyright (C) 2022 - 2024 Flowing Code
5+
* Copyright (C) 2022 - 2025 Flowing Code
66
* %%
77
* Licensed under the Apache License, Version 2.0 (the "License");
88
* you may not use this file except in compliance with the License.
@@ -348,6 +348,16 @@ public static Component getEmptyGridLabel(Grid<?> grid) {
348348

349349
private final FooterToolbarGridHelper footerToolbar = new FooterToolbarGridHelper(this);
350350

351+
/**
352+
* Adds a toolbar component to the footer of the grid.
353+
* <p>
354+
* Note: The grid must have its columns configured before calling this method.
355+
* Otherwise, an {@link IllegalStateException} will be thrown.
356+
*
357+
* @param grid the grid to add the toolbar to
358+
* @param toolBar the toolbar component to add
359+
* @throws IllegalStateException if the grid columns have not been configured
360+
*/
351361
public static void addToolbarFooter(Grid<?> grid, Component toolBar) {
352362
getHelper(grid).footerToolbar.setFooterToolbar(toolBar);
353363
}

src/test/java/com/flowingcode/vaadin/addons/gridhelpers/test/FooterToolbarTest.java

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* #%L
33
* Grid Helpers Add-on
44
* %%
5-
* Copyright (C) 2022 - 2024 Flowing Code
5+
* Copyright (C) 2022 - 2025 Flowing Code
66
* %%
77
* Licensed under the Apache License, Version 2.0 (the "License");
88
* you may not use this file except in compliance with the License.
@@ -22,28 +22,24 @@
2222
import com.flowingcode.vaadin.addons.gridhelpers.GridHelper;
2323
import com.vaadin.flow.component.grid.Grid;
2424
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
25-
import org.junit.Before;
2625
import org.junit.Test;
27-
import org.junit.Test.None;
2826

2927
public class FooterToolbarTest {
30-
31-
private Grid<Bean> grid;
3228

33-
private HorizontalLayout toolbarFooter;
34-
35-
private class Bean {}
36-
37-
@Before
38-
public void before() {
39-
grid = new Grid<>(Bean.class, false);
29+
private static class Bean {}
30+
31+
@Test
32+
public void addToolbarFooter() {
33+
Grid<Bean> grid = new Grid<>(Bean.class, false);
4034
grid.addColumn(x -> x).setHeader("Header");
41-
toolbarFooter = new HorizontalLayout();
42-
}
43-
44-
@Test(expected = None.class)
45-
public void addToolbarFooter_toolbarFooterIsShown() {
35+
var toolbarFooter = new HorizontalLayout();
4636
GridHelper.addToolbarFooter(grid, toolbarFooter);
4737
}
4838

39+
@Test(expected = IllegalStateException.class)
40+
public void testSetFooterToolbarBeforeColumnsConfiguredThrowsException() {
41+
Grid<Bean> grid = new Grid<>(Bean.class, false);
42+
var toolbarFooter = new HorizontalLayout();
43+
GridHelper.addToolbarFooter(grid, toolbarFooter);
44+
}
4945
}

0 commit comments

Comments
 (0)