home *** CD-ROM | disk | FTP | other *** search
- package sun.awt;
-
- import java.awt.Component;
- import java.awt.Container;
- import java.awt.Dimension;
- import java.awt.GridLayout;
- import java.awt.Insets;
-
- public class VariableGridLayout extends GridLayout {
- double[] rowFractions;
- double[] colFractions;
- int rows;
- int cols;
- int hgap;
- int vgap;
-
- public VariableGridLayout(int rows, int cols) {
- this(rows, cols, 0, 0);
- if (rows != 0) {
- this.stdRowFractions(rows);
- }
-
- if (cols != 0) {
- this.stdColFractions(cols);
- }
-
- }
-
- public VariableGridLayout(int rows, int cols, int hgap, int vgap) {
- super(rows, cols, hgap, vgap);
- this.rows = rows;
- this.cols = cols;
- this.hgap = hgap;
- this.vgap = vgap;
- if (rows != 0) {
- this.stdRowFractions(rows);
- }
-
- if (cols != 0) {
- this.stdColFractions(cols);
- }
-
- }
-
- void stdRowFractions(int nrows) {
- this.rowFractions = new double[nrows];
-
- for(int i = 0; i < nrows; ++i) {
- this.rowFractions[i] = (double)1.0F / (double)nrows;
- }
-
- }
-
- void stdColFractions(int ncols) {
- this.colFractions = new double[ncols];
-
- for(int i = 0; i < ncols; ++i) {
- this.colFractions[i] = (double)1.0F / (double)ncols;
- }
-
- }
-
- public void setRowFraction(int rowNum, double fraction) {
- this.rowFractions[rowNum] = fraction;
- }
-
- public void setColFraction(int colNum, double fraction) {
- this.colFractions[colNum] = fraction;
- }
-
- public double getRowFraction(int rowNum) {
- return this.rowFractions[rowNum];
- }
-
- public double getColFraction(int colNum) {
- return this.colFractions[colNum];
- }
-
- public void layoutContainer(Container parent) {
- Insets insets = parent.insets();
- int ncomponents = parent.countComponents();
- int nrows = this.rows;
- int ncols = this.cols;
- if (nrows > 0) {
- ncols = (ncomponents + nrows - 1) / nrows;
- } else {
- nrows = (ncomponents + ncols - 1) / ncols;
- }
-
- if (this.rows == 0) {
- this.stdRowFractions(nrows);
- }
-
- if (this.cols == 0) {
- this.stdColFractions(ncols);
- }
-
- Dimension size = ((Component)parent).size();
- int w = size.width - (insets.left + insets.right);
- int h = size.height - (insets.top + insets.bottom);
- w -= (ncols - 1) * this.hgap;
- h -= (nrows - 1) * this.vgap;
- int c = 0;
-
- for(int x = insets.left; c < ncols; ++c) {
- int colWidth = (int)(this.getColFraction(c) * (double)w);
- int r = 0;
-
- for(int y = insets.top; r < nrows; ++r) {
- int i = r * ncols + c;
- int rowHeight = (int)(this.getRowFraction(r) * (double)h);
- if (i < ncomponents) {
- parent.getComponent(i).reshape(x, y, colWidth, rowHeight);
- }
-
- y += rowHeight + this.vgap;
- }
-
- x += colWidth + this.hgap;
- }
-
- }
-
- static String fracsToString(double[] array) {
- String result = "[" + array.length + "]";
-
- for(int i = 0; i < array.length; ++i) {
- result = result + "<" + array[i] + ">";
- }
-
- return result;
- }
-
- public String toString() {
- return this.getClass().getName() + "[hgap=" + this.hgap + ",vgap=" + this.vgap + ",rows=" + this.rows + ",cols=" + this.cols + ",rowFracs=" + fracsToString(this.rowFractions) + ",colFracs=" + fracsToString(this.colFractions) + "]";
- }
- }
-