Class Grid


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

      • GROW_100

        private static final java.lang.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:
        Constant Field Values
      • 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:
        Constant Field Values
      • GAP_RC_CONST

        private static final ResizeConstraint GAP_RC_CONST
        A constraint used for gaps.
      • 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 java.util.LinkedHashMap<java.lang.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 java.util.HashMap<java.lang.Integer,​BoundSize> wrapGapMap
      • rowIndexes

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

        private final java.util.TreeSet<java.lang.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 java.util.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 java.util.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.
      • linkTargetIDs

        private java.util.HashMap<java.lang.String,​java.lang.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 java.lang.Float[] pushXs
      • pushYs

        private final java.lang.Float[] pushYs
      • callbackList

        private final java.util.ArrayList<LayoutCallback> callbackList
      • lastRefWidth

        private int lastRefWidth
      • lastRefHeight

        private int lastRefHeight
      • PARENT_ROWCOL_SIZES_MAP

        private static java.util.WeakHashMap<java.lang.Object,​int[][]>[] PARENT_ROWCOL_SIZES_MAP
      • PARENT_GRIDPOS_MAP

        private static java.util.WeakHashMap<java.lang.Object,​java.util.ArrayList<Grid.WeakCell>> PARENT_GRIDPOS_MAP
    • Constructor Detail

      • Grid

        public Grid​(ContainerWrapper container,
                    LC lc,
                    AC rowConstr,
                    AC colConstr,
                    java.util.Map<? extends ComponentWrapper,​CC> ccMap,
                    java.util.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 Detail

      • ensureIndexSizes

        private void ensureIndexSizes​(int colCount,
                                      int rowCount)
      • 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)
        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()
      • 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)
      • getDockInsets

        private static int getDockInsets​(java.util.TreeSet<java.lang.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​(java.util.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 java.lang.Float[] getDefaultPushWeights​(boolean isRows)
      • clearGroupLinkBounds

        private void clearGroupLinkBounds()
      • resetLinkValues

        private void resetLinkValues​(boolean parentSize,
                                     boolean compLinks)
      • 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,
                                    java.lang.Float[] defaultPushWeights)
      • addToSizeGroup

        private static void addToSizeGroup​(java.util.HashMap<java.lang.String,​int[]> sizeGroups,
                                           java.lang.String sizeGroup,
                                           int[] size)
      • addToEndGroup

        private static java.util.HashMap<java.lang.String,​java.lang.Integer> addToEndGroup​(java.util.HashMap<java.lang.String,​java.lang.Integer> endGroups,
                                                                                                 java.lang.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 <= 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)
      • getComponentResizeConstraints

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

        private static boolean[] getComponentGapPush​(java.util.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​(java.util.ArrayList<Grid.CompWrap> compWraps,
                                       boolean isHor)
      • hasDocks

        private boolean hasDocks()
      • adjustMinPrefForSpanningComps

        private void adjustMinPrefForSpanningComps​(DimConstraint[] specs,
                                                   java.lang.Float[] defPush,
                                                   Grid.FlowSizeSpec fss,
                                                   java.util.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 java.util.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,
                                                   java.util.TreeSet<java.lang.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,
                                   java.util.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.
      • layoutSerial

        private static void layoutSerial​(ContainerWrapper parent,
                                         java.util.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,
                                              java.util.ArrayList<Grid.CompWrap> compWraps,
                                              UnitValue rowAlign,
                                              int start,
                                              int size,
                                              boolean isHor,
                                              boolean fromEnd)
      • setCompWrapBounds

        private static void setCompWrapBounds​(ContainerWrapper parent,
                                              int[][] sizes,
                                              java.util.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​(java.util.ArrayList<Grid.CompWrap> compWraps,
                                                             int sType,
                                                             boolean centerBaseline)
      • getTotalSizeParallel

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

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

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

        private static int[][] getComponentSizes​(java.util.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 java.lang.Float[] extractSubArray​(DimConstraint[] specs,
                                                         java.lang.Float[] arr,
                                                         int ix,
                                                         int len)
      • putSizesAndIndexes

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

        static int[][] getSizesAndIndexes​(java.lang.Object parComp,
                                          boolean isRows)
      • saveGrid

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

        static java.util.HashMap<java.lang.Object,​int[]> getGridPositions​(java.lang.Object parComp)