Class GridLayout

java.lang.Object
com.googlecode.lanterna.gui2.GridLayout
All Implemented Interfaces:
LayoutManager

public class GridLayout extends 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 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.

  • Field Details

    • 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 Details

    • GridLayout

      public GridLayout(int numberOfColumns)
      Creates a new GridLayout 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 for GridLayout: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 strategy
      verticalAlignment - 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 for GridLayout: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 strategy
      verticalAlignment - Vertical alignment strategy
      grabExtraHorizontalSpace - If set to true, this component will ask to be assigned extra horizontal space if there is any to assign
      grabExtraVerticalSpace - If set to true, 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 for GridLayout: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 strategy
      verticalAlignment - Vertical alignment strategy
      grabExtraHorizontalSpace - If set to true, this component will ask to be assigned extra horizontal space if there is any to assign
      grabExtraVerticalSpace - If set to true, this component will ask to be assigned extra vertical space if there is any to assign
      horizontalSpan - How many "cells" this component wants to span horizontally
      verticalSpan - 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: 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 interface LayoutManager
      Returns:
      true if this layout manager's internal state has changed since the last call to doLayout
    • getPreferredSize

      public TerminalSize getPreferredSize(List<Component> components)
      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 interface LayoutManager
      Parameters:
      components - List of components
      Returns:
      Size the layout manager would like to have
    • doLayout

      public void doLayout(TerminalSize area, List<Component> components)
      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 call setPosition(..) and setSize(..) on the Components.
      Specified by:
      doLayout in interface LayoutManager
      Parameters:
      area - Size available to this layout manager to lay out the components on
      components - List of components to lay out
    • getPreferredColumnWidths

      private int[] getPreferredColumnWidths(Component[][] table)
    • getPreferredRowHeights

      private int[] getPreferredRowHeights(Component[][] table)
    • getExpandableColumns

      private Set<Integer> getExpandableColumns(Component[][] table)
    • getExpandableRows

      private Set<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, Set<Integer> expandableColumns, int totalWidth)
    • grabExtraVerticalSpace

      private int grabExtraVerticalSpace(TerminalSize area, int[] rowHeights, Set<Integer> expandableRows, int totalHeight)
    • buildTable

      private Component[][] buildTable(List<Component> components)
    • eliminateUnusedRowsAndColumns

      private Component[][] eliminateUnusedRowsAndColumns(Component[][] table)
    • getLayoutData

      private GridLayout.GridLayoutData getLayoutData(Component component)