java.lang.Object
com.googlecode.lanterna.gui2.GridLayout
- All Implemented Interfaces:
LayoutManager
This emulates the behaviour of the GridLayout in SWT (as opposed to the one in AWT/Swing). I originally ported the
SWT class itself but due to licensing concerns (the eclipse license is not compatible with LGPL) I was advised not to
do that. This is a partial implementation and some of the semantics have changed, but in general it works the same
way so the SWT documentation will generally match.
You use the GridLayout
by specifying a number of columns you want your grid to have and then when you add
components, you assign LayoutData
to these components using the different static methods in this class
(createLayoutData(..)
). You can set components to span both rows and columns, as well as defining how to
distribute the available space.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enum
The enum is used to specify where in a grid cell a component should be placed, in the case that the preferred size of the component is smaller than the space in the cell.(package private) static class
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate int
private boolean
private static final GridLayout.GridLayoutData
private int
private int
private final int
private int
private int
private int
-
Constructor Summary
ConstructorsConstructorDescriptionGridLayout
(int numberOfColumns) Creates a newGridLayout
with the specified number of columns. -
Method Summary
Modifier and TypeMethodDescriptionprivate Component[][]
buildTable
(List<Component> components) static LayoutData
createHorizontallyEndAlignedLayoutData
(int horizontalSpan) This is a shortcut method that will create a grid layout data object that will expand its cell as much as is can vertically and make the component occupy the whole area vertically and center it horizontallystatic LayoutData
This is a shortcut method that will create a grid layout data object that will expand its cell as much as is can horizontally and make the component occupy the whole area horizontally and center it vertically, spanning 1 cell.static LayoutData
createHorizontallyFilledLayoutData
(int horizontalSpan) This is a shortcut method that will create a grid layout data object that will expand its cell as much as is can horizontally and make the component occupy the whole area horizontally and center it verticallystatic LayoutData
createLayoutData
(GridLayout.Alignment horizontalAlignment, GridLayout.Alignment verticalAlignment) Creates a layout data object forGridLayout
:s that specify the horizontal and vertical alignment for the component in case the cell space is larger than the preferred size of the componentstatic LayoutData
createLayoutData
(GridLayout.Alignment horizontalAlignment, GridLayout.Alignment verticalAlignment, boolean grabExtraHorizontalSpace, boolean grabExtraVerticalSpace) Creates a layout data object forGridLayout
:s that specify the horizontal and vertical alignment for the component in case the cell space is larger than the preferred size of the component.static LayoutData
createLayoutData
(GridLayout.Alignment horizontalAlignment, GridLayout.Alignment verticalAlignment, boolean grabExtraHorizontalSpace, boolean grabExtraVerticalSpace, int horizontalSpan, int verticalSpan) Creates a layout data object forGridLayout
:s that specify the horizontal and vertical alignment for the component in case the cell space is larger than the preferred size of the component.void
doLayout
(TerminalSize area, List<Component> components) Given a size constraint, update the location and size of each component in the component list by laying them out in the available area.private Component[][]
eliminateUnusedRowsAndColumns
(Component[][] table) int
Returns the bottom margin, i.e.getExpandableColumns
(Component[][] table) getExpandableRows
(Component[][] table) int
Returns the horizontal spacing, i.e.private GridLayout.GridLayoutData
getLayoutData
(Component component) int
Returns the left margin, i.e.private int[]
getPreferredColumnWidths
(Component[][] table) private int[]
getPreferredRowHeights
(Component[][] table) getPreferredSize
(List<Component> components) This method returns the dimensions it would prefer to have to be able to layout all components while giving all of them as much space as they are asking for.int
Returns the right margin, i.e.int
Returns the top margin, i.e.int
Returns the vertical spacing, i.e.private int
grabExtraHorizontalSpace
(TerminalSize area, int[] columnWidths, Set<Integer> expandableColumns, int totalWidth) private int
grabExtraVerticalSpace
(TerminalSize area, int[] rowHeights, Set<Integer> expandableRows, int totalHeight) boolean
Returns true if the internal state of this LayoutManager has changed since the last call to doLayout.setBottomMarginSize
(int bottomMarginSize) Sets the bottom margin, i.e.setHorizontalSpacing
(int horizontalSpacing) Sets the horizontal spacing, i.e.setLeftMarginSize
(int leftMarginSize) Sets the left margin, i.e.setRightMarginSize
(int rightMarginSize) Sets the right margin, i.e.setTopMarginSize
(int topMarginSize) Sets the top margin, i.e.setVerticalSpacing
(int verticalSpacing) Sets the vertical spacing, i.e.private int
shrinkHeightToFitArea
(TerminalSize area, int[] rowHeights) private int
shrinkWidthToFitArea
(TerminalSize area, int[] columnWidths)
-
Field Details
-
DEFAULT
-
numberOfColumns
private final int numberOfColumns -
horizontalSpacing
private int horizontalSpacing -
verticalSpacing
private int verticalSpacing -
topMarginSize
private int topMarginSize -
bottomMarginSize
private int bottomMarginSize -
leftMarginSize
private int leftMarginSize -
rightMarginSize
private int rightMarginSize -
changed
private boolean changed
-
-
Constructor Details
-
GridLayout
public GridLayout(int numberOfColumns) Creates a newGridLayout
with the specified number of columns. Initially, this layout will have a horizontal spacing of 1 and vertical spacing of 0, with a left and right margin of 1.- Parameters:
numberOfColumns
- Number of columns in this grid
-
-
Method Details
-
createLayoutData
public static LayoutData createLayoutData(GridLayout.Alignment horizontalAlignment, GridLayout.Alignment verticalAlignment) Creates a layout data object forGridLayout
:s that specify the horizontal and vertical alignment for the component in case the cell space is larger than the preferred size of the component- Parameters:
horizontalAlignment
- Horizontal alignment strategyverticalAlignment
- Vertical alignment strategy- Returns:
- The layout data object containing the specified alignments
-
createLayoutData
public static LayoutData createLayoutData(GridLayout.Alignment horizontalAlignment, GridLayout.Alignment verticalAlignment, boolean grabExtraHorizontalSpace, boolean grabExtraVerticalSpace) Creates a layout data object forGridLayout
:s that specify the horizontal and vertical alignment for the component in case the cell space is larger than the preferred size of the component. This method also has fields for indicating that the component would like to take more space if available to the container. For example, if the container is assigned is assigned an area of 50x15, but all the child components in the grid together only asks for 40x10, the remaining 10 columns and 5 rows will be empty. If just a single component asks for extra space horizontally and/or vertically, the grid will expand out to fill the entire area and the text space will be assigned to the component that asked for it.- Parameters:
horizontalAlignment
- Horizontal alignment strategyverticalAlignment
- Vertical alignment strategygrabExtraHorizontalSpace
- If set totrue
, this component will ask to be assigned extra horizontal space if there is any to assigngrabExtraVerticalSpace
- If set totrue
, this component will ask to be assigned extra vertical space if there is any to assign- Returns:
- The layout data object containing the specified alignments and size requirements
-
createLayoutData
public static LayoutData createLayoutData(GridLayout.Alignment horizontalAlignment, GridLayout.Alignment verticalAlignment, boolean grabExtraHorizontalSpace, boolean grabExtraVerticalSpace, int horizontalSpan, int verticalSpan) Creates a layout data object forGridLayout
:s that specify the horizontal and vertical alignment for the component in case the cell space is larger than the preferred size of the component. This method also has fields for indicating that the component would like to take more space if available to the container. For example, if the container is assigned is assigned an area of 50x15, but all the child components in the grid together only asks for 40x10, the remaining 10 columns and 5 rows will be empty. If just a single component asks for extra space horizontally and/or vertically, the grid will expand out to fill the entire area and the text space will be assigned to the component that asked for it. It also puts in data on how many rows and/or columns the component should span.- Parameters:
horizontalAlignment
- Horizontal alignment strategyverticalAlignment
- Vertical alignment strategygrabExtraHorizontalSpace
- If set totrue
, this component will ask to be assigned extra horizontal space if there is any to assigngrabExtraVerticalSpace
- If set totrue
, this component will ask to be assigned extra vertical space if there is any to assignhorizontalSpan
- How many "cells" this component wants to span horizontallyverticalSpan
- How many "cells" this component wants to span vertically- Returns:
- The layout data object containing the specified alignments, size requirements and cell spanning
-
createHorizontallyFilledLayoutData
This is a shortcut method that will create a grid layout data object that will expand its cell as much as is can horizontally and make the component occupy the whole area horizontally and center it vertically, spanning 1 cell.- Returns:
- Layout data object with the specified span and horizontally expanding as much as it can
-
createHorizontallyFilledLayoutData
This is a shortcut method that will create a grid layout data object that will expand its cell as much as is can horizontally and make the component occupy the whole area horizontally and center it vertically- Parameters:
horizontalSpan
- How many cells to span horizontally- Returns:
- Layout data object with the specified span and horizontally expanding as much as it can
-
createHorizontallyEndAlignedLayoutData
This is a shortcut method that will create a grid layout data object that will expand its cell as much as is can vertically and make the component occupy the whole area vertically and center it horizontally- Parameters:
horizontalSpan
- How many cells to span vertically- Returns:
- Layout data object with the specified span and vertically expanding as much as it can
-
getHorizontalSpacing
public int getHorizontalSpacing()Returns the horizontal spacing, i.e. the number of empty columns between each cell- Returns:
- Horizontal spacing
-
setHorizontalSpacing
Sets the horizontal spacing, i.e. the number of empty columns between each cell- Parameters:
horizontalSpacing
- New horizontal spacing- Returns:
- Itself
-
getVerticalSpacing
public int getVerticalSpacing()Returns the vertical spacing, i.e. the number of empty columns between each row- Returns:
- Vertical spacing
-
setVerticalSpacing
Sets the vertical spacing, i.e. the number of empty columns between each row- Parameters:
verticalSpacing
- New vertical spacing- Returns:
- Itself
-
getTopMarginSize
public int getTopMarginSize()Returns the top margin, i.e. number of empty rows above the first row in the grid- Returns:
- Top margin, in number of rows
-
setTopMarginSize
Sets the top margin, i.e. number of empty rows above the first row in the grid- Parameters:
topMarginSize
- Top margin, in number of rows- Returns:
- Itself
-
getBottomMarginSize
public int getBottomMarginSize()Returns the bottom margin, i.e. number of empty rows below the last row in the grid- Returns:
- Bottom margin, in number of rows
-
setBottomMarginSize
Sets the bottom margin, i.e. number of empty rows below the last row in the grid- Parameters:
bottomMarginSize
- Bottom margin, in number of rows- Returns:
- Itself
-
getLeftMarginSize
public int getLeftMarginSize()Returns the left margin, i.e. number of empty columns left of the first column in the grid- Returns:
- Left margin, in number of columns
-
setLeftMarginSize
Sets the left margin, i.e. number of empty columns left of the first column in the grid- Parameters:
leftMarginSize
- Left margin, in number of columns- Returns:
- Itself
-
getRightMarginSize
public int getRightMarginSize()Returns the right margin, i.e. number of empty columns right of the last column in the grid- Returns:
- Right margin, in number of columns
-
setRightMarginSize
Sets the right margin, i.e. number of empty columns right of the last column in the grid- Parameters:
rightMarginSize
- Right margin, in number of columns- Returns:
- Itself
-
hasChanged
public boolean hasChanged()Description copied from interface:LayoutManager
Returns true if the internal state of this LayoutManager has changed since the last call to doLayout. This will tell the container that it needs to call doLayout again.- Specified by:
hasChanged
in interfaceLayoutManager
- Returns:
true
if this layout manager's internal state has changed since the last call todoLayout
-
getPreferredSize
Description copied from interface:LayoutManager
This method returns the dimensions it would prefer to have to be able to layout all components while giving all of them as much space as they are asking for.- Specified by:
getPreferredSize
in interfaceLayoutManager
- Parameters:
components
- List of components- Returns:
- Size the layout manager would like to have
-
doLayout
Description copied from interface:LayoutManager
Given a size constraint, update the location and size of each component in the component list by laying them out in the available area. This method will callsetPosition(..)
andsetSize(..)
on the Components.- Specified by:
doLayout
in interfaceLayoutManager
- Parameters:
area
- Size available to this layout manager to lay out the components oncomponents
- List of components to lay out
-
getPreferredColumnWidths
-
getPreferredRowHeights
-
getExpandableColumns
-
getExpandableRows
-
shrinkWidthToFitArea
-
shrinkHeightToFitArea
-
grabExtraHorizontalSpace
private int grabExtraHorizontalSpace(TerminalSize area, int[] columnWidths, Set<Integer> expandableColumns, int totalWidth) -
grabExtraVerticalSpace
private int grabExtraVerticalSpace(TerminalSize area, int[] rowHeights, Set<Integer> expandableRows, int totalHeight) -
buildTable
-
eliminateUnusedRowsAndColumns
-
getLayoutData
-