home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1997 May / Pcwk0597.iso / sybase / starbuck / java.z / GridLayout.java < prev    next >
Text File  |  1996-05-03  |  6KB  |  188 lines

  1. /*
  2.  * @(#)GridLayout.java    1.11 95/12/14 Arthur van Hoff
  3.  *
  4.  * Copyright (c) 1994-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 java.awt;
  21.  
  22. /**
  23.  * A layout manager for a container that lays out grids.
  24.  *
  25.  *
  26.  * @version 1.11, 14 Dec 1995
  27.  * @author 
  28.  */
  29. public class GridLayout implements LayoutManager {
  30.     int hgap;
  31.     int vgap;
  32.     int rows;
  33.     int cols;
  34.  
  35.     /**
  36.      * Creates a grid layout with the specified rows and columns.
  37.      * @param rows the rows
  38.      * @param cols the columns
  39.      */
  40.     public GridLayout(int rows, int cols) {
  41.     this(rows, cols, 0, 0);
  42.     }
  43.  
  44.     /**
  45.      * Creates a grid layout with the specified rows, columns,
  46.      * horizontal gap, and vertical gap.
  47.      * @param rows the rows; zero means 'any number.'
  48.      * @param cols the columns; zero means 'any number.'  Only one of 'rows'
  49.      * and 'cols' can be zero, not both.
  50.      * @param hgap the horizontal gap variable
  51.      * @param vgap the vertical gap variable
  52.      * @exception IllegalArgumentException If the rows and columns are invalid.
  53.      */
  54.     public GridLayout(int rows, int cols, int hgap, int vgap) {
  55.     if ((rows == 0) && (cols == 0)) {
  56.         throw new IllegalArgumentException("invalid rows,cols");
  57.     }
  58.  
  59.     this.rows = rows;
  60.     this.cols = cols;
  61.     this.hgap = hgap;
  62.     this.vgap = vgap;
  63.     }
  64.  
  65.     /**
  66.      * Adds the specified component with the specified name to the layout.
  67.      * @param name the name of the component
  68.      * @param comp the component to be added
  69.      */
  70.     public void addLayoutComponent(String name, Component comp) {
  71.     }
  72.  
  73.     /**
  74.      * Removes the specified component from the layout. Does not apply.
  75.      * @param comp the component to be removed
  76.      */
  77.     public void removeLayoutComponent(Component comp) {
  78.     }
  79.  
  80.     /** 
  81.      * Returns the preferred dimensions for this layout given the components
  82.      * int the specified panel.
  83.      * @param parent the component which needs to be laid out 
  84.      * @see #minimumLayoutSize
  85.      */
  86.     public Dimension preferredLayoutSize(Container parent) {
  87.     Insets insets = parent.insets();
  88.     int ncomponents = parent.countComponents();
  89.     int nrows = rows;
  90.     int ncols = cols;
  91.  
  92.     if (nrows > 0) {
  93.         ncols = (ncomponents + nrows - 1) / nrows;
  94.     } else {
  95.         nrows = (ncomponents + ncols - 1) / ncols;
  96.     }
  97.     int w = 0;
  98.     int h = 0;
  99.     for (int i = 0 ; i < ncomponents ; i++) {
  100.         Component comp = parent.getComponent(i);
  101.         Dimension d = comp.preferredSize();
  102.         if (w < d.width) {
  103.         w = d.width;
  104.         }
  105.         if (h < d.height) {
  106.         h = d.height;
  107.         }
  108.     }
  109.     return new Dimension(insets.left + insets.right + ncols*w + (cols-1)*hgap, 
  110.                  insets.top + insets.bottom + nrows*h + (rows-1)*vgap);
  111.     }
  112.  
  113.     /**
  114.      * Returns the minimum dimensions needed to layout the components 
  115.      * contained in the specified panel.
  116.      * @param parent the component which needs to be laid out 
  117.      * @see #preferredLayoutSize
  118.      */
  119.     public Dimension minimumLayoutSize(Container parent) {
  120.     Insets insets = parent.insets();
  121.     int ncomponents = parent.countComponents();
  122.     int nrows = rows;
  123.     int ncols = cols;
  124.  
  125.     if (nrows > 0) {
  126.         ncols = (ncomponents + nrows - 1) / nrows;
  127.     } else {
  128.         nrows = (ncomponents + ncols - 1) / ncols;
  129.     }
  130.     int w = 0;
  131.     int h = 0;
  132.     for (int i = 0 ; i < ncomponents ; i++) {
  133.         Component comp = parent.getComponent(i);
  134.         Dimension d = comp.minimumSize();
  135.         if (w < d.width) {
  136.         w = d.width;
  137.         }
  138.         if (h < d.height) {
  139.         h = d.height;
  140.         }
  141.     }
  142.     return new Dimension(insets.left + insets.right + ncols*w + (cols-1)*hgap, 
  143.                  insets.top + insets.bottom + nrows*h + (rows-1)*vgap);
  144.     }
  145.  
  146.     /** 
  147.      * Lays out the container in the specified panel.  
  148.      * @param parent the specified component being laid out
  149.      * @see Container
  150.      */
  151.     public void layoutContainer(Container parent) {
  152.     Insets insets = parent.insets();
  153.     int ncomponents = parent.countComponents();
  154.     int nrows = rows;
  155.     int ncols = cols;
  156.  
  157.     if (ncomponents == 0) {
  158.         return;
  159.     }
  160.     if (nrows > 0) {
  161.         ncols = (ncomponents + nrows - 1) / nrows;
  162.     } else {
  163.         nrows = (ncomponents + ncols - 1) / ncols;
  164.     }
  165.     int w = parent.width - (insets.left + insets.right);
  166.     int h = parent.height - (insets.top + insets.bottom);
  167.     w = (w - (ncols - 1) * hgap) / ncols;
  168.     h = (h - (nrows - 1) * vgap) / nrows;
  169.  
  170.     for (int c = 0, x = insets.left ; c < ncols ; c++, x += w + hgap) {
  171.         for (int r = 0, y = insets.top ; r < nrows ; r++, y += h + vgap) {
  172.         int i = r * ncols + c;
  173.         if (i < ncomponents) {
  174.             parent.getComponent(i).reshape(x, y, w, h);
  175.         }
  176.         }
  177.     }
  178.     }
  179.     
  180.     /**
  181.      * Returns the String representation of this GridLayout's values.
  182.      */
  183.     public String toString() {
  184.     return getClass().getName() + "[hgap=" + hgap + ",vgap=" + vgap + 
  185.                            ",rows=" + rows + ",cols=" + cols + "]";
  186.     }
  187. }
  188.