home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 25 / CDROM25.iso / Share / prog / VJ11 / VJTRIAL.EXE / IE30Java.exe / classd.exe / sun / awt / VariableGridLayout.java < prev    next >
Encoding:
Java Source  |  1997-01-27  |  5.7 KB  |  227 lines

  1. /*
  2.  * @(#)VariableGridLayout.java    1.3 95/10/04 Herb Jellinek
  3.  *
  4.  * Copyright (c) 1995 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19.  
  20. package sun.awt;
  21.  
  22. import java.awt.*;
  23. import java.util.BitSet;
  24.  
  25.  
  26. /**
  27.  * A layout manager for a container that lays out grids.  Allows setting
  28.  * the relative sizes of rows and columns.
  29.  *
  30.  * @version 1.3, 10/04/95
  31.  * @author Herb Jellinek
  32.  */
  33.  
  34.  
  35. public class VariableGridLayout extends GridLayout {
  36.  
  37.     BitSet rowsSet = new BitSet();
  38.     double rowFractions[] = null;
  39.  
  40.     BitSet colsSet = new BitSet();
  41.     double colFractions[] = null;
  42.  
  43.     int rows;
  44.     int cols;
  45.     int hgap;
  46.     int vgap;
  47.     
  48.     /**
  49.      * Creates a grid layout with the specified rows and specified columns.
  50.      * @param rows the rows
  51.      * @param cols the columns
  52.      */
  53.     public VariableGridLayout(int rows, int cols) {
  54.     this(rows, cols, 0, 0);
  55.  
  56.     if (rows != 0) {
  57.         rowsSet = new BitSet(rows);
  58.         stdRowFractions(rows);
  59.     }
  60.     
  61.     if (cols != 0) {
  62.         colsSet = new BitSet(cols);
  63.         stdColFractions(cols);
  64.     }
  65.     }
  66.  
  67.     /**
  68.      * Creates a grid layout with the specified rows, columns,
  69.      * horizontal gap, and vertical gap.
  70.      * @param rows the rows
  71.      * @param cols the columns
  72.      * @param hgap the horizontal gap variable
  73.      * @param vgap the vertical gap variable
  74.      * @exception IllegalArgumentException If the rows and columns are invalid.
  75.      */
  76.     public VariableGridLayout(int rows, int cols, int hgap, int vgap) {
  77.     super(rows, cols, hgap, vgap);
  78.  
  79.     this.rows = rows;
  80.     this.cols = cols;
  81.     this.hgap = hgap;
  82.     this.vgap = vgap;
  83.     
  84.     if (rows != 0) {
  85.         rowsSet = new BitSet(rows);
  86.         stdRowFractions(rows);
  87.     }
  88.     
  89.     if (cols != 0) {
  90.         colsSet = new BitSet(cols);
  91.         stdColFractions(cols);
  92.     }
  93.     }
  94.  
  95.     void stdRowFractions(int nrows) {
  96.     rowFractions = new double[nrows];
  97.     for (int i = 0; i < nrows; i++) {
  98.         rowFractions[i] = 1.0 / nrows;
  99.     }
  100.     }
  101.  
  102.     void stdColFractions(int ncols) {
  103.     colFractions = new double[ncols];
  104.     for (int i = 0; i < ncols; i++) {
  105.         colFractions[i] = 1.0 / ncols;
  106.     }
  107.     }
  108.     
  109.     public void setRowFraction(int rowNum, double fraction) {
  110.     rowsSet.set(rowNum);
  111.     rowFractions[rowNum] = fraction;
  112.     }
  113.  
  114.     public void setColFraction(int colNum, double fraction) {
  115.     colsSet.set(colNum);
  116.     colFractions[colNum] = fraction;
  117.     }
  118.  
  119.     public double getRowFraction(int rowNum) {
  120.     return rowFractions[rowNum];
  121.     }
  122.  
  123.     public double getColFraction(int colNum) {
  124.     return colFractions[colNum];
  125.     }
  126.  
  127.     void allocateExtraSpace(double vec[], BitSet userSet) {
  128.     // collect the space that's been explicitly allocated...
  129.     double total = 0.0;
  130.     int unallocated = 0;
  131.     int i;
  132.     for (i = 0; i < vec.length; i++) {
  133.         if (userSet.get(i)) {
  134.         total += vec[i];
  135.         } else {
  136.         unallocated++;
  137.         }
  138.     }
  139.  
  140.     // ... then spread the extra space
  141.     if (unallocated != 0) {
  142.         double space = (1.0 - total) / unallocated;
  143.         for (i = 0; i < vec.length; i++) {
  144.         if (!userSet.get(i)) {
  145.             vec[i] = space;
  146.             userSet.set(i);
  147.         }
  148.         }
  149.     }
  150.     }    
  151.             
  152.     
  153.     void allocateExtraSpace() {
  154.     allocateExtraSpace(rowFractions, rowsSet);
  155.     allocateExtraSpace(colFractions, colsSet);
  156.     }
  157.     
  158.     /** 
  159.      * Lays out the container in the specified panel.  
  160.      * @param parent the specified component being laid out
  161.      * @see Container
  162.      */
  163.     public void layoutContainer(Container parent) {
  164.     Insets insets = parent.insets();
  165.     int ncomponents = parent.countComponents();
  166.     int nrows = rows;
  167.     int ncols = cols;
  168.  
  169.     if (nrows > 0) {
  170.         ncols = (ncomponents + nrows - 1) / nrows;
  171.     } else {
  172.         nrows = (ncomponents + ncols - 1) / ncols;
  173.     }
  174.  
  175.     if (rows == 0) {
  176.         stdRowFractions(nrows);
  177.     }
  178.     if (cols == 0) {
  179.         stdColFractions(ncols);
  180.     }
  181.  
  182.     Dimension size = parent.size();
  183.     int w = size.width - (insets.left + insets.right);
  184.     int h = size.height - (insets.top + insets.bottom);
  185.  
  186.     w = (w - (ncols - 1) * hgap);
  187.     h = (h - (nrows - 1) * vgap);
  188.  
  189.     allocateExtraSpace();
  190.     
  191.     for (int c = 0, x = insets.left ; c < ncols ; c++) {
  192.         int colWidth = (int)(getColFraction(c) * w);
  193.         for (int r = 0, y = insets.top ; r < nrows ; r++) {
  194.         int i = r * ncols + c;
  195.         int rowHeight = (int)(getRowFraction(r) * h);
  196.         
  197.         if (i < ncomponents) {
  198.             parent.getComponent(i).reshape(x, y, colWidth, rowHeight);
  199.         }
  200.         y += rowHeight + vgap;
  201.         }
  202.         x += colWidth + hgap;
  203.     }
  204.     }
  205.  
  206.     static String fracsToString(double array[]) {
  207.     String result = "["+array.length+"]";
  208.     
  209.     for (int i = 0; i < array.length; i++) {
  210.         result += "<"+array[i]+">";
  211.     }
  212.     return result;
  213.     }
  214.     
  215.     /**
  216.      * Returns the String representation of this VariableGridLayout's values.
  217.      */
  218.     public String toString() {
  219.     return getClass().getName() + "[hgap=" + hgap + ",vgap=" + vgap + 
  220.                            ",rows=" + rows + ",cols=" + cols +
  221.                        ",rowFracs=" +
  222.                        fracsToString(rowFractions) +
  223.                        ",colFracs=" +
  224.                        fracsToString(colFractions) + "]";
  225.     }
  226. }
  227.