Expose column and row min setting in GridView

- Add method setMinSize for setting minimum width for columns
  and minimum height for rows.
- Relates #805
This commit is contained in:
Janne Valkealahti
2023-07-16 18:13:39 +01:00
parent 3ac48bf15a
commit d053fab383
2 changed files with 218 additions and 21 deletions

View File

@@ -29,6 +29,7 @@ import org.springframework.shell.component.view.event.MouseHandler;
import org.springframework.shell.component.view.event.MouseHandler.MouseHandlerResult;
import org.springframework.shell.component.view.geom.Rectangle;
import org.springframework.shell.component.view.screen.Screen;
import org.springframework.util.Assert;
/**
* {@code GridView} is a layout container with no initial {@link View views}.
@@ -54,38 +55,41 @@ public class GridView extends BoxView {
private boolean showBorders;
/**
* Defines how the columns of the grid are distributed. Each value
* defines the size of one column, starting with the leftmost column. Values
* greater 0 represent absolute column widths (gaps not included). Values less
* or equal 0 represent proportional column widths or fractions of the remaining
* free space, where 0 is treated the same as -1. That is, a column with a value
* Defines how the columns of the grid are distributed. Each value defines the
* size of one column, starting with the leftmost column. Values greater 0
* represent absolute column widths (gaps not included). Values less or equal 0
* represent proportional column widths or fractions of the remaining free
* space, where 0 is treated the same as -1. That is, a column with a value
* of -3 will have three times the width of a column with a value of -1 (or 0).
* The minimum width set with SetMinSize() is always observed.
* The minimum width set with {@link #setMinSize(int, int)} is always observed.
*
* Views may extend beyond the columns defined explicitly with this
* function. A value of 0 is assumed for any undefined column. In fact, if you
* never call this function, all columns occupied by Views will have the
* same width. On the other hand, unoccupied columns defined with this function
* will always take their place.
* <p>Views may extend beyond the columns defined explicitly with this function. A
* value of 0 is assumed for any undefined column. In fact, if you never call
* this function, all columns occupied by Views will have the same width. On the
* other hand, unoccupied columns defined with this function will always take
* their place.
*
* Assuming a total width of the grid of 100 cells and a minimum width of 0, the
* <p>Assuming a total width of the grid of 100 cells and a minimum width of 0, the
* following call will result in columns with widths of 30, 10, 15, 15, and 30
* cells:
* <p>
*
* grid.SetColumns(30, 10, -1, -1, -2)
* grid.setColumnSize(30, 10, -1, -1, -2)
*
* If a primitive were then placed in the 6th and 7th column, the resulting
* <p>If a {@link View} were then placed in the 6th and 7th column, the resulting
* widths would be: 30, 10, 10, 10, 20, 10, and 10 cells.
*
* If you then called SetMinSize() as follows:
* If you then called setMinSize() as follows:
* <p>
*
* grid.SetMinSize(15, 20)
* grid.setMinSize(15, 20)
*
* The resulting widths would be: 30, 15, 15, 15, 20, 15, and 15 cells, a total
* <p>The resulting widths would be: 30, 15, 15, 15, 20, 15, and 15 cells, a total
* of 125 cells, 25 cells wider than the available grid width.
*
* @param columns
* @return
* @param columns the column sizes
* @return a grid view for chaining
* @see #setRowSize(int...)
*/
public GridView setColumnSize(int... columns) {
this.columnSize = columns;
@@ -93,9 +97,10 @@ public class GridView extends BoxView {
}
/**
* For documentation see {@link #setColumnSize(int...)} as it's equivalent for rows.
*
* @param rows
* @return
* @param rows the row sizes
* @return a grid view for chaining
* @see #setColumnSize(int...)
*/
public GridView setRowSize(int... rows) {
@@ -103,6 +108,21 @@ public class GridView extends BoxView {
return this;
}
/**
* Sets an absolute minimum width for rows and an absolute minimum height for
* columns. Negative values cannot be used.
*
* @param minWidth the rows minimum width
* @param minHeight the columns minimum height
* @return a grid view for chaining
*/
public GridView setMinSize(int minWidth, int minHeight) {
Assert.state(minWidth > -1 || minHeight > -1, "Minimum sizes for rows or colums cannot be negative");
this.minWidth = minWidth;
this.minHeight = minHeight;
return this;
}
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,