home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / jbuilder / unsupported / JDK1.2beta3 / SOURCE / SRC.ZIP / java / awt / GridLayout.java < prev    next >
Encoding:
Java Source  |  1998-03-20  |  11.7 KB  |  361 lines

  1. /*
  2.  * @(#)GridLayout.java    1.19 98/03/18
  3.  *
  4.  * Copyright 1995-1997 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.awt;
  16.  
  17. /**
  18.  * The <code>GridLayout</code> class is a layout manager that 
  19.  * lays out a container's components in a rectangular grid. 
  20.  * <p>
  21.  * The container is divided into equal-sized rectangles, 
  22.  * and one component is placed in each rectangle. 
  23.  * <p>
  24.  * For example, the following is an applet that lays out six buttons 
  25.  * into three rows and two columns: 
  26.  * <p>
  27.  * <hr><blockquote><pre>
  28.  * import java.awt.*;
  29.  * import java.applet.Applet;
  30.  * public class ButtonGrid extends Applet {
  31.  *     public void init() {
  32.  *         setLayout(new GridLayout(3,2));
  33.  *         add(new Button("1"));
  34.  *         add(new Button("2"));
  35.  *         add(new Button("3"));
  36.  *         add(new Button("4"));
  37.  *         add(new Button("5"));
  38.  *         add(new Button("6"));
  39.  *     }
  40.  * }
  41.  * </pre></blockquote><hr>     
  42.  * <p>
  43.  * It produces the following output:
  44.  * <p>
  45.  * <img src="images-awt/GridLayout-1.gif" 
  46.  * ALIGN=center HSPACE=10 VSPACE=7>
  47.  *
  48.  * @version 1.19, 03/18/98
  49.  * @author 
  50.  * @since   JDK1.0
  51.  */
  52. public class GridLayout implements LayoutManager, java.io.Serializable {
  53.     int hgap;
  54.     int vgap;
  55.     int rows;
  56.     int cols;
  57.  
  58.     /**
  59.      * Creates a grid layout with a default of one column per component,
  60.      * in a single row.
  61.      * @since JDK1.1
  62.      */
  63.     public GridLayout() {
  64.     this(1, 0, 0, 0);
  65.     }
  66.  
  67.     /**
  68.      * Creates a grid layout with the specified number of rows and 
  69.      * columns. All components in the layout are given equal size. 
  70.      * <p>
  71.      * One, but not both, of <code>rows</code> and <code>cols</code> can 
  72.      * be zero, which means that any number of objects can be placed in a 
  73.      * row or in a column. 
  74.      * @param     rows   the rows, with the value zero meaning 
  75.      *                   any number of rows.
  76.      * @param     cols   the columns, with the value zero meaning 
  77.      *                   any number of columns.
  78.      */
  79.     public GridLayout(int rows, int cols) {
  80.     this(rows, cols, 0, 0);
  81.     }
  82.  
  83.     /**
  84.      * Creates a grid layout with the specified number of rows and 
  85.      * columns. All components in the layout are given equal size. 
  86.      * <p>
  87.      * In addition, the horizontal and vertical gaps are set to the 
  88.      * specified values. Horizontal gaps are placed at the left and 
  89.      * right edges, and between each of the columns. Vertical gaps are 
  90.      * placed at the top and bottom edges, and between each of the rows. 
  91.      * <p>
  92.      * One, but not both, of <code>rows</code> and <code>cols</code> can 
  93.      * be zero, which means that any number of objects can be placed in a 
  94.      * row or in a column. 
  95.      * @param     rows   the rows, with the value zero meaning 
  96.      *                   any number of rows.
  97.      * @param     cols   the columns, with the value zero meaning 
  98.      *                   any number of columns.
  99.      * @param     hgap   the horizontal gap. 
  100.      * @param     vgap   the vertical gap. 
  101.      * @exception   IllegalArgumentException  if the of <code>rows</code> 
  102.      *                   or <code>cols</code> is invalid.
  103.      */
  104.     public GridLayout(int rows, int cols, int hgap, int vgap) {
  105.     if ((rows == 0) && (cols == 0)) {
  106.         throw new IllegalArgumentException("rows and cols cannot both be zero");
  107.     }
  108.     this.rows = rows;
  109.     this.cols = cols;
  110.     this.hgap = hgap;
  111.     this.vgap = vgap;
  112.     }
  113.  
  114.     /**
  115.      * Gets the number of rows in this layout.
  116.      * @return    the number of rows in this layout.
  117.      * @since     JDK1.1
  118.      */
  119.     public int getRows() {
  120.     return rows;
  121.     }
  122.  
  123.     /**
  124.      * Sets the number of rows in this layout to the specified value.
  125.      * @param        rows   the number of rows in this layout.
  126.      * @exception    IllegalArgumentException  if the value of both 
  127.      *               <code>rows</code> and <code>cols</code> is set to zero.
  128.      * @since        JDK1.1
  129.      */
  130.     public void setRows(int rows) {
  131.     if ((rows == 0) && (this.cols == 0)) {
  132.         throw new IllegalArgumentException("rows and cols cannot both be zero");
  133.     }
  134.     this.rows = rows;
  135.     }
  136.  
  137.     /**
  138.      * Gets the number of columns in this layout.
  139.      * @return     the number of columns in this layout.
  140.      * @since      JDK1.1
  141.      */
  142.     public int getColumns() {
  143.     return cols;
  144.     }
  145.  
  146.     /**
  147.      * Sets the number of columns in this layout to the specified value.
  148.      * @param        cols   the number of columns in this layout.
  149.      * @exception    IllegalArgumentException  if the value of both 
  150.      *               <code>rows</code> and <code>cols</code> is set to zero.
  151.      * @since        JDK1.1
  152.      */
  153.     public void setColumns(int cols) {
  154.     if ((cols == 0) && (this.rows == 0)) {
  155.         throw new IllegalArgumentException("rows and cols cannot both be zero");
  156.     }
  157.     this.cols = cols;
  158.     }
  159.  
  160.     /**
  161.      * Gets the horizontal gap between components.
  162.      * @return       the horizontal gap between components.
  163.      * @since        JDK1.1
  164.      */
  165.     public int getHgap() {
  166.     return hgap;
  167.     }
  168.     
  169.     /**
  170.      * Sets the horizontal gap between components to the specified value.
  171.      * @param        hgap   the horizontal gap between components.
  172.      * @since        JDK1.1
  173.      */
  174.     public void setHgap(int hgap) {
  175.     this.hgap = hgap;
  176.     }
  177.     
  178.     /**
  179.      * Gets the vertical gap between components.
  180.      * @return       the vertical gap between components.
  181.      * @since        JDK1.1
  182.      */
  183.     public int getVgap() {
  184.     return vgap;
  185.     }
  186.     
  187.     /**
  188.      * Sets the vertical gap between components to the specified value.
  189.      * @param         vgap  the vertical gap between components.
  190.      * @since        JDK1.1
  191.      */
  192.     public void setVgap(int vgap) {
  193.     this.vgap = vgap;
  194.     }
  195.  
  196.     /**
  197.      * Adds the specified component with the specified name to the layout.
  198.      * @param name the name of the component.
  199.      * @param comp the component to be added.
  200.      */
  201.     public void addLayoutComponent(String name, Component comp) {
  202.     }
  203.  
  204.     /**
  205.      * Removes the specified component from the layout. 
  206.      * @param comp the component to be removed.
  207.      */
  208.     public void removeLayoutComponent(Component comp) {
  209.     }
  210.  
  211.     /** 
  212.      * Determines the preferred size of the container argument using 
  213.      * this grid layout. 
  214.      * <p>
  215.      * The preferred width of a grid layout is the largest preferred 
  216.      * width of any of the widths in the container times the number of 
  217.      * columns, plus the horizontal padding times the number of columns 
  218.      * plus one, plus the left and right insets of the target container. 
  219.      * <p>
  220.      * The preferred height of a grid layout is the largest preferred 
  221.      * height of any of the heights in the container times the number of 
  222.      * rows, plus the vertical padding times the number of rows plus one, 
  223.      * plus the top and bottom insets of the target container. 
  224.      * 
  225.      * @param     target   the container in which to do the layout.
  226.      * @return    the preferred dimensions to lay out the 
  227.      *                      subcomponents of the specified container.
  228.      * @see       java.awt.GridLayout#minimumLayoutSize 
  229.      * @see       java.awt.Container#getPreferredSize()
  230.      */
  231.     public Dimension preferredLayoutSize(Container parent) {
  232.     Insets insets = parent.getInsets();
  233.     int ncomponents = parent.getComponentCount();
  234.     int nrows = rows;
  235.     int ncols = cols;
  236.  
  237.     if (nrows > 0) {
  238.         ncols = (ncomponents + nrows - 1) / nrows;
  239.     } else {
  240.         nrows = (ncomponents + ncols - 1) / ncols;
  241.     }
  242.     int w = 0;
  243.     int h = 0;
  244.     for (int i = 0 ; i < ncomponents ; i++) {
  245.         Component comp = parent.getComponent(i);
  246.         Dimension d = comp.getPreferredSize();
  247.         if (w < d.width) {
  248.         w = d.width;
  249.         }
  250.         if (h < d.height) {
  251.         h = d.height;
  252.         }
  253.     }
  254.     return new Dimension(insets.left + insets.right + ncols*w + (ncols-1)*hgap, 
  255.                  insets.top + insets.bottom + nrows*h + (nrows-1)*vgap);
  256.     }
  257.  
  258.     /**
  259.      * Determines the minimum size of the container argument using this 
  260.      * grid layout. 
  261.      * <p>
  262.      * The minimum width of a grid layout is the largest minimum width 
  263.      * of any of the widths in the container times the number of columns, 
  264.      * plus the horizontal padding times the number of columns plus one, 
  265.      * plus the left and right insets of the target container. 
  266.      * <p>
  267.      * The minimum height of a grid layout is the largest minimum height 
  268.      * of any of the heights in the container times the number of rows, 
  269.      * plus the vertical padding times the number of rows plus one, plus 
  270.      * the top and bottom insets of the target container. 
  271.      *  
  272.      * @param       target   the container in which to do the layout.
  273.      * @return      the minimum dimensions needed to lay out the 
  274.      *                      subcomponents of the specified container.
  275.      * @see         java.awt.GridLayout#preferredLayoutSize
  276.      * @see         java.awt.Container#doLayout
  277.      */
  278.     public Dimension minimumLayoutSize(Container parent) {
  279.     Insets insets = parent.getInsets();
  280.     int ncomponents = parent.getComponentCount();
  281.     int nrows = rows;
  282.     int ncols = cols;
  283.  
  284.     if (nrows > 0) {
  285.         ncols = (ncomponents + nrows - 1) / nrows;
  286.     } else {
  287.         nrows = (ncomponents + ncols - 1) / ncols;
  288.     }
  289.     int w = 0;
  290.     int h = 0;
  291.     for (int i = 0 ; i < ncomponents ; i++) {
  292.         Component comp = parent.getComponent(i);
  293.         Dimension d = comp.getMinimumSize();
  294.         if (w < d.width) {
  295.         w = d.width;
  296.         }
  297.         if (h < d.height) {
  298.         h = d.height;
  299.         }
  300.     }
  301.     return new Dimension(insets.left + insets.right + ncols*w + (ncols-1)*hgap, 
  302.                  insets.top + insets.bottom + nrows*h + (nrows-1)*vgap);
  303.     }
  304.  
  305.     /** 
  306.      * Lays out the specified container using this layout. 
  307.      * <p>
  308.      * This method reshapes the components in the specified target 
  309.      * container in order to satisfy the constraints of the 
  310.      * <code>GridLayout</code> object. 
  311.      * <p>
  312.      * The grid layout manager determines the size of individual 
  313.      * components by dividing the free space in the container into 
  314.      * equal-sized portions according to the number of rows and columns 
  315.      * in the layout. The container's free space equals the container's 
  316.      * size minus any insets and any specified horizontal or vertical 
  317.      * gap. All components in a grid layout are given the same size. 
  318.      *  
  319.      * @param      target   the container in which to do the layout.
  320.      * @see        java.awt.Container
  321.      * @see        java.awt.Container#doLayout
  322.      */
  323.     public void layoutContainer(Container parent) {
  324.     Insets insets = parent.getInsets();
  325.     int ncomponents = parent.getComponentCount();
  326.     int nrows = rows;
  327.     int ncols = cols;
  328.  
  329.     if (ncomponents == 0) {
  330.         return;
  331.     }
  332.     if (nrows > 0) {
  333.         ncols = (ncomponents + nrows - 1) / nrows;
  334.     } else {
  335.         nrows = (ncomponents + ncols - 1) / ncols;
  336.     }
  337.     int w = parent.width - (insets.left + insets.right);
  338.     int h = parent.height - (insets.top + insets.bottom);
  339.     w = (w - (ncols - 1) * hgap) / ncols;
  340.     h = (h - (nrows - 1) * vgap) / nrows;
  341.  
  342.     for (int c = 0, x = insets.left ; c < ncols ; c++, x += w + hgap) {
  343.         for (int r = 0, y = insets.top ; r < nrows ; r++, y += h + vgap) {
  344.         int i = r * ncols + c;
  345.         if (i < ncomponents) {
  346.             parent.getComponent(i).setBounds(x, y, w, h);
  347.         }
  348.         }
  349.     }
  350.     }
  351.     
  352.     /**
  353.      * Returns the string representation of this grid layout's values.
  354.      * @return     a string representation of this grid layout.
  355.      */
  356.     public String toString() {
  357.     return getClass().getName() + "[hgap=" + hgap + ",vgap=" + vgap + 
  358.                            ",rows=" + rows + ",cols=" + cols + "]";
  359.     }
  360. }
  361.