List of usage examples for java.awt Container getTreeLock
public final Object getTreeLock()
From source file:FormatLayout.java
/** * Returns the preferred size of the component using this layout manager. * * @param parent the parent./*from ww w . j av a 2s.c o m*/ * * @return the preferred size of the component. */ public Dimension preferredLayoutSize(final Container parent) { Component c0, c1, c2, c3, c4, c5; synchronized (parent.getTreeLock()) { final Insets insets = parent.getInsets(); int componentIndex = 0; final int rowCount = this.rowHeights.length; for (int i = 0; i < this.columnWidths.length; i++) { this.columnWidths[i] = 0; } this.columns1and2Width = 0; this.columns4and5Width = 0; this.columns1to4Width = 0; this.columns1to5Width = 0; this.columns0to5Width = 0; this.totalHeight = 0; for (int rowIndex = 0; rowIndex < rowCount; rowIndex++) { final int format = this.rowFormats[rowIndex % this.rowFormats.length]; switch (format) { case FormatLayout.C: c0 = parent.getComponent(componentIndex); updateC(rowIndex, c0.getPreferredSize()); componentIndex = componentIndex + 1; break; case FormatLayout.LC: c0 = parent.getComponent(componentIndex); c1 = parent.getComponent(componentIndex + 1); updateLC(rowIndex, c0.getPreferredSize(), c1.getPreferredSize()); componentIndex = componentIndex + 2; break; case FormatLayout.LCB: c0 = parent.getComponent(componentIndex); c1 = parent.getComponent(componentIndex + 1); c2 = parent.getComponent(componentIndex + 2); updateLCB(rowIndex, c0.getPreferredSize(), c1.getPreferredSize(), c2.getPreferredSize()); componentIndex = componentIndex + 3; break; case FormatLayout.LCLC: c0 = parent.getComponent(componentIndex); c1 = parent.getComponent(componentIndex + 1); c2 = parent.getComponent(componentIndex + 2); c3 = parent.getComponent(componentIndex + 3); updateLCLC(rowIndex, c0.getPreferredSize(), c1.getPreferredSize(), c2.getPreferredSize(), c3.getPreferredSize()); componentIndex = componentIndex + 4; break; case FormatLayout.LCBLC: c0 = parent.getComponent(componentIndex); c1 = parent.getComponent(componentIndex + 1); c2 = parent.getComponent(componentIndex + 2); c3 = parent.getComponent(componentIndex + 3); c4 = parent.getComponent(componentIndex + 4); updateLCBLC(rowIndex, c0.getPreferredSize(), c1.getPreferredSize(), c2.getPreferredSize(), c3.getPreferredSize(), c4.getPreferredSize()); componentIndex = componentIndex + 5; break; case FormatLayout.LCLCB: c0 = parent.getComponent(componentIndex); c1 = parent.getComponent(componentIndex + 1); c2 = parent.getComponent(componentIndex + 2); c3 = parent.getComponent(componentIndex + 3); c4 = parent.getComponent(componentIndex + 4); updateLCLCB(rowIndex, c0.getPreferredSize(), c1.getPreferredSize(), c2.getPreferredSize(), c3.getPreferredSize(), c4.getPreferredSize()); componentIndex = componentIndex + 5; break; case FormatLayout.LCBLCB: c0 = parent.getComponent(componentIndex); c1 = parent.getComponent(componentIndex + 1); c2 = parent.getComponent(componentIndex + 2); c3 = parent.getComponent(componentIndex + 3); c4 = parent.getComponent(componentIndex + 4); c5 = parent.getComponent(componentIndex + 5); updateLCBLCB(rowIndex, c0.getPreferredSize(), c1.getPreferredSize(), c2.getPreferredSize(), c3.getPreferredSize(), c4.getPreferredSize(), c5.getPreferredSize()); componentIndex = componentIndex + 6; break; } } complete(); return new Dimension(this.totalWidth + insets.left + insets.right, this.totalHeight + (rowCount - 1) * this.rowGap + insets.top + insets.bottom); } }
From source file:FormatLayout.java
/** * Returns the minimum size of the component using this layout manager. * * @param parent the parent.//from w w w . ja va 2 s.c o m * * @return the minimum size of the component */ public Dimension minimumLayoutSize(final Container parent) { Component c0, c1, c2, c3, c4, c5; synchronized (parent.getTreeLock()) { final Insets insets = parent.getInsets(); int componentIndex = 0; final int rowCount = this.rowHeights.length; for (int i = 0; i < this.columnWidths.length; i++) { this.columnWidths[i] = 0; } this.columns1and2Width = 0; this.columns4and5Width = 0; this.columns1to4Width = 0; this.columns1to5Width = 0; this.columns0to5Width = 0; final int totalHeight = 0; for (int rowIndex = 0; rowIndex < rowCount; rowIndex++) { final int format = this.rowFormats[rowIndex % this.rowFormats.length]; switch (format) { case FormatLayout.C: c0 = parent.getComponent(componentIndex); this.columns0to5Width = Math.max(this.columns0to5Width, c0.getMinimumSize().width); componentIndex = componentIndex + 1; break; case FormatLayout.LC: c0 = parent.getComponent(componentIndex); c1 = parent.getComponent(componentIndex + 1); updateLC(rowIndex, c0.getMinimumSize(), c1.getMinimumSize()); componentIndex = componentIndex + 2; break; case FormatLayout.LCB: c0 = parent.getComponent(componentIndex); c1 = parent.getComponent(componentIndex + 1); c2 = parent.getComponent(componentIndex + 2); updateLCB(rowIndex, c0.getMinimumSize(), c1.getMinimumSize(), c2.getMinimumSize()); componentIndex = componentIndex + 3; break; case FormatLayout.LCLC: c0 = parent.getComponent(componentIndex); c1 = parent.getComponent(componentIndex + 1); c2 = parent.getComponent(componentIndex + 2); c3 = parent.getComponent(componentIndex + 3); updateLCLC(rowIndex, c0.getMinimumSize(), c1.getMinimumSize(), c2.getMinimumSize(), c3.getMinimumSize()); componentIndex = componentIndex + 3; break; case FormatLayout.LCBLC: c0 = parent.getComponent(componentIndex); c1 = parent.getComponent(componentIndex + 1); c2 = parent.getComponent(componentIndex + 2); c3 = parent.getComponent(componentIndex + 3); c4 = parent.getComponent(componentIndex + 4); updateLCBLC(rowIndex, c0.getMinimumSize(), c1.getMinimumSize(), c2.getMinimumSize(), c3.getMinimumSize(), c4.getMinimumSize()); componentIndex = componentIndex + 4; break; case FormatLayout.LCLCB: c0 = parent.getComponent(componentIndex); c1 = parent.getComponent(componentIndex + 1); c2 = parent.getComponent(componentIndex + 2); c3 = parent.getComponent(componentIndex + 3); c4 = parent.getComponent(componentIndex + 4); updateLCLCB(rowIndex, c0.getMinimumSize(), c1.getMinimumSize(), c2.getMinimumSize(), c3.getMinimumSize(), c4.getMinimumSize()); componentIndex = componentIndex + 4; break; case FormatLayout.LCBLCB: c0 = parent.getComponent(componentIndex); c1 = parent.getComponent(componentIndex + 1); c2 = parent.getComponent(componentIndex + 2); c3 = parent.getComponent(componentIndex + 3); c4 = parent.getComponent(componentIndex + 4); c5 = parent.getComponent(componentIndex + 5); updateLCBLCB(rowIndex, c0.getMinimumSize(), c1.getMinimumSize(), c2.getMinimumSize(), c3.getMinimumSize(), c4.getMinimumSize(), c5.getMinimumSize()); componentIndex = componentIndex + 5; break; } } complete(); return new Dimension(this.totalWidth + insets.left + insets.right, totalHeight + (rowCount - 1) * this.rowGap + insets.top + insets.bottom); } }
From source file:FormatLayout.java
/** * Performs the layout of the container. * * @param parent the parent.//from ww w. j ava 2s . c o m */ public void layoutContainer(final Container parent) { Component c0, c1, c2, c3, c4, c5; synchronized (parent.getTreeLock()) { final Insets insets = parent.getInsets(); int componentIndex = 0; final int rowCount = this.rowHeights.length; for (int i = 0; i < this.columnWidths.length; i++) { this.columnWidths[i] = 0; } this.columns1and2Width = 0; this.columns4and5Width = 0; this.columns1to4Width = 0; this.columns1to5Width = 0; this.columns0to5Width = parent.getBounds().width - insets.left - insets.right; this.totalHeight = 0; for (int rowIndex = 0; rowIndex < rowCount; rowIndex++) { final int format = this.rowFormats[rowIndex % this.rowFormats.length]; switch (format) { case FormatLayout.C: c0 = parent.getComponent(componentIndex); updateC(rowIndex, c0.getPreferredSize()); componentIndex = componentIndex + 1; break; case FormatLayout.LC: c0 = parent.getComponent(componentIndex); c1 = parent.getComponent(componentIndex + 1); updateLC(rowIndex, c0.getPreferredSize(), c1.getPreferredSize()); componentIndex = componentIndex + 2; break; case FormatLayout.LCB: c0 = parent.getComponent(componentIndex); c1 = parent.getComponent(componentIndex + 1); c2 = parent.getComponent(componentIndex + 2); updateLCB(rowIndex, c0.getPreferredSize(), c1.getPreferredSize(), c2.getPreferredSize()); componentIndex = componentIndex + 3; break; case FormatLayout.LCLC: c0 = parent.getComponent(componentIndex); c1 = parent.getComponent(componentIndex + 1); c2 = parent.getComponent(componentIndex + 2); c3 = parent.getComponent(componentIndex + 3); updateLCLC(rowIndex, c0.getPreferredSize(), c1.getPreferredSize(), c2.getPreferredSize(), c3.getPreferredSize()); componentIndex = componentIndex + 4; break; case FormatLayout.LCBLC: c0 = parent.getComponent(componentIndex); c1 = parent.getComponent(componentIndex + 1); c2 = parent.getComponent(componentIndex + 2); c3 = parent.getComponent(componentIndex + 3); c4 = parent.getComponent(componentIndex + 4); updateLCBLC(rowIndex, c0.getPreferredSize(), c1.getPreferredSize(), c2.getPreferredSize(), c3.getPreferredSize(), c4.getPreferredSize()); componentIndex = componentIndex + 5; break; case FormatLayout.LCLCB: c0 = parent.getComponent(componentIndex); c1 = parent.getComponent(componentIndex + 1); c2 = parent.getComponent(componentIndex + 2); c3 = parent.getComponent(componentIndex + 3); c4 = parent.getComponent(componentIndex + 4); updateLCLCB(rowIndex, c0.getPreferredSize(), c1.getPreferredSize(), c2.getPreferredSize(), c3.getPreferredSize(), c4.getPreferredSize()); componentIndex = componentIndex + 5; break; case FormatLayout.LCBLCB: c0 = parent.getComponent(componentIndex); c1 = parent.getComponent(componentIndex + 1); c2 = parent.getComponent(componentIndex + 2); c3 = parent.getComponent(componentIndex + 3); c4 = parent.getComponent(componentIndex + 4); c5 = parent.getComponent(componentIndex + 5); updateLCBLCB(rowIndex, c0.getPreferredSize(), c1.getPreferredSize(), c2.getPreferredSize(), c3.getPreferredSize(), c4.getPreferredSize(), c5.getPreferredSize()); componentIndex = componentIndex + 6; break; } } complete(); componentIndex = 0; int rowY = insets.top; final int[] rowX = new int[6]; rowX[0] = insets.left; rowX[1] = rowX[0] + this.columnWidths[0] + this.columnGaps[0]; rowX[2] = rowX[1] + this.columnWidths[1] + this.columnGaps[1]; rowX[3] = rowX[2] + this.columnWidths[2] + this.columnGaps[2]; rowX[4] = rowX[3] + this.columnWidths[3] + this.columnGaps[3]; rowX[5] = rowX[4] + this.columnWidths[4] + this.columnGaps[4]; final int w1to2 = this.columnWidths[1] + this.columnGaps[1] + this.columnWidths[2]; final int w4to5 = this.columnWidths[4] + this.columnGaps[4] + this.columnWidths[5]; final int w1to4 = w1to2 + this.columnGaps[2] + this.columnWidths[3] + this.columnGaps[3] + this.columnWidths[4]; final int w1to5 = w1to4 + this.columnGaps[4] + this.columnWidths[5]; final int w0to5 = w1to5 + this.columnWidths[0] + this.columnGaps[0]; for (int rowIndex = 0; rowIndex < rowCount; rowIndex++) { final int format = this.rowFormats[rowIndex % this.rowFormats.length]; switch (format) { case FormatLayout.C: c0 = parent.getComponent(componentIndex); c0.setBounds(rowX[0], rowY, w0to5, c0.getPreferredSize().height); componentIndex = componentIndex + 1; break; case FormatLayout.LC: c0 = parent.getComponent(componentIndex); c0.setBounds(rowX[0], rowY + (this.rowHeights[rowIndex] - c0.getPreferredSize().height) / 2, this.columnWidths[0], c0.getPreferredSize().height); c1 = parent.getComponent(componentIndex + 1); c1.setBounds(rowX[1], rowY + (this.rowHeights[rowIndex] - c1.getPreferredSize().height) / 2, w1to5, c1.getPreferredSize().height); componentIndex = componentIndex + 2; break; case FormatLayout.LCB: c0 = parent.getComponent(componentIndex); c0.setBounds(rowX[0], rowY + (this.rowHeights[rowIndex] - c0.getPreferredSize().height) / 2, this.columnWidths[0], c0.getPreferredSize().height); c1 = parent.getComponent(componentIndex + 1); c1.setBounds(rowX[1], rowY + (this.rowHeights[rowIndex] - c1.getPreferredSize().height) / 2, w1to4, c1.getPreferredSize().height); c2 = parent.getComponent(componentIndex + 2); c2.setBounds(rowX[5], rowY + (this.rowHeights[rowIndex] - c2.getPreferredSize().height) / 2, this.columnWidths[5], c2.getPreferredSize().height); componentIndex = componentIndex + 3; break; case FormatLayout.LCLC: c0 = parent.getComponent(componentIndex); c0.setBounds(rowX[0], rowY + (this.rowHeights[rowIndex] - c0.getPreferredSize().height) / 2, this.columnWidths[0], c0.getPreferredSize().height); c1 = parent.getComponent(componentIndex + 1); c1.setBounds(rowX[1], rowY + (this.rowHeights[rowIndex] - c1.getPreferredSize().height) / 2, w1to2, c1.getPreferredSize().height); c2 = parent.getComponent(componentIndex + 2); c2.setBounds(rowX[3], rowY + (this.rowHeights[rowIndex] - c2.getPreferredSize().height) / 2, this.columnWidths[3], c2.getPreferredSize().height); c3 = parent.getComponent(componentIndex + 3); c3.setBounds(rowX[4], rowY + (this.rowHeights[rowIndex] - c3.getPreferredSize().height) / 2, w4to5, c3.getPreferredSize().height); componentIndex = componentIndex + 4; break; case FormatLayout.LCBLC: c0 = parent.getComponent(componentIndex); c0.setBounds(rowX[0], rowY + (this.rowHeights[rowIndex] - c0.getPreferredSize().height) / 2, this.columnWidths[0], c0.getPreferredSize().height); c1 = parent.getComponent(componentIndex + 1); c1.setBounds(rowX[1], rowY + (this.rowHeights[rowIndex] - c1.getPreferredSize().height) / 2, this.columnWidths[1], c1.getPreferredSize().height); c2 = parent.getComponent(componentIndex + 2); c2.setBounds(rowX[2], rowY + (this.rowHeights[rowIndex] - c2.getPreferredSize().height) / 2, this.columnWidths[2], c2.getPreferredSize().height); c3 = parent.getComponent(componentIndex + 3); c3.setBounds(rowX[3], rowY + (this.rowHeights[rowIndex] - c3.getPreferredSize().height) / 2, this.columnWidths[3], c3.getPreferredSize().height); c4 = parent.getComponent(componentIndex + 4); c4.setBounds(rowX[4], rowY + (this.rowHeights[rowIndex] - c4.getPreferredSize().height) / 2, w4to5, c4.getPreferredSize().height); componentIndex = componentIndex + 5; break; case FormatLayout.LCLCB: c0 = parent.getComponent(componentIndex); c0.setBounds(rowX[0], rowY + (this.rowHeights[rowIndex] - c0.getPreferredSize().height) / 2, this.columnWidths[0], c0.getPreferredSize().height); c1 = parent.getComponent(componentIndex + 1); c1.setBounds(rowX[1], rowY + (this.rowHeights[rowIndex] - c1.getPreferredSize().height) / 2, w1to2, c1.getPreferredSize().height); c2 = parent.getComponent(componentIndex + 2); c2.setBounds(rowX[3], rowY + (this.rowHeights[rowIndex] - c2.getPreferredSize().height) / 2, this.columnWidths[3], c2.getPreferredSize().height); c3 = parent.getComponent(componentIndex + 3); c3.setBounds(rowX[4], rowY + (this.rowHeights[rowIndex] - c3.getPreferredSize().height) / 2, this.columnWidths[4], c3.getPreferredSize().height); c4 = parent.getComponent(componentIndex + 4); c4.setBounds(rowX[5], rowY + (this.rowHeights[rowIndex] - c4.getPreferredSize().height) / 2, this.columnWidths[5], c4.getPreferredSize().height); componentIndex = componentIndex + 5; break; case FormatLayout.LCBLCB: c0 = parent.getComponent(componentIndex); c0.setBounds(rowX[0], rowY + (this.rowHeights[rowIndex] - c0.getPreferredSize().height) / 2, this.columnWidths[0], c0.getPreferredSize().height); c1 = parent.getComponent(componentIndex + 1); c1.setBounds(rowX[1], rowY + (this.rowHeights[rowIndex] - c1.getPreferredSize().height) / 2, this.columnWidths[1], c1.getPreferredSize().height); c2 = parent.getComponent(componentIndex + 2); c2.setBounds(rowX[2], rowY + (this.rowHeights[rowIndex] - c2.getPreferredSize().height) / 2, this.columnWidths[2], c2.getPreferredSize().height); c3 = parent.getComponent(componentIndex + 3); c3.setBounds(rowX[3], rowY + (this.rowHeights[rowIndex] - c3.getPreferredSize().height) / 2, this.columnWidths[3], c3.getPreferredSize().height); c4 = parent.getComponent(componentIndex + 4); c4.setBounds(rowX[4], rowY + (this.rowHeights[rowIndex] - c4.getPreferredSize().height) / 2, this.columnWidths[4], c4.getPreferredSize().height); c5 = parent.getComponent(componentIndex + 5); c5.setBounds(rowX[5], rowY + (this.rowHeights[rowIndex] - c5.getPreferredSize().height) / 2, this.columnWidths[5], c5.getPreferredSize().height); componentIndex = componentIndex + 6; break; } rowY = rowY + this.rowHeights[rowIndex] + this.rowGap; } } }
From source file:fxts.stations.ui.SideLayout.java
/** * Fills in an instance of <code>SideLayoutInfo</code> for the * current set of managed children. This requires three passes through the * set of children:/* ww w . j av a 2s.c om*/ * <ol> * <li>Figure out the dimensions of the layout grid. * <li>Determine which cells the components occupy. * <li>Distribute the weights and min sizes amoung the rows/columns. * </ol> * This also caches the minsizes for all the children when they are * first encountered (so subsequent loops don't need to ask again). * * @param aParent the layout container * @param aSizeflag either <code>PREFERREDSIZE</code> or <code>MINSIZE</code> * * @return the <code>SideLayoutInfo</code> for the set of children */ protected SideLayoutInfo getLayoutInfo(Container aParent, int aSizeflag) { synchronized (aParent.getTreeLock()) { SideLayoutInfo r = new SideLayoutInfo(); Component comp; SideConstraints constraints; Dimension d; Component[] components = aParent.getComponents(); int compindex; int i; int j; int k; int px; int py; int pixels_diff; int nextSize; int curX, curY, curWidth, curHeight, curRow, curCol; double weight_diff; double weight; double start; double size; int[] xMax; int[] yMax; /* * Pass #1 * * Figure out the dimensions of the layout grid (use a value of 1 for * zero or negative widths and heights). */ r.width = r.height = 0; curRow = curCol = -1; xMax = new int[MAXGRIDSIZE]; yMax = new int[MAXGRIDSIZE]; for (compindex = 0; compindex < components.length; compindex++) { comp = components[compindex]; if (!comp.isVisible()) { continue; } constraints = lookupConstraints(comp); curX = constraints.gridx; curY = constraints.gridy; curWidth = constraints.gridwidth; if (curWidth <= 0) { curWidth = 1; } curHeight = constraints.gridheight; if (curHeight <= 0) { curHeight = 1; } /* If x or y is negative, then use relative positioning: */ if (curX < 0 && curY < 0) { if (curRow >= 0) { curY = curRow; } else if (curCol >= 0) { curX = curCol; } else { curY = 0; } } if (curX < 0) { px = 0; for (i = curY; i < curY + curHeight; i++) { px = Math.max(px, xMax[i]); } curX = px - curX - 1; if (curX < 0) { curX = 0; } } else if (curY < 0) { py = 0; for (i = curX; i < curX + curWidth; i++) { py = Math.max(py, yMax[i]); } curY = py - curY - 1; if (curY < 0) { curY = 0; } } /* Adjust the grid width and height */ for (px = curX + curWidth; r.width < px; r.width++) { } for (py = curY + curHeight; r.height < py; r.height++) { } /* Adjust the xMax and yMax arrays */ for (i = curX; i < curX + curWidth; i++) { yMax[i] = py; } for (i = curY; i < curY + curHeight; i++) { xMax[i] = px; } /* Cache the current slave's size. */ if (aSizeflag == PREFERREDSIZE) { d = comp.getPreferredSize(); } else { d = comp.getMinimumSize(); } constraints.minWidth = d.width; //constraints.setMinWidth(d.width); constraints.minHeight = d.height; /* Zero width and height must mean that this is the last item (or * else something is wrong). */ if (constraints.gridheight == 0 && constraints.gridwidth == 0) { curRow = curCol = -1; } /* Zero width starts a new row */ if (constraints.gridheight == 0 && curRow < 0) { curCol = curX + curWidth; } /* Zero height starts a new column */ else if (constraints.gridwidth == 0 && curCol < 0) { curRow = curY + curHeight; } } /* * Apply minimum row/column dimensions */ if (mColumnWidths != null && r.width < mColumnWidths.length) { r.width = mColumnWidths.length; } if (mRowHeights != null && r.height < mRowHeights.length) { r.height = mRowHeights.length; } /* * Pass #2 * * Negative values for gridX are filled in with the current x value. * Negative values for gridY are filled in with the current y value. * Negative or zero values for gridWidth and gridHeight end the current * row or column, respectively. */ curRow = curCol = -1; xMax = new int[MAXGRIDSIZE]; yMax = new int[MAXGRIDSIZE]; for (compindex = 0; compindex < components.length; compindex++) { comp = components[compindex]; if (!comp.isVisible()) { continue; } constraints = lookupConstraints(comp); curX = constraints.gridx; curY = constraints.gridy; curWidth = constraints.gridwidth; curHeight = constraints.gridheight; /* If x or y is negative, then use relative positioning: */ if (curX < 0 && curY < 0) { if (curRow >= 0) { curY = curRow; } else if (curCol >= 0) { curX = curCol; } else { curY = 0; } } if (curX < 0) { if (curHeight <= 0) { curHeight += r.height - curY; if (curHeight < 1) { curHeight = 1; } } px = 0; for (i = curY; i < curY + curHeight; i++) { px = Math.max(px, xMax[i]); } curX = px - curX - 1; if (curX < 0) { curX = 0; } } else if (curY < 0) { if (curWidth <= 0) { curWidth += r.width - curX; if (curWidth < 1) { curWidth = 1; } } py = 0; for (i = curX; i < curX + curWidth; i++) { py = Math.max(py, yMax[i]); } curY = py - curY - 1; if (curY < 0) { curY = 0; } } if (curWidth <= 0) { curWidth += r.width - curX; if (curWidth < 1) { curWidth = 1; } } if (curHeight <= 0) { curHeight += r.height - curY; if (curHeight < 1) { curHeight = 1; } } px = curX + curWidth; py = curY + curHeight; for (i = curX; i < curX + curWidth; i++) { yMax[i] = py; } for (i = curY; i < curY + curHeight; i++) { xMax[i] = px; } /* Make negative sizes start a new row/column */ if (constraints.gridheight == 0 && constraints.gridwidth == 0) { curRow = curCol = -1; } if (constraints.gridheight == 0 && curRow < 0) { curCol = curX + curWidth; } else if (constraints.gridwidth == 0 && curCol < 0) { curRow = curY + curHeight; } /* Assign the new values to the gridbag slave */ constraints.tempX = curX; constraints.tempY = curY; constraints.tempWidth = curWidth; constraints.tempHeight = curHeight; } /* * Apply minimum row/column dimensions and weights */ if (mColumnWidths != null) { System.arraycopy(mColumnWidths, 0, r.minWidth, 0, mColumnWidths.length); } if (mRowHeights != null) { System.arraycopy(mRowHeights, 0, r.minHeight, 0, mRowHeights.length); } if (mColumnWeights != null) { System.arraycopy(mColumnWeights, 0, r.weightX, 0, mColumnWeights.length); } if (mRowWeights != null) { System.arraycopy(mRowWeights, 0, r.weightY, 0, mRowWeights.length); } /* * Pass #3 * * Distribute the minimun widths and weights: */ nextSize = Integer.MAX_VALUE; for (i = 1; i != Integer.MAX_VALUE; i = nextSize, nextSize = Integer.MAX_VALUE) { for (compindex = 0; compindex < components.length; compindex++) { comp = components[compindex]; if (!comp.isVisible()) { continue; } constraints = lookupConstraints(comp); if (constraints.tempWidth == i) { px = constraints.tempX + constraints.tempWidth; /* right column */ /* * Figure out if we should use this slave\'s weight. If the weight * is less than the total weight spanned by the width of the cell, * then discard the weight. Otherwise split the difference * according to the existing weights. */ weight_diff = constraints.weightx; for (k = constraints.tempX; k < px; k++) { weight_diff -= r.weightX[k]; } if (weight_diff > 0.0) { weight = 0.0; for (k = constraints.tempX; k < px; k++) { weight += r.weightX[k]; } for (k = constraints.tempX; weight > 0.0 && k < px; k++) { double wt = r.weightX[k]; double dx = (wt * weight_diff) / weight; r.weightX[k] += dx; weight_diff -= dx; weight -= wt; } /* Assign the remainder to the rightmost cell */ r.weightX[px - 1] += weight_diff; } /* * Calculate the minWidth array values. * First, figure out how wide the current slave needs to be. * Then, see if it will fit within the current minWidth values. * If it will not fit, add the difference according to the * weightX array. */ pixels_diff = constraints.minWidth + constraints.ipadx + constraints.insets.left + constraints.insets.right; for (k = constraints.tempX; k < px; k++) { pixels_diff -= r.minWidth[k]; } if (pixels_diff > 0) { weight = 0.0; for (k = constraints.tempX; k < px; k++) { weight += r.weightX[k]; } for (k = constraints.tempX; weight > 0.0 && k < px; k++) { double wt = r.weightX[k]; int dx = (int) ((wt * (double) pixels_diff) / weight); r.minWidth[k] += dx; pixels_diff -= dx; weight -= wt; } /* Any leftovers go into the rightmost cell */ r.minWidth[px - 1] += pixels_diff; } } else if (constraints.tempWidth > i && constraints.tempWidth < nextSize) { nextSize = constraints.tempWidth; } if (constraints.tempHeight == i) { py = constraints.tempY + constraints.tempHeight; /* bottom row */ /* * Figure out if we should use this slave's weight. If the weight * is less than the total weight spanned by the height of the cell, * then discard the weight. Otherwise split it the difference * according to the existing weights. */ weight_diff = constraints.weighty; for (k = constraints.tempY; k < py; k++) { weight_diff -= r.weightY[k]; } if (weight_diff > 0.0) { weight = 0.0; for (k = constraints.tempY; k < py; k++) { weight += r.weightY[k]; } for (k = constraints.tempY; weight > 0.0 && k < py; k++) { double wt = r.weightY[k]; double dy = (wt * weight_diff) / weight; r.weightY[k] += dy; weight_diff -= dy; weight -= wt; } /* Assign the remainder to the bottom cell */ r.weightY[py - 1] += weight_diff; } /* * Calculate the minHeight array values. * First, figure out how tall the current slave needs to be. * Then, see if it will fit within the current minHeight values. * If it will not fit, add the difference according to the * weightY array. */ pixels_diff = constraints.minHeight + constraints.ipady + constraints.insets.top + constraints.insets.bottom; for (k = constraints.tempY; k < py; k++) { pixels_diff -= r.minHeight[k]; } if (pixels_diff > 0) { weight = 0.0; for (k = constraints.tempY; k < py; k++) { weight += r.weightY[k]; } for (k = constraints.tempY; weight > 0.0 && k < py; k++) { double wt = r.weightY[k]; int dy = (int) ((wt * (double) pixels_diff) / weight); r.minHeight[k] += dy; pixels_diff -= dy; weight -= wt; } /* Any leftovers go into the bottom cell */ r.minHeight[py - 1] += pixels_diff; } } else if (constraints.tempHeight > i && constraints.tempHeight < nextSize) { nextSize = constraints.tempHeight; } } } return r; } }
From source file:org.kineticsystem.commons.layout.TetrisLayout.java
/** * Lay out components in the specified container. * @param parent The container which needs to be laid out. *///from ww w . ja va 2s . co m public void layoutContainer(Container parent) { /* * This method synchronizes on the tree lock of the component. This tree * lock is an object that can be used to provide thread-safe access to * the layout manager in case different threads are simultaneously * adding or removing components. The tree lock object is used as a * synchronization point for all of the methods associated with laying * out containers and invalidating components, and it is good * programming practice to use it to ensure a thread-safe * implementation. */ synchronized (parent.getTreeLock()) { // Layout components. if (containerSize == null) { setup(parent); } Insets insets = parent.getInsets(); int componentNumber = parent.getComponentCount(); if (componentNumber == 0) { return; } Dimension size = parent.getSize(); cGaps[0].setIncSize(cGaps[0].getSize()); for (int i = 1; i < cBars.length + 1; i++) { cGaps[i].setIncSize(cGaps[i].getSize() + cGaps[i - 1].getIncSize()); } rGaps[0].setIncSize(rGaps[0].getSize()); for (int i = 1; i < rBars.length + 1; i++) { rGaps[i].setIncSize(rGaps[i].getSize() + rGaps[i - 1].getIncSize()); } int actualGapFreeWidth = Math .max(size.width - insets.left - insets.right - cGaps[cBars.length].getIncSize(), 0); int actualGapFreeHeight = Math .max(size.height - insets.top - insets.bottom - rGaps[rBars.length].getIncSize(), 0); for (Map.Entry<Component, Cell> entry : constraintsMap.entrySet()) { Component comp = (Component) entry.getKey(); Cell cell = (Cell) entry.getValue(); if (cell != null) { // Actual cell position. int x0 = (int) Math.round(cSizes[cell.getCol()].evaluate(actualGapFreeWidth)); int y0 = (int) Math.round(rSizes[cell.getRow()].evaluate(actualGapFreeHeight)); int x1 = (int) Math.round(cSizes[cell.getCol() + cell.getCols()].evaluate(actualGapFreeWidth)); int y1 = (int) Math.round(rSizes[cell.getRow() + cell.getRows()].evaluate(actualGapFreeHeight)); // Actual cell dimension. int w = x1 - x0; int h = y1 - y0; // Component position correction. int xCorrection = insets.left + cGaps[cell.getCol()].getIncSize(); int yCorrection = insets.top + rGaps[cell.getRow()].getIncSize(); // Component dimension correction. int wCorrection = cGaps[cell.getCol() + cell.getCols() - 1].getIncSize() - cGaps[cell.getCol()].getIncSize(); int hCorrection = rGaps[cell.getRow() + cell.getRows() - 1].getIncSize() - rGaps[cell.getRow()].getIncSize(); // Preferred component dimension. int wComp = comp.getPreferredSize().width; int hComp = comp.getPreferredSize().height; int fill = cell.getFill(); int anchor = cell.getAnchor(); switch (fill) { case Cell.NONE: { if (wComp > w) { wComp = w; } if (hComp > h) { hComp = h; } switch (anchor) { case Cell.FIRST_LINE_START: x0 += xCorrection; y0 += yCorrection; break; case Cell.PAGE_START: x0 += xCorrection + (w - wComp) / 2; y0 += yCorrection; break; case Cell.FIRST_LINE_END: x0 += xCorrection + w + wCorrection - wComp; y0 += yCorrection; break; case Cell.LINE_START: x0 += xCorrection; y0 += yCorrection + (h - hComp) / 2; break; case Cell.CENTER: x0 += xCorrection + (w - wComp) / 2; y0 += yCorrection + (h - hComp) / 2; break; case Cell.LINE_END: x0 += xCorrection + w + wCorrection - wComp; y0 += yCorrection + (h - hComp) / 2; break; case Cell.LAST_LINE_START: x0 += xCorrection; y0 += yCorrection + h + hCorrection - hComp; break; case Cell.PAGE_END: x0 += xCorrection + (w - wComp) / 2; y0 += yCorrection + h + hCorrection - hComp; break; case Cell.LAST_LINE_END: x0 += xCorrection + w + wCorrection - wComp; y0 += yCorrection + h - hCorrection - hComp; break; } w = Math.min(w, wComp) + wCorrection; h = Math.min(h, hComp) + hCorrection; break; } case Cell.PROPORTIONAL: { double ratio = Math.min((double) ((double) w / (double) wComp), (double) ((double) h / (double) hComp)); wComp = (int) Math.round(wComp * ratio); hComp = (int) Math.round(hComp * ratio); switch (anchor) { case Cell.FIRST_LINE_START: x0 += xCorrection; y0 += yCorrection; break; case Cell.PAGE_START: x0 += xCorrection + (w - wComp) / 2; y0 += yCorrection; break; case Cell.FIRST_LINE_END: x0 += xCorrection + wCorrection + w - wComp; y0 += yCorrection; break; case Cell.LINE_START: x0 += xCorrection; y0 += yCorrection + (h - hComp) / 2; break; case Cell.CENTER: x0 += xCorrection + (w - wComp) / 2; y0 += yCorrection + (h - hComp) / 2; break; case Cell.LINE_END: x0 += xCorrection + wCorrection + w - wComp; y0 += yCorrection + (h - hComp) / 2; break; case Cell.LAST_LINE_START: x0 += xCorrection; y0 += yCorrection + hCorrection + h - hComp; break; case Cell.PAGE_END: x0 += xCorrection + (w - wComp) / 2; y0 += yCorrection + hCorrection + h - hComp; break; case Cell.LAST_LINE_END: x0 += xCorrection + wCorrection + w - wComp; y0 += yCorrection + hCorrection + h - hComp; break; } w = Math.min(w, wComp) + wCorrection; h = Math.min(h, hComp) + hCorrection; break; } case Cell.BOTH: { x0 += xCorrection; y0 += yCorrection; w += wCorrection; h += hCorrection; break; } case Cell.HORIZONTAL: { if (hComp > h) { hComp = h; } switch (anchor) { case Cell.FIRST_LINE_START: case Cell.PAGE_START: case Cell.FIRST_LINE_END: y0 += yCorrection; break; case Cell.LINE_START: case Cell.CENTER: case Cell.LINE_END: y0 += yCorrection + (h - hComp) / 2; break; case Cell.LAST_LINE_START: case Cell.PAGE_END: case Cell.LAST_LINE_END: y0 += yCorrection + h + hCorrection - hComp; break; } x0 += xCorrection; w += wCorrection; h = Math.min(h, hComp) + hCorrection; break; } case Cell.VERTICAL: { if (wComp > w) { wComp = w; } switch (anchor) { case Cell.FIRST_LINE_START: case Cell.LINE_START: case Cell.LAST_LINE_START: x0 += xCorrection; break; case Cell.PAGE_START: case Cell.CENTER: case Cell.PAGE_END: x0 += xCorrection + (w - wComp) / 2; break; case Cell.FIRST_LINE_END: case Cell.LINE_END: case Cell.LAST_LINE_END: x0 += xCorrection + w + wCorrection - wComp; break; } y0 += yCorrection; w = Math.min(w, wComp) + wCorrection; h += hCorrection; break; } } comp.setBounds(x0, y0, w, h); } } } }