From a19a50b3f354dc243952f70afaf10074df0ff7db Mon Sep 17 00:00:00 2001 From: Janne Valkealahti Date: Fri, 21 Jul 2023 18:06:57 +0100 Subject: [PATCH] Polish GridView - Add some new tests - Add javadocs - Relates #805 --- .../component/view/control/GridView.java | 42 ++++++++++++++ .../component/view/control/GridViewTests.java | 56 +++++++++++++++++++ 2 files changed, 98 insertions(+) diff --git a/spring-shell-core/src/main/java/org/springframework/shell/component/view/control/GridView.java b/spring-shell-core/src/main/java/org/springframework/shell/component/view/control/GridView.java index d61c49e7..2754f0b1 100644 --- a/spring-shell-core/src/main/java/org/springframework/shell/component/view/control/GridView.java +++ b/spring-shell-core/src/main/java/org/springframework/shell/component/view/control/GridView.java @@ -123,6 +123,48 @@ public class GridView extends BoxView { return this; } + /** + * Adds a {@link View} and its position to the grid. The top-left corner of the + * {@link View} will be located in the top-left corner of the grid cell at + * the given row and column and will span "rowSpan" rows and "colSpan" columns. + * For example, for a primitive to occupy rows 2, 3, and 4 and columns 5 and 6: + * + *

+ * grid.addItem(view, 2, 5, 3, 2, 0, 0) + * + * If {@code rowSpan} or colSpan is 0, the {@link View} will not be drawn. + * + *

+ * You can add the same {@link View} multiple times with different grid + * positions. The {@code minGridWidth} and {@code minGridHeight} values will + * then determine which of those positions will be used. These minimum values + * refer to the overall size of the grid. If multiple items for the same + * primitive apply, the one that has at least one highest minimum value will + * be used, or the {@link View} added last if those values are the same. Example: + * + *

+ * AddItem(view, 0, 0, 0, 0, 0, 0). // Hide in small grids. + * AddItem(view, 0, 0, 1, 2, 100, 0). // One-column layout for medium grids. + * AddItem(view, 1, 1, 3, 2, 300, 0) // Multi-column layout for large grids. + * + *

+ * To use the same grid layout for all sizes, simply set {@code minGridWidth} + * and {@code minGridHeight} to 0. + * + *

+ * If the item's focus is set to true, it will receive focus when the grid + * receives focus. If there are multiple items with a true focus flag, the + * last visible one that was added will receive focus. + * + * @param view the view to add + * @param row the row + * @param column the column + * @param rowSpan the row span + * @param colSpan the column span + * @param minGridHeight the mininum height + * @param minGridWidth the minimun width + * @return a grid view for chaining + */ public GridView addItem(View view, int row, int column, int rowSpan, int colSpan, int minGridHeight, int minGridWidth) { GridItem gridItem = new GridItem(view, row, column, colSpan, rowSpan, minGridHeight, diff --git a/spring-shell-core/src/test/java/org/springframework/shell/component/view/control/GridViewTests.java b/spring-shell-core/src/test/java/org/springframework/shell/component/view/control/GridViewTests.java index 3a4c7471..ff2d7294 100644 --- a/spring-shell-core/src/test/java/org/springframework/shell/component/view/control/GridViewTests.java +++ b/spring-shell-core/src/test/java/org/springframework/shell/component/view/control/GridViewTests.java @@ -21,6 +21,7 @@ import org.junit.jupiter.api.Test; import org.springframework.shell.component.view.screen.DefaultScreen; import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.Mockito.never; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.verify; @@ -267,6 +268,61 @@ class GridViewTests extends AbstractViewTests { } + @Nested + class DynamicLayoutMinWidthHeight { + + @Test + void widthHides_1_base() { + BoxView sbox1 = spy(new BoxView()); + BoxView sbox2 = spy(new BoxView()); + BoxView sbox3 = spy(new BoxView()); + + GridView grid = new GridView(); + grid.setRowSize(0); + grid.setColumnSize(0, 0, 0); + + grid.addItem(sbox1, 0, 0, 1, 1, 0, 0); + grid.addItem(sbox2, 0, 1, 1, 1, 0, 0); + grid.addItem(sbox3, 0, 2, 1, 1, 0, 0); + + grid.setRect(0, 0, 80, 24); + grid.draw(screen24x80); + + verify(sbox1).setRect(0, 0, 26, 24); + verify(sbox2).setRect(26, 0, 27, 24); + verify(sbox3).setRect(53, 0, 27, 24); + } + + @Test + void widthHides_1_shouldHide() { + BoxView sbox1 = spy(new BoxView()); + BoxView sbox2 = spy(new BoxView()); + BoxView sbox3 = spy(new BoxView()); + + GridView grid = new GridView(); + grid.setRowSize(0); + grid.setColumnSize(0, 0, 0); + + // this should get used + grid.addItem(sbox1, 0, 0, 0, 1, 0, 0); + grid.addItem(sbox2, 0, 0, 1, 3, 0, 0); + grid.addItem(sbox3, 0, 2, 0, 1, 0, 0); + + // this should not get used + grid.addItem(sbox1, 0, 0, 1, 1, 0, 100); + grid.addItem(sbox2, 0, 1, 1, 1, 0, 100); + grid.addItem(sbox3, 0, 2, 1, 1, 0, 100); + + grid.setRect(0, 0, 80, 24); + grid.draw(screen24x80); + + verify(sbox1, never()).setRect(anyInt(), anyInt(), anyInt(), anyInt()); + verify(sbox2).setRect(0, 0, 80, 24); + verify(sbox3, never()).setRect(anyInt(), anyInt(), anyInt(), anyInt()); + } + + } + @Nested class Borders {