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

  1. package sun.awt;
  2.  
  3. import java.awt.Component;
  4. import java.awt.Container;
  5. import java.awt.Dimension;
  6. import java.awt.GridLayout;
  7. import java.awt.Insets;
  8.  
  9. public class VariableGridLayout extends GridLayout {
  10.    double[] rowFractions;
  11.    double[] colFractions;
  12.    int rows;
  13.    int cols;
  14.    int hgap;
  15.    int vgap;
  16.  
  17.    public VariableGridLayout(int rows, int cols) {
  18.       this(rows, cols, 0, 0);
  19.       if (rows != 0) {
  20.          this.stdRowFractions(rows);
  21.       }
  22.  
  23.       if (cols != 0) {
  24.          this.stdColFractions(cols);
  25.       }
  26.  
  27.    }
  28.  
  29.    public VariableGridLayout(int rows, int cols, int hgap, int vgap) {
  30.       super(rows, cols, hgap, vgap);
  31.       this.rows = rows;
  32.       this.cols = cols;
  33.       this.hgap = hgap;
  34.       this.vgap = vgap;
  35.       if (rows != 0) {
  36.          this.stdRowFractions(rows);
  37.       }
  38.  
  39.       if (cols != 0) {
  40.          this.stdColFractions(cols);
  41.       }
  42.  
  43.    }
  44.  
  45.    void stdRowFractions(int nrows) {
  46.       this.rowFractions = new double[nrows];
  47.  
  48.       for(int i = 0; i < nrows; ++i) {
  49.          this.rowFractions[i] = (double)1.0F / (double)nrows;
  50.       }
  51.  
  52.    }
  53.  
  54.    void stdColFractions(int ncols) {
  55.       this.colFractions = new double[ncols];
  56.  
  57.       for(int i = 0; i < ncols; ++i) {
  58.          this.colFractions[i] = (double)1.0F / (double)ncols;
  59.       }
  60.  
  61.    }
  62.  
  63.    public void setRowFraction(int rowNum, double fraction) {
  64.       this.rowFractions[rowNum] = fraction;
  65.    }
  66.  
  67.    public void setColFraction(int colNum, double fraction) {
  68.       this.colFractions[colNum] = fraction;
  69.    }
  70.  
  71.    public double getRowFraction(int rowNum) {
  72.       return this.rowFractions[rowNum];
  73.    }
  74.  
  75.    public double getColFraction(int colNum) {
  76.       return this.colFractions[colNum];
  77.    }
  78.  
  79.    public void layoutContainer(Container parent) {
  80.       Insets insets = parent.insets();
  81.       int ncomponents = parent.countComponents();
  82.       int nrows = this.rows;
  83.       int ncols = this.cols;
  84.       if (nrows > 0) {
  85.          ncols = (ncomponents + nrows - 1) / nrows;
  86.       } else {
  87.          nrows = (ncomponents + ncols - 1) / ncols;
  88.       }
  89.  
  90.       if (this.rows == 0) {
  91.          this.stdRowFractions(nrows);
  92.       }
  93.  
  94.       if (this.cols == 0) {
  95.          this.stdColFractions(ncols);
  96.       }
  97.  
  98.       Dimension size = ((Component)parent).size();
  99.       int w = size.width - (insets.left + insets.right);
  100.       int h = size.height - (insets.top + insets.bottom);
  101.       w -= (ncols - 1) * this.hgap;
  102.       h -= (nrows - 1) * this.vgap;
  103.       int c = 0;
  104.  
  105.       for(int x = insets.left; c < ncols; ++c) {
  106.          int colWidth = (int)(this.getColFraction(c) * (double)w);
  107.          int r = 0;
  108.  
  109.          for(int y = insets.top; r < nrows; ++r) {
  110.             int i = r * ncols + c;
  111.             int rowHeight = (int)(this.getRowFraction(r) * (double)h);
  112.             if (i < ncomponents) {
  113.                parent.getComponent(i).reshape(x, y, colWidth, rowHeight);
  114.             }
  115.  
  116.             y += rowHeight + this.vgap;
  117.          }
  118.  
  119.          x += colWidth + this.hgap;
  120.       }
  121.  
  122.    }
  123.  
  124.    static String fracsToString(double[] array) {
  125.       String result = "[" + array.length + "]";
  126.  
  127.       for(int i = 0; i < array.length; ++i) {
  128.          result = result + "<" + array[i] + ">";
  129.       }
  130.  
  131.       return result;
  132.    }
  133.  
  134.    public String toString() {
  135.       return this.getClass().getName() + "[hgap=" + this.hgap + ",vgap=" + this.vgap + ",rows=" + this.rows + ",cols=" + this.cols + ",rowFracs=" + fracsToString(this.rowFractions) + ",colFracs=" + fracsToString(this.colFractions) + "]";
  136.    }
  137. }
  138.