home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1998 January / PCO0198.ISO / 1&1 / java.z / java_301 / java / awt / GridLayout.class (.txt) < prev    next >
Encoding:
Java Class File  |  1996-10-20  |  3.5 KB  |  128 lines

  1. package java.awt;
  2.  
  3. public class GridLayout implements LayoutManager {
  4.    int hgap;
  5.    int vgap;
  6.    int rows;
  7.    int cols;
  8.  
  9.    public GridLayout(int rows, int cols) {
  10.       this(rows, cols, 0, 0);
  11.    }
  12.  
  13.    public GridLayout(int rows, int cols, int hgap, int vgap) {
  14.       if (rows == 0 && cols == 0) {
  15.          throw new IllegalArgumentException("invalid rows,cols");
  16.       } else {
  17.          this.rows = rows;
  18.          this.cols = cols;
  19.          this.hgap = hgap;
  20.          this.vgap = vgap;
  21.       }
  22.    }
  23.  
  24.    public void addLayoutComponent(String name, Component comp) {
  25.    }
  26.  
  27.    public void removeLayoutComponent(Component comp) {
  28.    }
  29.  
  30.    public Dimension preferredLayoutSize(Container parent) {
  31.       Insets insets = parent.insets();
  32.       int ncomponents = parent.countComponents();
  33.       int nrows = this.rows;
  34.       int ncols = this.cols;
  35.       if (nrows > 0) {
  36.          ncols = (ncomponents + nrows - 1) / nrows;
  37.       } else {
  38.          nrows = (ncomponents + ncols - 1) / ncols;
  39.       }
  40.  
  41.       int w = 0;
  42.       int h = 0;
  43.  
  44.       for(int i = 0; i < ncomponents; ++i) {
  45.          Component comp = parent.getComponent(i);
  46.          Dimension d = comp.preferredSize();
  47.          if (w < d.width) {
  48.             w = d.width;
  49.          }
  50.  
  51.          if (h < d.height) {
  52.             h = d.height;
  53.          }
  54.       }
  55.  
  56.       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);
  57.    }
  58.  
  59.    public Dimension minimumLayoutSize(Container parent) {
  60.       Insets insets = parent.insets();
  61.       int ncomponents = parent.countComponents();
  62.       int nrows = this.rows;
  63.       int ncols = this.cols;
  64.       if (nrows > 0) {
  65.          ncols = (ncomponents + nrows - 1) / nrows;
  66.       } else {
  67.          nrows = (ncomponents + ncols - 1) / ncols;
  68.       }
  69.  
  70.       int w = 0;
  71.       int h = 0;
  72.  
  73.       for(int i = 0; i < ncomponents; ++i) {
  74.          Component comp = parent.getComponent(i);
  75.          Dimension d = comp.minimumSize();
  76.          if (w < d.width) {
  77.             w = d.width;
  78.          }
  79.  
  80.          if (h < d.height) {
  81.             h = d.height;
  82.          }
  83.       }
  84.  
  85.       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);
  86.    }
  87.  
  88.    public void layoutContainer(Container parent) {
  89.       Insets insets = parent.insets();
  90.       int ncomponents = parent.countComponents();
  91.       int nrows = this.rows;
  92.       int ncols = this.cols;
  93.       if (ncomponents != 0) {
  94.          if (nrows > 0) {
  95.             ncols = (ncomponents + nrows - 1) / nrows;
  96.          } else {
  97.             nrows = (ncomponents + ncols - 1) / ncols;
  98.          }
  99.  
  100.          int w = parent.width - (insets.left + insets.right);
  101.          int h = parent.height - (insets.top + insets.bottom);
  102.          w = (w - (ncols - 1) * this.hgap) / ncols;
  103.          h = (h - (nrows - 1) * this.vgap) / nrows;
  104.          int c = 0;
  105.  
  106.          for(int x = insets.left; c < ncols; x += w + this.hgap) {
  107.             int r = 0;
  108.  
  109.             for(int y = insets.top; r < nrows; y += h + this.vgap) {
  110.                int i = r * ncols + c;
  111.                if (i < ncomponents) {
  112.                   parent.getComponent(i).reshape(x, y, w, h);
  113.                }
  114.  
  115.                ++r;
  116.             }
  117.  
  118.             ++c;
  119.          }
  120.  
  121.       }
  122.    }
  123.  
  124.    public String toString() {
  125.       return this.getClass().getName() + "[hgap=" + this.hgap + ",vgap=" + this.vgap + ",rows=" + this.rows + ",cols=" + this.cols + "]";
  126.    }
  127. }
  128.