Class Grid

java.lang.Object
net.miginfocom.layout.Grid

public final class Grid extends Object
Holds components in a grid. Does most of the logic behind the layout manager.
  • Field Details

    • TEST_GAPS

      public static final boolean TEST_GAPS
      See Also:
    • GROW_100

      private static final Float[] GROW_100
    • DOCK_DIM_CONSTRAINT

      private static final DimConstraint DOCK_DIM_CONSTRAINT
    • MAX_GRID

      private static final int MAX_GRID
      This is the maximum grid position for "normal" components. Docking components use the space out to MAX_DOCK_GRID and below 0.
      See Also:
    • MAX_DOCK_GRID

      private static final int MAX_DOCK_GRID
      Docking components will use the grid coordinates -MAX_DOCK_GRID -> 0 and MAX_GRID -> MAX_DOCK_GRID.
      See Also:
    • GAP_RC_CONST

      private static final ResizeConstraint GAP_RC_CONST
      A constraint used for gaps.
    • GAP_RC_CONST_PUSH

      private static final ResizeConstraint GAP_RC_CONST_PUSH
    • DEF_CC

      private static final CC DEF_CC
      Used for components that doesn't have a CC set. Not that it's really really important that the CC is never changed in this Grid class.
    • lc

      private final LC lc
      The constraints. Never null.
    • container

      private final ContainerWrapper container
      The parent that is layout out and this grid is done for. Never null.
    • grid

      private final LinkedHashMap<Integer,Grid.Cell> grid
      An x, y array implemented as a sparse array to accommodate for any grid size without wasting memory (or rather 15 bit (0-MAX_GRID * 0-MAX_GRID).
    • wrapGapMap

      private HashMap<Integer,BoundSize> wrapGapMap
    • rowIndexes

      private final TreeSet<Integer> rowIndexes
      The size of the grid. Row count and column count.
    • colIndexes

      private final TreeSet<Integer> colIndexes
      The size of the grid. Row count and column count.
    • rowConstr

      private final AC rowConstr
      The row and column specifications.
    • colConstr

      private final AC colConstr
      The row and column specifications.
    • colFlowSpecs

      private Grid.FlowSizeSpec colFlowSpecs
      The in the constructor calculated min/pref/max sizes of the rows and columns.
    • rowFlowSpecs

      private Grid.FlowSizeSpec rowFlowSpecs
      The in the constructor calculated min/pref/max sizes of the rows and columns.
    • colGroupLists

      private final ArrayList<Grid.LinkedDimGroup>[] colGroupLists
      Components that are connections in one dimension (such as baseline alignment for instance) are grouped together and stored here. One for each row/column.
    • rowGroupLists

      private final ArrayList<Grid.LinkedDimGroup>[] rowGroupLists
      Components that are connections in one dimension (such as baseline alignment for instance) are grouped together and stored here. One for each row/column.
    • width

      private int[] width
      The in the constructor calculated min/pref/max size of the whole grid.
    • height

      private int[] height
      The in the constructor calculated min/pref/max size of the whole grid.
    • debugRects

      private ArrayList<int[]> debugRects
      If debug is on contains the bounds for things to paint when calling ContainerWrapper.paintDebugCell(int, int, int, int)
    • linkTargetIDs

      private HashMap<String,Boolean> linkTargetIDs
      If any of the absolute coordinates for component bounds has links the name of the target is in this Set. Since it requires some memory and computations this is checked at the creation so that the link information is only created if needed later.

      The boolean is true for groups id:s and null for normal id:s.

    • dockOffY

      private final int dockOffY
    • dockOffX

      private final int dockOffX
    • pushXs

      private final Float[] pushXs
    • pushYs

      private final Float[] pushYs
    • callbackList

      private final ArrayList<LayoutCallback> callbackList
    • lastRefWidth

      private int lastRefWidth
    • lastRefHeight

      private int lastRefHeight
    • PARENT_ROWCOL_SIZES_MAP

      private static WeakHashMap<Object,int[][]>[] PARENT_ROWCOL_SIZES_MAP
    • PARENT_GRIDPOS_MAP

      private static WeakHashMap<Object,ArrayList<Grid.WeakCell>> PARENT_GRIDPOS_MAP
  • Constructor Details

    • Grid

      public Grid(ContainerWrapper container, LC lc, AC rowConstr, AC colConstr, Map<? extends ComponentWrapper,CC> ccMap, ArrayList<LayoutCallback> callbackList)
      Constructor.
      Parameters:
      container - The container that will be laid out.
      lc - The form flow constraints.
      rowConstr - The rows specifications. If more cell rows are required, the last element will be used for when there is no corresponding element in this array.
      colConstr - The columns specifications. If more cell rows are required, the last element will be used for when there is no corresponding element in this array.
      ccMap - The map containing the parsed constraints for each child component of parent. Will not be altered. Can have null CC which will use a common cached one.
      callbackList - A list of callbacks or null if none. Will not be altered.
  • Method Details

    • ensureIndexSizes

      private void ensureIndexSizes(int colCount, int rowCount)
    • getCC

      private static CC getCC(ComponentWrapper comp, Map<? extends ComponentWrapper,CC> ccMap)
    • addLinkIDs

      private void addLinkIDs(CC cc)
    • invalidateContainerSize

      public void invalidateContainerSize()
      If the container (parent) that this grid is laying out has changed its bounds, call this method to clear any cached values min/pref/max sizes of the components and rows/columns.

      If any component can have changed cell the grid needs to be recreated.

    • invalidateComponentSizes

      private void invalidateComponentSizes()
    • layout

      public boolean layout(int[] bounds, UnitValue alignX, UnitValue alignY, boolean debug, boolean notUsed)
      Deprecated.
      since 5.0 Last boolean is not needed and is gotten from the new ComponentWrapper.getContentBias() instead;
    • layout

      public boolean layout(int[] bounds, UnitValue alignX, UnitValue alignY, boolean debug)
      Does the actual layout. Uses many values calculated in the constructor.
      Parameters:
      bounds - The bounds to layout against. Normally that of the parent. [x, y, width, height].
      alignX - The alignment for the x-axis. Can be null.
      alignY - The alignment for the y-axis. Can be null.
      debug - If debug information should be saved in debugRects.
      Returns:
      If the layout has changed the preferred size and there is need for a new layout. This can happen if one or more components in the grid has a content bias according to ComponentWrapper.getContentBias().
      Since:
      5.0
    • layoutImpl

      private boolean layoutImpl(int[] bounds, UnitValue alignX, UnitValue alignY, boolean debug, boolean trialRun)
      Does the actual layout. Uses many values calculated in the constructor.
      Parameters:
      bounds - The bounds to layout against. Normally that of the parent. [x, y, width, height].
      alignX - The alignment for the x-axis. Can be null.
      alignY - The alignment for the y-axis. Can be null.
      debug - If debug information should be saved in debugRects.
      trialRun - If true the bounds calculated will not be transferred to the components. Only the internal size of the components will be calculated.
      Returns:
      If the layout has changed the preferred size and there is need for a new layout. This can happen if one or more components in the grid has a content bias according to ComponentWrapper.getContentBias().
      Since:
      5.0
    • paintDebug

      public void paintDebug()
    • getContainer

      public ContainerWrapper getContainer()
    • getWidth

      public final int[] getWidth()
    • getWidth

      public final int[] getWidth(int refHeight)
    • getHeight

      public final int[] getHeight()
    • getHeight

      public final int[] getHeight(int refWidth)
    • checkSizeCalcs

      private void checkSizeCalcs(int refWidth, int refHeight)
    • calcGridSizes

      private void calcGridSizes(int refWidth, int refHeight)
    • getPos

      private UnitValue[] getPos(ComponentWrapper cw, CC cc)
    • getCallbackSize

      private BoundSize[] getCallbackSize(ComponentWrapper cw)
    • getDockInsets

      private static int getDockInsets(TreeSet<Integer> set)
    • setLinkedBounds

      private boolean setLinkedBounds(ComponentWrapper cw, CC cc, int x, int y, int w, int h, boolean external)
      Parameters:
      cw - Never null.
      cc - Never null.
      external - The bounds should be stored even if they are not in linkTargetIDs.
      Returns:
      If a change has been made.
    • increase

      private int increase(int[] p, int cnt)
      Go to next cell.
      Parameters:
      p - The point to increase
      cnt - How many cells to advance.
      Returns:
      The new value in the "increasing" dimension.
    • wrap

      private void wrap(int[] cellXY, BoundSize gapSize)
      Wraps to the next row or column depending on if horizontal flow or vertical flow is used.
      Parameters:
      cellXY - The point to wrap and thus set either x or y to 0 and increase the other one.
      gapSize - The gaps size specified in a "wrap XXX" or "newline XXX" or null if none.
    • sortCellsByPlatform

      private static void sortCellsByPlatform(Collection<Grid.Cell> cells, ContainerWrapper parent)
      Sort components (normally buttons in a button bar) so they appear in the correct order.
      Parameters:
      cells - The cells to sort.
      parent - The parent.
    • getDefaultPushWeights

      private Float[] getDefaultPushWeights(boolean isRows)
    • clearGroupLinkBounds

      private void clearGroupLinkBounds()
    • resetLinkValues

      private void resetLinkValues(boolean parentSize, boolean compLinks)
    • getGroupContaining

      private static Grid.LinkedDimGroup getGroupContaining(ArrayList<Grid.LinkedDimGroup>[] groupLists, Grid.CompWrap cw)
      Returns the Grid.LinkedDimGroup that has the Grid.CompWrap cw.
      Parameters:
      groupLists - The lists to search in.
      cw - The component wrap to find.
      Returns:
      The linked group or null if none had the component wrap.
    • doAbsoluteCorrections

      private boolean doAbsoluteCorrections(Grid.CompWrap cw, int[] bounds)
    • adjustSizeForAbsolute

      private void adjustSizeForAbsolute(boolean isHor)
      Adjust grid's width or height for the absolute components' positions.
    • getAbsoluteDimBounds

      private int[] getAbsoluteDimBounds(Grid.CompWrap cw, int refSize, boolean isHor)
    • layoutInOneDim

      private void layoutInOneDim(int refSize, UnitValue align, boolean isRows, Float[] defaultPushWeights)
    • addToSizeGroup

      private static void addToSizeGroup(HashMap<String,int[]> sizeGroups, String sizeGroup, int[] size)
    • addToEndGroup

      private static HashMap<String,Integer> addToEndGroup(HashMap<String,Integer> endGroups, String endGroup, int end)
    • calcRowsOrColsSizes

      private Grid.FlowSizeSpec calcRowsOrColsSizes(boolean isHor, int containerSize)
      Calculates Min, Preferred and Max size for the columns OR rows.
      Parameters:
      isHor - If it is the horizontal dimension to calculate.
      containerSize - The reference container size in the dimension. If invalid input: '<'= 0 it will be replaced by the actual container's size.
      Returns:
      The sizes in a Grid.FlowSizeSpec.
    • getParentSize

      private static int getParentSize(ComponentWrapper cw, boolean isHor)
    • getMinPrefMaxSumSize

      private int[] getMinPrefMaxSumSize(boolean isHor, int[][] sizes)
    • getRowResizeConstraints

      private static ResizeConstraint[] getRowResizeConstraints(DimConstraint[] specs)
    • getComponentResizeConstraints

      private static ResizeConstraint[] getComponentResizeConstraints(ArrayList<Grid.CompWrap> compWraps, boolean isHor)
    • getComponentGapPush

      private static boolean[] getComponentGapPush(ArrayList<Grid.CompWrap> compWraps, boolean isHor)
    • getRowGaps

      private int[][] getRowGaps(DimConstraint[] specs, int refSize, boolean isHor, boolean[] fillInPushGaps)
      Returns the row gaps in pixel sizes. One more than there are specs sent in.
      Parameters:
      specs -
      refSize -
      isHor -
      fillInPushGaps - If the gaps are pushing. NOTE! this argument will be filled in and thus changed!
      Returns:
      The row gaps in pixel sizes. One more than there are specs sent in.
    • getGaps

      private static int[][] getGaps(ArrayList<Grid.CompWrap> compWraps, boolean isHor)
    • hasDocks

      private boolean hasDocks()
    • adjustMinPrefForSpanningComps

      private void adjustMinPrefForSpanningComps(DimConstraint[] specs, Float[] defPush, Grid.FlowSizeSpec fss, ArrayList<Grid.LinkedDimGroup>[] groupsLists)
      Adjust min/pref size for columns(or rows) that has components that spans multiple columns (or rows).
      Parameters:
      specs - The specs for the columns or rows. Last index will be used if count is greater than this array's length.
      defPush - The default grow weight if the specs does not have anyone that will grow. Comes from "push" in the CC.
      fss -
      groupsLists -
    • divideIntoLinkedGroups

      private ArrayList<Grid.LinkedDimGroup>[] divideIntoLinkedGroups(boolean isRows)
      For one dimension divide the component wraps into logical groups. One group for component wraps that share a common something, line the property to layout by base line.
      Parameters:
      isRows - If rows, and not columns, are to be divided.
      Returns:
      One ArrayList for every row/column.
    • convertSpanToSparseGrid

      private static int convertSpanToSparseGrid(int curIx, int span, TreeSet<Integer> indexes)
      Spanning is specified in the uncompressed grid number. They can for instance be more than 60000 for the outer edge dock grid cells. When the grid is compressed and indexed after only the cells that area occupied the span is erratic. This method use the row/col indexes and corrects the span to be correct for the compressed grid.
      Parameters:
      span - The span in the uncompressed grid. LayoutUtil.INF will be interpreted to span the rest of the column/row excluding the surrounding docking components.
      indexes - The indexes in the correct dimension.
      Returns:
      The converted span.
    • isCellFree

      private boolean isCellFree(int r, int c, ArrayList<int[]> occupiedRects)
    • getCell

      private Grid.Cell getCell(int r, int c)
    • setCell

      private void setCell(int r, int c, Grid.Cell cell)
    • addDockingCell

      private void addDockingCell(int[] dockInsets, int side, Grid.CompWrap cw)
      Adds a docking cell. That cell is outside the normal cell indexes.
      Parameters:
      dockInsets - The current dock insets. Will be updated!
      side - top == 0, left == 1, bottom = 2, right = 3.
      cw - The compwrap to put in a cell and add.
    • layoutBaseline

      private static void layoutBaseline(ContainerWrapper parent, ArrayList<Grid.CompWrap> compWraps, DimConstraint dc, int start, int size, int sizeType, int spanCount)
    • layoutSerial

      private static void layoutSerial(ContainerWrapper parent, ArrayList<Grid.CompWrap> compWraps, DimConstraint dc, int start, int size, boolean isHor, int spanCount, boolean fromEnd)
    • setCompWrapBounds

      private static void setCompWrapBounds(ContainerWrapper parent, int[] allSizes, ArrayList<Grid.CompWrap> compWraps, UnitValue rowAlign, int start, int size, boolean isHor, boolean fromEnd)
    • layoutParallel

      private static void layoutParallel(ContainerWrapper parent, ArrayList<Grid.CompWrap> compWraps, DimConstraint dc, int start, int size, boolean isHor, boolean fromEnd)
    • setCompWrapBounds

      private static void setCompWrapBounds(ContainerWrapper parent, int[][] sizes, ArrayList<Grid.CompWrap> compWraps, UnitValue rowAlign, int start, int size, boolean isHor, boolean fromEnd)
    • correctAlign

      private static UnitValue correctAlign(CC cc, UnitValue rowAlign, boolean isHor, boolean fromEnd)
    • getBaselineAboveBelow

      private static Grid.AboveBelow getBaselineAboveBelow(ArrayList<Grid.CompWrap> compWraps, int sType, boolean centerBaseline)
    • getTotalSizeParallel

      private static int getTotalSizeParallel(ArrayList<Grid.CompWrap> compWraps, int sType, boolean isHor)
    • getTotalSizeSerial

      private static int getTotalSizeSerial(ArrayList<Grid.CompWrap> compWraps, int sType, boolean isHor)
    • getTotalGroupsSizeParallel

      private static int getTotalGroupsSizeParallel(ArrayList<Grid.LinkedDimGroup> groups, int sType, boolean countSpanning)
    • getComponentSizes

      private static int[][] getComponentSizes(ArrayList<Grid.CompWrap> compWraps, boolean isHor)
      Parameters:
      compWraps -
      isHor -
      Returns:
      Might contain LayoutUtil.NOT_SET
    • mergeSizesGapsAndResConstrs

      private static Grid.FlowSizeSpec mergeSizesGapsAndResConstrs(ResizeConstraint[] resConstr, boolean[] gapPush, int[][] minPrefMaxSizes, int[][] gapSizes)
      Merges sizes and gaps together with Resize Constraints. For gaps GAP_RC_CONST is used.
      Parameters:
      resConstr - One resize constraint for every row/component. Can be lesser in length and the last element should be used for missing elements.
      gapPush - If the corresponding gap should be considered pushing and thus want to take free space if left over. Should be one more than resConstrs!
      minPrefMaxSizes - The sizes (min/pref/max) for every row/component.
      gapSizes - The gaps before and after each row/component packed in one double sized array.
      Returns:
      A holder for the merged values.
    • mergeSizes

      private static int[] mergeSizes(int[] oldValues, int[] newValues)
    • mergeSizes

      private static int mergeSizes(int oldValue, int newValue, boolean toMax)
    • constrainSize

      private static int constrainSize(int s)
    • correctMinMax

      private static void correctMinMax(int[] s)
    • extractSubArray

      private static Float[] extractSubArray(DimConstraint[] specs, Float[] arr, int ix, int len)
    • putSizesAndIndexes

      private static void putSizesAndIndexes(Object parComp, int[] sizes, int[] ixArr, boolean isRows)
    • getSizesAndIndexes

      static int[][] getSizesAndIndexes(Object parComp, boolean isRows)
    • saveGrid

      private static void saveGrid(ComponentWrapper parComp, LinkedHashMap<Integer,Grid.Cell> grid)
    • getGridPositions

      static HashMap<Object,int[]> getGridPositions(Object parComp)