home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / main.bin / GridLayout.java < prev    next >
Text File  |  1997-10-01  |  13KB  |  377 lines

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