home *** CD-ROM | disk | FTP | other *** search
- package java.awt;
-
- public class GridLayout implements LayoutManager {
- int hgap;
- int vgap;
- int rows;
- int cols;
-
- public GridLayout(int rows, int cols) {
- this(rows, cols, 0, 0);
- }
-
- public GridLayout(int rows, int cols, int hgap, int vgap) {
- if (rows == 0 && cols == 0) {
- throw new IllegalArgumentException("invalid rows,cols");
- } else {
- this.rows = rows;
- this.cols = cols;
- this.hgap = hgap;
- this.vgap = vgap;
- }
- }
-
- public void addLayoutComponent(String name, Component comp) {
- }
-
- public void removeLayoutComponent(Component comp) {
- }
-
- public Dimension preferredLayoutSize(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;
- }
-
- int w = 0;
- int h = 0;
-
- for(int i = 0; i < ncomponents; ++i) {
- Component comp = parent.getComponent(i);
- Dimension d = comp.preferredSize();
- if (w < d.width) {
- w = d.width;
- }
-
- if (h < d.height) {
- h = d.height;
- }
- }
-
- return new Dimension(insets.left + insets.right + ncols * w + (this.cols - 1) * this.hgap, insets.top + insets.bottom + nrows * h + (this.rows - 1) * this.vgap);
- }
-
- public Dimension minimumLayoutSize(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;
- }
-
- int w = 0;
- int h = 0;
-
- for(int i = 0; i < ncomponents; ++i) {
- Component comp = parent.getComponent(i);
- Dimension d = comp.minimumSize();
- if (w < d.width) {
- w = d.width;
- }
-
- if (h < d.height) {
- h = d.height;
- }
- }
-
- return new Dimension(insets.left + insets.right + ncols * w + (this.cols - 1) * this.hgap, insets.top + insets.bottom + nrows * h + (this.rows - 1) * this.vgap);
- }
-
- public void layoutContainer(Container parent) {
- Insets insets = parent.insets();
- int ncomponents = parent.countComponents();
- int nrows = this.rows;
- int ncols = this.cols;
- if (ncomponents != 0) {
- if (nrows > 0) {
- ncols = (ncomponents + nrows - 1) / nrows;
- } else {
- nrows = (ncomponents + ncols - 1) / ncols;
- }
-
- int w = parent.width - (insets.left + insets.right);
- int h = parent.height - (insets.top + insets.bottom);
- w = (w - (ncols - 1) * this.hgap) / ncols;
- h = (h - (nrows - 1) * this.vgap) / nrows;
- int c = 0;
-
- for(int x = insets.left; c < ncols; x += w + this.hgap) {
- int r = 0;
-
- for(int y = insets.top; r < nrows; y += h + this.vgap) {
- int i = r * ncols + c;
- if (i < ncomponents) {
- parent.getComponent(i).reshape(x, y, w, h);
- }
-
- ++r;
- }
-
- ++c;
- }
-
- }
- }
-
- public String toString() {
- return this.getClass().getName() + "[hgap=" + this.hgap + ",vgap=" + this.vgap + ",rows=" + this.rows + ",cols=" + this.cols + "]";
- }
- }
-