- java.lang.Object
-
- com.googlecode.lanterna.gui2.GridLayout
-
- All Implemented Interfaces:
LayoutManager
public class GridLayout extends java.lang.Object implements 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
GridLayoutby specifying a number of columns you want your grid to have and then when you add components, you assignLayoutDatato 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 Classes Modifier and Type Class Description static classGridLayout.AlignmentThe 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 classGridLayout.GridLayoutData
-
Field Summary
Fields Modifier and Type Field Description private intbottomMarginSizeprivate booleanchangedprivate static GridLayout.GridLayoutDataDEFAULTprivate inthorizontalSpacingprivate intleftMarginSizeprivate intnumberOfColumnsprivate intrightMarginSizeprivate inttopMarginSizeprivate intverticalSpacing
-
Constructor Summary
Constructors Constructor Description GridLayout(int numberOfColumns)Creates a newGridLayoutwith the specified number of columns.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private Component[][]buildTable(java.util.List<Component> components)static LayoutDatacreateHorizontallyEndAlignedLayoutData(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 LayoutDatacreateHorizontallyFilledLayoutData()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 LayoutDatacreateHorizontallyFilledLayoutData(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 LayoutDatacreateLayoutData(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 LayoutDatacreateLayoutData(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 LayoutDatacreateLayoutData(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.voiddoLayout(TerminalSize area, java.util.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)intgetBottomMarginSize()Returns the bottom margin, i.e.private java.util.Set<java.lang.Integer>getExpandableColumns(Component[][] table)private java.util.Set<java.lang.Integer>getExpandableRows(Component[][] table)intgetHorizontalSpacing()Returns the horizontal spacing, i.e.private GridLayout.GridLayoutDatagetLayoutData(Component component)intgetLeftMarginSize()Returns the left margin, i.e.private int[]getPreferredColumnWidths(Component[][] table)private int[]getPreferredRowHeights(Component[][] table)TerminalSizegetPreferredSize(java.util.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.intgetRightMarginSize()Returns the right margin, i.e.intgetTopMarginSize()Returns the top margin, i.e.intgetVerticalSpacing()Returns the vertical spacing, i.e.private intgrabExtraHorizontalSpace(TerminalSize area, int[] columnWidths, java.util.Set<java.lang.Integer> expandableColumns, int totalWidth)private intgrabExtraVerticalSpace(TerminalSize area, int[] rowHeights, java.util.Set<java.lang.Integer> expandableRows, int totalHeight)booleanhasChanged()Returns true if the internal state of this LayoutManager has changed since the last call to doLayout.GridLayoutsetBottomMarginSize(int bottomMarginSize)Sets the bottom margin, i.e.GridLayoutsetHorizontalSpacing(int horizontalSpacing)Sets the horizontal spacing, i.e.GridLayoutsetLeftMarginSize(int leftMarginSize)Sets the left margin, i.e.GridLayoutsetRightMarginSize(int rightMarginSize)Sets the right margin, i.e.GridLayoutsetTopMarginSize(int topMarginSize)Sets the top margin, i.e.GridLayoutsetVerticalSpacing(int verticalSpacing)Sets the vertical spacing, i.e.private intshrinkHeightToFitArea(TerminalSize area, int[] rowHeights)private intshrinkWidthToFitArea(TerminalSize area, int[] columnWidths)
-
-
-
Field Detail
-
DEFAULT
private static final GridLayout.GridLayoutData 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 Detail
-
GridLayout
public GridLayout(int numberOfColumns)
Creates a newGridLayoutwith 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 Detail
-
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
public static LayoutData 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
public 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 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
public 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 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
public GridLayout setHorizontalSpacing(int horizontalSpacing)
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
public GridLayout setVerticalSpacing(int verticalSpacing)
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
public GridLayout setTopMarginSize(int topMarginSize)
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
public GridLayout setBottomMarginSize(int bottomMarginSize)
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
public GridLayout setLeftMarginSize(int leftMarginSize)
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
public GridLayout setRightMarginSize(int rightMarginSize)
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:LayoutManagerReturns 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:
hasChangedin interfaceLayoutManager- Returns:
trueif this layout manager's internal state has changed since the last call todoLayout
-
getPreferredSize
public TerminalSize getPreferredSize(java.util.List<Component> components)
Description copied from interface:LayoutManagerThis 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:
getPreferredSizein interfaceLayoutManager- Parameters:
components- List of components- Returns:
- Size the layout manager would like to have
-
doLayout
public void doLayout(TerminalSize area, java.util.List<Component> components)
Description copied from interface:LayoutManagerGiven 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:
doLayoutin interfaceLayoutManager- Parameters:
area- Size available to this layout manager to lay out the components oncomponents- List of components to lay out
-
getPreferredColumnWidths
private int[] getPreferredColumnWidths(Component[][] table)
-
getPreferredRowHeights
private int[] getPreferredRowHeights(Component[][] table)
-
getExpandableColumns
private java.util.Set<java.lang.Integer> getExpandableColumns(Component[][] table)
-
getExpandableRows
private java.util.Set<java.lang.Integer> getExpandableRows(Component[][] table)
-
shrinkWidthToFitArea
private int shrinkWidthToFitArea(TerminalSize area, int[] columnWidths)
-
shrinkHeightToFitArea
private int shrinkHeightToFitArea(TerminalSize area, int[] rowHeights)
-
grabExtraHorizontalSpace
private int grabExtraHorizontalSpace(TerminalSize area, int[] columnWidths, java.util.Set<java.lang.Integer> expandableColumns, int totalWidth)
-
grabExtraVerticalSpace
private int grabExtraVerticalSpace(TerminalSize area, int[] rowHeights, java.util.Set<java.lang.Integer> expandableRows, int totalHeight)
-
eliminateUnusedRowsAndColumns
private Component[][] eliminateUnusedRowsAndColumns(Component[][] table)
-
getLayoutData
private GridLayout.GridLayoutData getLayoutData(Component component)
-
-