public void addLayoutComponent(Component comp, Object constraints) { GridConstraints c = (GridConstraints) constraints; int row = c.getRow(); int rowSpan = c.getRowSpan(); int rowCount = this.getRowCount(); if (row >= 0 && row < rowCount) { if (row + rowSpan - 1 >= rowCount) { throw new IllegalArgumentException( "wrong row span: " + rowSpan + "; row=" + row + " rowCount=" + rowCount); } else { int column = c.getColumn(); int colSpan = c.getColSpan(); int columnCount = this.getColumnCount(); if (column >= 0 && column < columnCount) { if (column + colSpan - 1 >= columnCount) { throw new IllegalArgumentException( "wrong col span: " + colSpan + "; column=" + column + " columnCount=" + columnCount); } else { super.addLayoutComponent(comp, constraints); } } else { throw new IllegalArgumentException("wrong column: " + column); } } } else { throw new IllegalArgumentException("wrong row: " + row); } }
private static void updateSizesFromChildren(DimensionInfo info, boolean min, int[] widths) { for (int i = info.getComponentCount() - 1; i >= 0; --i) { Component child = info.getComponent(i); GridConstraints c = info.getConstraints(i); if (c.isUseParentLayout() && child instanceof Container) { Container container = (Container) child; if (container.getLayout() instanceof GridLayoutManager) { updateSizesFromChild(info, min, widths, container, i); } else if (container.getComponentCount() == 1 && container.getComponent(0) instanceof Container) { Container childContainer = (Container) container.getComponent(0); if (childContainer.getLayout() instanceof GridLayoutManager) { updateSizesFromChild(info, min, widths, childContainer, i); } } } } }
private int checkSetSizesFromParent(Container container, Insets insets) { int skipLayout = 0; GridLayoutManager parentGridLayout = null; GridConstraints parentGridConstraints = null; Container parent = container.getParent(); if (parent != null) { if (parent.getLayout() instanceof GridLayoutManager) { parentGridLayout = (GridLayoutManager) parent.getLayout(); parentGridConstraints = parentGridLayout.getConstraintsForComponent(container); } else { Container col = parent.getParent(); if (col != null && col.getLayout() instanceof GridLayoutManager) { parentGridLayout = (GridLayoutManager) col.getLayout(); parentGridConstraints = parentGridLayout.getConstraintsForComponent(parent); } } } if (parentGridLayout != null && parentGridConstraints.isUseParentLayout()) { int i; if (this.myRowStretches.length == parentGridConstraints.getRowSpan()) { int row = parentGridConstraints.getRow(); this.myYs[0] = insets.top + this.myMargin.top; this.myHeights[0] = parentGridLayout.myHeights[row] - this.myYs[0]; for (i = 1; i < this.myRowStretches.length; ++i) { this.myYs[i] = parentGridLayout.myYs[i + row] - parentGridLayout.myYs[row]; this.myHeights[i] = parentGridLayout.myHeights[i + row]; } this.myHeights[this.myRowStretches.length - 1] -= insets.bottom + this.myMargin.bottom; skipLayout |= 1; } if (this.myColumnStretches.length == parentGridConstraints.getColSpan()) { int column = parentGridConstraints.getColumn(); this.myXs[0] = insets.left + this.myMargin.left; this.myWidths[0] = parentGridLayout.myWidths[column] - this.myXs[0]; for (i = 1; i < this.myColumnStretches.length; ++i) { this.myXs[i] = parentGridLayout.myXs[i + column] - parentGridLayout.myXs[column]; this.myWidths[i] = parentGridLayout.myWidths[i + column]; } this.myWidths[this.myColumnStretches.length - 1] -= insets.right + this.myMargin.right; skipLayout |= 2; } } return skipLayout; }
public void layoutContainer(Container container) { this.validateInfos(container); LayoutState layoutState = this.myLayoutState; DimensionInfo horizontalInfo = this.myHorizontalInfo; DimensionInfo verticalInfo = this.myVerticalInfo; Insets insets = getInsets(container); int skipLayout = this.checkSetSizesFromParent(container, insets); Dimension gap = this.getTotalGap(container, horizontalInfo, verticalInfo); Dimension size = container.getSize(); size.width -= gap.width; size.height -= gap.height; Dimension prefSize = this.preferredLayoutSize(container); prefSize.width -= gap.width; prefSize.height -= gap.height; Dimension minSize = this.minimumLayoutSize(container); minSize.width -= gap.width; minSize.height -= gap.height; int[] i; int c; int component; if ((skipLayout & 1) == 0) { if (this.mySameSizeVertically) { i = getSameSizes(verticalInfo, Math.max(size.height, minSize.height)); } else if (size.height < prefSize.height) { i = this.getMinSizes(verticalInfo); this.new_doIt(i, 0, verticalInfo.getCellCount(), size.height, verticalInfo, true); } else { i = this.getPrefSizes(verticalInfo); this.new_doIt(i, 0, verticalInfo.getCellCount(), size.height, verticalInfo, false); } c = insets.top + this.myMargin.top; for (component = 0; component < i.length; ++component) { this.myYs[component] = c; this.myHeights[component] = i[component]; c += i[component]; if (shouldAddGapAfterCell(verticalInfo, component)) { c += verticalInfo.getGap(); } } } if ((skipLayout & 2) == 0) { if (this.mySameSizeHorizontally) { i = getSameSizes(horizontalInfo, Math.max(size.width, minSize.width)); } else if (size.width < prefSize.width) { i = this.getMinSizes(horizontalInfo); this.new_doIt(i, 0, horizontalInfo.getCellCount(), size.width, horizontalInfo, true); } else { i = this.getPrefSizes(horizontalInfo); this.new_doIt(i, 0, horizontalInfo.getCellCount(), size.width, horizontalInfo, false); } c = insets.left + this.myMargin.left; for (component = 0; component < i.length; ++component) { this.myXs[component] = c; this.myWidths[component] = i[component]; c += i[component]; if (shouldAddGapAfterCell(horizontalInfo, component)) { c += horizontalInfo.getGap(); } } } for (int index = 0; index < layoutState.getComponentCount(); ++index) { GridConstraints constraints = layoutState.getConstraints(index); Component cmpnt = layoutState.getComponent(index); int column = horizontalInfo.getCell(index); int colSpan = horizontalInfo.getSpan(index); int row = verticalInfo.getCell(index); int rowSpan = verticalInfo.getSpan(index); int cellWidth = this.myXs[column + colSpan - 1] + this.myWidths[column + colSpan - 1] - this.myXs[column]; int cellHeight = this.myYs[row + rowSpan - 1] + this.myHeights[row + rowSpan - 1] - this.myYs[row]; Dimension componentSize = new Dimension(cellWidth, cellHeight); if ((constraints.getFill() & 1) == 0) { componentSize.width = Math.min(componentSize.width, horizontalInfo.getPreferredWidth(index)); } if ((constraints.getFill() & 2) == 0) { componentSize.height = Math.min(componentSize.height, verticalInfo.getPreferredWidth(index)); } Util.adjustSize(cmpnt, constraints, componentSize); int dx = 0; int dy = 0; if ((constraints.getAnchor() & 4) != 0) { dx = cellWidth - componentSize.width; } else if ((constraints.getAnchor() & 8) == 0) { dx = (cellWidth - componentSize.width) / 2; } if ((constraints.getAnchor() & 2) != 0) { dy = cellHeight - componentSize.height; } else if ((constraints.getAnchor() & 1) == 0) { dy = (cellHeight - componentSize.height) / 2; } int indent = 10 * constraints.getIndent(); componentSize.width -= indent; dx += indent; cmpnt.setBounds( this.myXs[column] + dx, this.myYs[row] + dy, componentSize.width, componentSize.height); } }