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

  1. /*
  2.  * @(#)GridBagLayout.java    1.27 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. package java.awt;
  15.  
  16. import java.util.Hashtable;
  17. import java.util.Vector;
  18.  
  19. class GridBagLayoutInfo implements java.io.Serializable {
  20.   int width, height;        /* number of cells horizontally, vertically */
  21.   int startx, starty;        /* starting point for layout */
  22.   int minWidth[];        /* largest minWidth in each column */
  23.   int minHeight[];        /* largest minHeight in each row */
  24.   double weightX[];        /* largest weight in each column */
  25.   double weightY[];        /* largest weight in each row */
  26.  
  27.   GridBagLayoutInfo () {
  28.     minWidth = new int[GridBagLayout.MAXGRIDSIZE];
  29.     minHeight = new int[GridBagLayout.MAXGRIDSIZE];
  30.     weightX = new double[GridBagLayout.MAXGRIDSIZE];
  31.     weightY = new double[GridBagLayout.MAXGRIDSIZE];
  32.   }
  33. }
  34.  
  35. /**
  36.  * The <code>GridBagLayout</code> class is a flexible layout 
  37.  * manager that aligns components vertically and horizontally,
  38.  * without requiring that the components be of the same size.
  39.  * Each <code>GridBagLayout</code> object maintains a dynamic 
  40.  * rectangular grid of cells, with each component occupying 
  41.  * one or more cells, called its <em>display area</em>.
  42.  * <p>
  43.  * Each component managed by a grid bag layout is associated 
  44.  * with an instance of 
  45.  * <a href="java.awt.GridBagConstraints.html"><code>GridBagConstraints</code></a> 
  46.  * that specifies how the component is laid out within its display area.
  47.  * <p>
  48.  * How a <code>GridBagLayout</code> object places a set of components
  49.  * depends on the <code>GridBagConstraints</code> object associated 
  50.  * with each component, and on the minimum size 
  51.  * and the preferred size of the components' containers.
  52.  * <p>
  53.  * To use a grid bag layout effectively, you must customize one or more 
  54.  * of the <code>GridBagConstraints</code> objects that are associated 
  55.  * with its components. You customize a <code>GridBagConstraints</code> 
  56.  * object by setting one or more of its instance variables:
  57.  * <p>
  58.  * <dl>
  59.  * <dt><a href="java.awt.GridBagConstraints.html#gridx"><code>gridx</code></a>,
  60.  * <a href="java.awt.GridBagConstraints.html#gridy"><code>gridy</code></a>
  61.  * <dd>Specifies the cell at the upper left of the component's display area,
  62.  * where the upper-left-most cell has address 
  63.  * <code>gridx = 0</code>, 
  64.  * <code>gridy = 0</code>. 
  65.  * Use <code>GridBagConstraints.RELATIVE</code> (the default value)
  66.  * to specify that the component be just placed
  67.  * just to the right of (for <code>gridx</code>)
  68.  * or just below (for <code>gridy</code>)
  69.  * the component that was added to the container
  70.  * just before this component was added.
  71.  * <dt><a href="java.awt.GridBagConstraints.html#gridwidth"><code>gridwidth</code></a>,
  72.  * <a href="java.awt.GridBagConstraints.html#gridheight"><code>gridheight</code></a>
  73.  * <dd>Specifies the number of cells in a row (for <code>gridwidth</code>)
  74.  * or column (for <code>gridheight</code>)
  75.  * in the component's display area.
  76.  * The default value is 1.
  77.  * Use <code>GridBagConstraints.REMAINDER</code> to specify 
  78.  * that the component be the last one in its row (for <code>gridwidth</code>)
  79.  * or column (for <code>gridheight</code>).
  80.  * Use <code>GridBagConstraints.RELATIVE</code> to specify 
  81.  * that the component be the next to last one
  82.  * in its row (for <code>gridwidth</code>) 
  83.  * or column (for <code>gridheight</code>).
  84.  * <dt><a href="java.awt.GridBagConstraints.html#fill"><code>fill</code></a>
  85.  * <dd>Used when the component's display area
  86.  * is larger than the component's requested size
  87.  * to determine whether (and how) to resize the component.
  88.  * Possible values are
  89.  * <code>GridBagConstraints.NONE</code> (the default),
  90.  * <code>GridBagConstraints.HORIZONTAL</code> 
  91.  * (make the component wide enough to fill its display area
  92.  * horizontally, but don't change its height),
  93.  * <code>GridBagConstraints.VERTICAL</code> 
  94.  * (make the component tall enough to fill its display area 
  95.  * vertically, but don't change its width), and 
  96.  * <code>GridBagConstraints.BOTH</code> 
  97.  * (make the component fill its display area entirely). 
  98.  * <dt><a href="java.awt.GridBagConstraints.html#ipadx"><code>ipadx</code></a>,
  99.  * <a href="java.awt.GridBagConstraints.html#ipady"><code>ipady</code></a>
  100.  * <dd>Specifies the component's internal padding within the layout, 
  101.  * how much to add to the minimum size of the component.
  102.  * The width of the component will be at least its minimum width 
  103.  * plus <code>(ipadx * 2)</code> pixels (since the padding 
  104.  * applies to both sides of the component). Similarly, the height of 
  105.  * the component will be at least the minimum height plus 
  106.  * <code>(ipady * 2)</code> pixels.
  107.  * <dt><a href="java.awt.GridBagConstraints.html#insets"><code>insets</code></a>
  108.  * <dd>Specifies the component's external padding, the minimum
  109.  * amount of space between the component and the edges of its display area.
  110.  * <dt><a href="java.awt.GridBagConstraints.html#anchor"><code>anchor</code></a>
  111.  * <dd>Used when the component is smaller than its display area
  112.  * to determine where (within the display area) to place the component.
  113.  * Valid values are 
  114.  * <code>GridBagConstraints.CENTER</code> (the default), 
  115.  * <code>GridBagConstraints.NORTH</code>, 
  116.  * <code>GridBagConstraints.NORTHEAST</code>, 
  117.  * <code>GridBagConstraints.EAST</code>, 
  118.  * <code>GridBagConstraints.SOUTHEAST</code>, 
  119.  * <code>GridBagConstraints.SOUTH</code>, 
  120.  * <code>GridBagConstraints.SOUTHWEST</code>, 
  121.  * <code>GridBagConstraints.WEST</code>, and 
  122.  * <code>GridBagConstraints.NORTHWEST</code>. 
  123.  * <dt><a href="java.awt.GridBagConstraints.html#weightx"><code>weightx</code></a>,
  124.  * <a href="java.awt.GridBagConstraints.html#weighty"><code>weighty</code></a>
  125.  * <dd>Used to determine how to distribute space, which is 
  126.  * important for specifying resizing behavior.
  127.  * Unless you specify a weight for at least one component 
  128.  * in a row (<code>weightx</code>) and column (<code>weighty</code>), 
  129.  * all the components clump together in the center of their container.
  130.  * This is because when the weight is zero (the default),
  131.  * the <code>GridBagLayout</code> object puts any extra space 
  132.  * between its grid of cells and the edges of the container.
  133.  * </dl>
  134.  * <p>
  135.  * The following figure shows ten components (all buttons)
  136.  * managed by a grid bag layout:
  137.  * <p>
  138.  * <img src="images-awt/GridBagLayout-1.gif" 
  139.  * ALIGN=center HSPACE=10 VSPACE=7>
  140.  * <p>
  141.  * Each of the ten components has the <code>fill</code> field 
  142.  * of its associated <code>GridBagConstraints</code> object 
  143.  * set to <code>GridBagConstraints.BOTH</code>.
  144.  * In addition, the components have the following non-default constraints:
  145.  * <p>
  146.  * <ul>
  147.  * <li>Button1, Button2, Button3: <code>weightx = 1.0</code> 
  148.  * <li>Button4: <code>weightx = 1.0</code>, 
  149.  * <code>gridwidth = GridBagConstraints.REMAINDER</code> 
  150.  * <li>Button5: <code>gridwidth = GridBagConstraints.REMAINDER</code> 
  151.  * <li>Button6: <code>gridwidth = GridBagConstraints.RELATIVE</code> 
  152.  * <li>Button7: <code>gridwidth = GridBagConstraints.REMAINDER</code> 
  153.  * <li>Button8: <code>gridheight = 2</code>, 
  154.  * <code>weighty = 1.0</code> 
  155.  * <li>Button9, Button 10: 
  156.  * <code>gridwidth = GridBagConstraints.REMAINDER</code> 
  157.  * </ul>
  158.  * <p>
  159.  * Here is the code that implements the example shown above:
  160.  * <p>
  161.  * <hr><blockquote><pre>
  162.  * import java.awt.*;
  163.  * import java.util.*;
  164.  * import java.applet.Applet;
  165.  * 
  166.  * public class GridBagEx1 extends Applet {
  167.  * 
  168.  *     protected void makebutton(String name,
  169.  *                               GridBagLayout gridbag,
  170.  *                               GridBagConstraints c) {
  171.  *         Button button = new Button(name);
  172.  *         gridbag.setConstraints(button, c);
  173.  *         add(button);
  174.  *     }
  175.  * 
  176.  *     public void init() {
  177.  *         GridBagLayout gridbag = new GridBagLayout();
  178.  *         GridBagConstraints c = new GridBagConstraints();
  179.  *  
  180.  *         setFont(new Font("Helvetica", Font.PLAIN, 14));
  181.  *         setLayout(gridbag);
  182.  *    
  183.  *         c.fill = GridBagConstraints.BOTH;
  184.  *         c.weightx = 1.0;
  185.  *         makebutton("Button1", gridbag, c);
  186.  *         makebutton("Button2", gridbag, c);
  187.  *         makebutton("Button3", gridbag, c);
  188.  *     
  189.  *            c.gridwidth = GridBagConstraints.REMAINDER; //end row
  190.  *         makebutton("Button4", gridbag, c);
  191.  *     
  192.  *         c.weightx = 0.0;           //reset to the default
  193.  *         makebutton("Button5", gridbag, c); //another row
  194.  *     
  195.  *        c.gridwidth = GridBagConstraints.RELATIVE; //next-to-last in row
  196.  *         makebutton("Button6", gridbag, c);
  197.  *     
  198.  *        c.gridwidth = GridBagConstraints.REMAINDER; //end row
  199.  *         makebutton("Button7", gridbag, c);
  200.  *     
  201.  *        c.gridwidth = 1;              //reset to the default
  202.  *        c.gridheight = 2;
  203.  *         c.weighty = 1.0;
  204.  *         makebutton("Button8", gridbag, c);
  205.  *     
  206.  *         c.weighty = 0.0;           //reset to the default
  207.  *        c.gridwidth = GridBagConstraints.REMAINDER; //end row
  208.  *        c.gridheight = 1;           //reset to the default
  209.  *         makebutton("Button9", gridbag, c);
  210.  *         makebutton("Button10", gridbag, c);
  211.  *     
  212.  *         setSize(300, 100);
  213.  *     }
  214.  *     
  215.  *     public static void main(String args[]) {
  216.  *        Frame f = new Frame("GridBag Layout Example");
  217.  *        GridBagEx1 ex1 = new GridBagEx1();
  218.  *     
  219.  *        ex1.init();
  220.  *     
  221.  *        f.add("Center", ex1);
  222.  *        f.pack();
  223.  *        f.setSize(f.getPreferredSize());
  224.  *        f.show();
  225.  *     }
  226.  * }
  227.  * </pre></blockquote><hr>
  228.  * <p>
  229.  * @version 1.5, 16 Nov 1995
  230.  * @author Doug Stein
  231.  * @see       java.awt.GridBagConstraints
  232.  * @since     JDK1.0
  233.  */
  234. public class GridBagLayout implements LayoutManager2,
  235.                       java.io.Serializable {
  236.  
  237.     /**
  238.      * The maximum number of grid positions (both horizontally and 
  239.      * vertically) that can be laid out by the grid bag layout. 
  240.      */
  241.   protected static final int MAXGRIDSIZE = 512;
  242.  
  243.     /**
  244.      * The smallest grid that can be laid out by the grid bag layout. 
  245.      */
  246.   protected static final int MINSIZE = 1;
  247.   protected static final int PREFERREDSIZE = 2;
  248.  
  249.   protected Hashtable comptable;
  250.   protected GridBagConstraints defaultConstraints;
  251.   protected GridBagLayoutInfo layoutInfo;
  252.  
  253.   public int columnWidths[];
  254.   public int rowHeights[];
  255.   public double columnWeights[];
  256.   public double rowWeights[];
  257.  
  258.   /**
  259.    * Creates a grid bag layout manager.
  260.    */
  261.   public GridBagLayout () {
  262.     comptable = new Hashtable();
  263.     defaultConstraints = new GridBagConstraints();
  264.   }
  265.  
  266.   /**
  267.    * Sets the constraints for the specified component in this layout.
  268.    * @param       comp the component to be modified.
  269.    * @param       constraints the constraints to be applied.
  270.    */
  271.   public void setConstraints(Component comp, GridBagConstraints constraints) {
  272.     comptable.put(comp, constraints.clone());
  273.   }
  274.  
  275.   /**
  276.    * Gets the constraints for the specified component.  A copy of
  277.    * the actual <code>GridBagConstraints</code> object is returned.
  278.    * @param       comp the component to be queried.
  279.    * @return      the constraint for the specified component in this 
  280.    *                  grid bag layout; a copy of the actual constraint 
  281.    *                  object is returned.
  282.    */
  283.   public GridBagConstraints getConstraints(Component comp) {
  284.     GridBagConstraints constraints = (GridBagConstraints)comptable.get(comp);
  285.     if (constraints == null) {
  286.       setConstraints(comp, defaultConstraints);
  287.       constraints = (GridBagConstraints)comptable.get(comp);
  288.     }
  289.     return (GridBagConstraints)constraints.clone();
  290.   }
  291.  
  292.   /**
  293.    * Retrieves the constraints for the specified component.  
  294.    * The return value is not a copy, but is the actual 
  295.    * <code>GridBagConstraints</code> object used by the layout mechanism. 
  296.    * @param       comp the component to be queried
  297.    * @return      the contraints for the specified component.
  298.    */
  299.   protected GridBagConstraints lookupConstraints(Component comp) {
  300.     GridBagConstraints constraints = (GridBagConstraints)comptable.get(comp);
  301.     if (constraints == null) {
  302.       setConstraints(comp, defaultConstraints);
  303.       constraints = (GridBagConstraints)comptable.get(comp);
  304.     }
  305.     return constraints;
  306.   }
  307.  
  308.     /**
  309.      * Determines the origin of the layout grid. 
  310.      * Most applications do not call this method directly.
  311.      * @return     the origin of the cell in the top-left 
  312.      *                    corner of the layout grid.
  313.      * @since      JDK1.1
  314.      */
  315.   public Point getLayoutOrigin () {
  316.     Point origin = new Point(0,0);
  317.     if (layoutInfo != null) {
  318.       origin.x = layoutInfo.startx;
  319.       origin.y = layoutInfo.starty;
  320.     }
  321.     return origin;
  322.   }
  323.  
  324.     /**
  325.      * Determines column widths and row heights for the layout grid.
  326.      * <p>
  327.      * Most applications do not call this method directly.
  328.      * @return     an array of two arrays, containing the widths 
  329.      *                       of the layout columns and
  330.      *                       the heights of the layout rows.
  331.      * @since      JDK1.1
  332.      */
  333.   public int [][] getLayoutDimensions () {
  334.     if (layoutInfo == null)
  335.       return new int[2][0];
  336.  
  337.     int dim[][] = new int [2][];
  338.     dim[0] = new int[layoutInfo.width];
  339.     dim[1] = new int[layoutInfo.height];
  340.  
  341.     System.arraycopy(layoutInfo.minWidth, 0, dim[0], 0, layoutInfo.width);
  342.     System.arraycopy(layoutInfo.minHeight, 0, dim[1], 0, layoutInfo.height);
  343.  
  344.     return dim;
  345.   }
  346.  
  347.     /**
  348.      * Determines the weights of the layout grid's columns and rows.
  349.      * Weights are used to calculate how much a given column or row
  350.      * stretches beyond its preferred size, if the layout has extra
  351.      * room to fill.
  352.      * <p>
  353.      * Most applications do not call this method directly.
  354.      * @return      an array of two arrays, representing the 
  355.      *                    horizontal weights of the layout columns 
  356.      *                    and the vertical weights of the layout rows.
  357.      * @since       JDK1.1
  358.      */
  359.   public double [][] getLayoutWeights () {
  360.     if (layoutInfo == null)
  361.       return new double[2][0];
  362.  
  363.     double weights[][] = new double [2][];
  364.     weights[0] = new double[layoutInfo.width];
  365.     weights[1] = new double[layoutInfo.height];
  366.  
  367.     System.arraycopy(layoutInfo.weightX, 0, weights[0], 0, layoutInfo.width);
  368.     System.arraycopy(layoutInfo.weightY, 0, weights[1], 0, layoutInfo.height);
  369.  
  370.     return weights;
  371.   }
  372.  
  373.     /**
  374.      * Determines which cell in the layout grid contains the point
  375.      * specified by <code>(x, y)</code>. Each cell is identified 
  376.      * by its column index (ranging from 0 to the number of columns 
  377.      * minus 1) and its row index (ranging from 0 to the number of 
  378.      * rows minus 1).  
  379.      * <p>
  380.      * If the <code>(x, y)</code> point lies 
  381.      * outside the grid, the following rules are used.  
  382.      * The column index is returned as zero if <code>x</code> lies to the
  383.      * left of the layout, and as the number of columns if <code>x</code> lies
  384.      * to the right of the layout. The row index is returned as zero
  385.      * if <code>y</code> lies above the layout, 
  386.      * and as the number of rows if <code>y</code> lies
  387.      * below the layout.
  388.      * @param      x    the <i>x</i> coordinate of a point.
  389.      * @param      y    the <i>y</i> coordinate of a point.
  390.      * @return     an ordered pair of indexes that indicate which cell 
  391.      *             in the layout grid contains the point 
  392.      *             (<i>x</i>, <i>y</i>).
  393.      * @since      JDK1.1
  394.      */
  395.   public Point location(int x, int y) {
  396.     Point loc = new Point(0,0);
  397.     int i, d;
  398.  
  399.     if (layoutInfo == null)
  400.       return loc;
  401.  
  402.     d = layoutInfo.startx;
  403.     for (i=0; i<layoutInfo.width; i++) {
  404.       d += layoutInfo.minWidth[i];
  405.       if (d > x)
  406.     break;
  407.     }
  408.     loc.x = i;
  409.  
  410.     d = layoutInfo.starty;
  411.     for (i=0; i<layoutInfo.height; i++) {
  412.       d += layoutInfo.minHeight[i];
  413.       if (d > y)
  414.     break;
  415.     }
  416.     loc.y = i;
  417.  
  418.     return loc;
  419.   }
  420.  
  421.   /**
  422.    * Adds the specified component with the specified name to the layout.
  423.    * @param      name         the name of the component.
  424.    * @param      comp         the component to be added.
  425.    */
  426.   public void addLayoutComponent(String name, Component comp) {
  427.   }
  428.  
  429.     /**
  430.      * Adds the specified component to the layout, using the specified
  431.      * constraint object.
  432.      * @param      comp         the component to be added.
  433.      * @param      constraints  an object that determines how 
  434.      *                              the component is added to the layout.
  435.      */
  436.     public void addLayoutComponent(Component comp, Object constraints) {
  437.       if (constraints instanceof GridBagConstraints) {
  438.         setConstraints(comp, (GridBagConstraints)constraints);
  439.     } else if (constraints != null) {
  440.         throw new IllegalArgumentException("cannot add to layout: constraint must be a GridBagConstraint");
  441.     }
  442.     }
  443.  
  444.   /**
  445.      * Removes the specified component from this layout. 
  446.      * <p>
  447.      * Most applications do not call this method directly.  
  448.      * @param    comp   the component to be removed.
  449.      * @see      java.awt.Container#remove(java.awt.Component)
  450.      * @see      java.awt.Container#removeAll()
  451.    */
  452.   public void removeLayoutComponent(Component comp) {
  453.   }
  454.  
  455.   /** 
  456.      * Determines the preferred size of the <code>target</code> 
  457.      * container using this grid bag layout. 
  458.      * <p>
  459.      * Most applications do not call this method directly.
  460.      * @param     target   the container in which to do the layout.
  461.      * @see       java.awt.Container#getPreferredSize
  462.    */
  463.   public Dimension preferredLayoutSize(Container parent) {
  464.     GridBagLayoutInfo info = GetLayoutInfo(parent, PREFERREDSIZE);
  465.     return GetMinSize(parent, info);
  466.   }
  467.  
  468.   /**
  469.      * Determines the minimum size of the <code>target</code> container 
  470.      * using this grid bag layout. 
  471.      * <p>
  472.      * Most applications do not call this method directly. 
  473.      * @param     target   the container in which to do the layout.
  474.      * @see       java.awt.Container#doLayout
  475.    */
  476.   public Dimension minimumLayoutSize(Container parent) {
  477.     GridBagLayoutInfo info = GetLayoutInfo(parent, MINSIZE);
  478.     return GetMinSize(parent, info);
  479.   }
  480.  
  481.     /**
  482.      * Returns the maximum dimensions for this layout given the components
  483.      * in the specified target container.
  484.      * @param target the component which needs to be laid out
  485.      * @see Container
  486.      * @see #minimumLayoutSize
  487.      * @see #preferredLayoutSize
  488.      */
  489.     public Dimension maximumLayoutSize(Container target) {
  490.     return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
  491.     }
  492.  
  493.     /**
  494.      * Returns the alignment along the x axis.  This specifies how
  495.      * the component would like to be aligned relative to other 
  496.      * components.  The value should be a number between 0 and 1
  497.      * where 0 represents alignment along the origin, 1 is aligned
  498.      * the furthest away from the origin, 0.5 is centered, etc.
  499.      */
  500.     public float getLayoutAlignmentX(Container parent) {
  501.     return 0.5f;
  502.     }
  503.  
  504.     /**
  505.      * Returns the alignment along the y axis.  This specifies how
  506.      * the component would like to be aligned relative to other 
  507.      * components.  The value should be a number between 0 and 1
  508.      * where 0 represents alignment along the origin, 1 is aligned
  509.      * the furthest away from the origin, 0.5 is centered, etc.
  510.      */
  511.     public float getLayoutAlignmentY(Container parent) {
  512.     return 0.5f;
  513.     }
  514.  
  515.     /**
  516.      * Invalidates the layout, indicating that if the layout manager
  517.      * has cached information it should be discarded.
  518.      */
  519.     public void invalidateLayout(Container target) {
  520.     }
  521.                       
  522.   /** 
  523.    * Lays out the specified container using this grid bag layout.
  524.    * This method reshapes components in the specified container in 
  525.    * order to satisfy the contraints of this <code>GridBagLayout</code> 
  526.    * object.
  527.    * <p>
  528.    * Most applications do not call this method directly.
  529.    * @param parent the container in which to do the layout.
  530.    * @see java.awt.Container
  531.    * @see java.awt.Container#doLayout
  532.    */
  533.   public void layoutContainer(Container parent) {
  534.     ArrangeGrid(parent);
  535.   }
  536.  
  537.   /**
  538.      * Returns a string representation of this grid bag layout's values.
  539.      * @return     a string representation of this grid bag layout.
  540.    */
  541.   public String toString() {
  542.     return getClass().getName();
  543.   }
  544.  
  545.   /**
  546.    * Print the layout information.  Useful for debugging.
  547.    */
  548.  
  549.   /* DEBUG
  550.    *
  551.    *  protected void DumpLayoutInfo(GridBagLayoutInfo s) {
  552.    *    int x;
  553.    *
  554.    *    System.out.println("Col\tWidth\tWeight");
  555.    *    for (x=0; x<s.width; x++) {
  556.    *      System.out.println(x + "\t" +
  557.    *             s.minWidth[x] + "\t" +
  558.    *             s.weightX[x]);
  559.    *    }
  560.    *    System.out.println("Row\tHeight\tWeight");
  561.    *    for (x=0; x<s.height; x++) {
  562.    *      System.out.println(x + "\t" +
  563.    *             s.minHeight[x] + "\t" +
  564.    *             s.weightY[x]);
  565.    *    }
  566.    *  }
  567.    */
  568.  
  569.   /**
  570.    * Print the layout constraints.  Useful for debugging.
  571.    */
  572.  
  573.   /* DEBUG
  574.    *
  575.    *  protected void DumpConstraints(GridBagConstraints constraints) {
  576.    *    System.out.println(
  577.    *               "wt " +
  578.    *               constraints.weightx +
  579.    *               " " +
  580.    *               constraints.weighty +
  581.    *               ", " +
  582.    *
  583.    *               "box " +
  584.    *               constraints.gridx +
  585.    *               " " +
  586.    *               constraints.gridy +
  587.    *               " " +
  588.    *               constraints.gridwidth +
  589.    *               " " +
  590.    *               constraints.gridheight +
  591.    *               ", " +
  592.    *
  593.    *               "min " +
  594.    *               constraints.minWidth +
  595.    *               " " +
  596.    *               constraints.minHeight +
  597.    *               ", " +
  598.    *
  599.    *               "pad " +
  600.    *               constraints.insets.bottom +
  601.    *               " " +
  602.    *               constraints.insets.left +
  603.    *               " " +
  604.    *               constraints.insets.right +
  605.    *               " " +
  606.    *               constraints.insets.top +
  607.    *               " " +
  608.    *               constraints.ipadx +
  609.    *               " " +
  610.    *               constraints.ipady);
  611.    *  }
  612.    */
  613.  
  614.   /*
  615.    * Fill in an instance of the above structure for the current set
  616.    * of managed children.  This requires three passes through the
  617.    * set of children:
  618.    *
  619.    * 1) Figure out the dimensions of the layout grid
  620.    * 2) Determine which cells the components occupy
  621.    * 3) Distribute the weights and min sizes amoung the rows/columns.
  622.    *
  623.    * This also caches the minsizes for all the children when they are
  624.    * first encountered (so subsequent loops don't need to ask again).
  625.    */
  626.   
  627.   protected GridBagLayoutInfo GetLayoutInfo(Container parent, int sizeflag) {
  628.     GridBagLayoutInfo r = new GridBagLayoutInfo();
  629.     Component comp;
  630.     GridBagConstraints constraints;
  631.     Dimension d;
  632.     Component components[] = parent.getComponents();
  633.  
  634.     int compindex, i, j, k, px, py, pixels_diff, nextSize;
  635.     int curX, curY, curWidth, curHeight, curRow, curCol;
  636.     double weight_diff, weight, start, size;
  637.     int xMax[], yMax[];
  638.  
  639.     /*
  640.      * Pass #1
  641.      *
  642.      * Figure out the dimensions of the layout grid (use a value of 1 for
  643.      * zero or negative widths and heights).
  644.      */
  645.     
  646.     r.width = r.height = 0;
  647.     curRow = curCol = -1;
  648.     xMax = new int[MAXGRIDSIZE];
  649.     yMax = new int[MAXGRIDSIZE];
  650.  
  651.     for (compindex = 0 ; compindex < components.length ; compindex++) {
  652.       comp = components[compindex];
  653.       if (!comp.isVisible())
  654.     continue;
  655.       constraints = lookupConstraints(comp);
  656.       
  657.       curX = constraints.gridx;
  658.       curY = constraints.gridy;
  659.       curWidth = constraints.gridwidth;
  660.       if (curWidth <= 0)
  661.     curWidth = 1;
  662.       curHeight = constraints.gridheight;
  663.       if (curHeight <= 0)
  664.     curHeight = 1;
  665.       
  666.       /* If x or y is negative, then use relative positioning: */
  667.       if (curX < 0 && curY < 0) {
  668.     if (curRow >= 0)
  669.       curY = curRow;
  670.     else if (curCol >= 0)
  671.       curX = curCol;
  672.     else
  673.       curY = 0;
  674.       }
  675.       if (curX < 0) {
  676.     px = 0;
  677.     for (i = curY; i < (curY + curHeight); i++)
  678.       px = Math.max(px, xMax[i]);
  679.     
  680.     curX = px - curX - 1;
  681.     if(curX < 0)
  682.       curX = 0;
  683.       }
  684.       else if (curY < 0) {
  685.     py = 0;
  686.     for (i = curX; i < (curX + curWidth); i++)
  687.       py = Math.max(py, yMax[i]);
  688.     
  689.     curY = py - curY - 1;
  690.     if(curY < 0)
  691.       curY = 0;
  692.       }
  693.       
  694.       /* Adjust the grid width and height */
  695.       for (px = curX + curWidth; r.width < px; r.width++);
  696.       for (py = curY + curHeight; r.height < py; r.height++);
  697.       
  698.       /* Adjust the xMax and yMax arrays */
  699.       for (i = curX; i < (curX + curWidth); i++) { yMax[i] = py; }
  700.       for (i = curY; i < (curY + curHeight); i++) { xMax[i] = px; }
  701.       
  702.       /* Cache the current slave's size. */
  703.       if (sizeflag == PREFERREDSIZE)
  704.     d = comp.getPreferredSize();
  705.       else
  706.     d = comp.getMinimumSize();
  707.       constraints.minWidth = d.width;
  708.       constraints.minHeight = d.height;
  709.       
  710.       /* Zero width and height must mean that this is the last item (or
  711.        * else something is wrong). */
  712.       if (constraints.gridheight == 0 && constraints.gridwidth == 0)
  713.     curRow = curCol = -1;
  714.       
  715.       /* Zero width starts a new row */
  716.       if (constraints.gridheight == 0 && curRow < 0)
  717.     curCol = curX + curWidth;
  718.       
  719.       /* Zero height starts a new column */
  720.       else if (constraints.gridwidth == 0 && curCol < 0)
  721.     curRow = curY + curHeight;
  722.     }
  723.     
  724.     /*
  725.      * Apply minimum row/column dimensions
  726.      */
  727.     if (columnWidths != null && r.width < columnWidths.length)
  728.       r.width = columnWidths.length;
  729.     if (rowHeights != null && r.height < rowHeights.length)
  730.       r.height = rowHeights.length;
  731.  
  732.     /*
  733.      * Pass #2
  734.      *
  735.      * Negative values for gridX are filled in with the current x value.
  736.      * Negative values for gridY are filled in with the current y value.
  737.      * Negative or zero values for gridWidth and gridHeight end the current
  738.      *  row or column, respectively.
  739.      */
  740.     
  741.     curRow = curCol = -1;
  742.     xMax = new int[MAXGRIDSIZE];
  743.     yMax = new int[MAXGRIDSIZE];
  744.     
  745.     for (compindex = 0 ; compindex < components.length ; compindex++) {
  746.       comp = components[compindex];
  747.       if (!comp.isVisible())
  748.     continue;
  749.       constraints = lookupConstraints(comp);
  750.       
  751.       curX = constraints.gridx;
  752.       curY = constraints.gridy;
  753.       curWidth = constraints.gridwidth;
  754.       curHeight = constraints.gridheight;
  755.       
  756.       /* If x or y is negative, then use relative positioning: */
  757.       if (curX < 0 && curY < 0) {
  758.     if(curRow >= 0)
  759.       curY = curRow;
  760.     else if(curCol >= 0)
  761.       curX = curCol;
  762.     else
  763.       curY = 0;
  764.       }
  765.       
  766.       if (curX < 0) {
  767.     if (curHeight <= 0) {
  768.       curHeight += r.height - curY;
  769.       if (curHeight < 1)
  770.         curHeight = 1;
  771.     }
  772.     
  773.     px = 0;
  774.     for (i = curY; i < (curY + curHeight); i++)
  775.       px = Math.max(px, xMax[i]);
  776.     
  777.     curX = px - curX - 1;
  778.     if(curX < 0)
  779.       curX = 0;
  780.       }
  781.       else if (curY < 0) {
  782.     if (curWidth <= 0) {
  783.       curWidth += r.width - curX;
  784.       if (curWidth < 1)
  785.         curWidth = 1;
  786.     }
  787.     
  788.     py = 0;
  789.     for (i = curX; i < (curX + curWidth); i++)
  790.       py = Math.max(py, yMax[i]);
  791.     
  792.     curY = py - curY - 1;
  793.     if(curY < 0)
  794.       curY = 0;
  795.       }
  796.       
  797.       if (curWidth <= 0) {
  798.     curWidth += r.width - curX;
  799.     if (curWidth < 1)
  800.       curWidth = 1;
  801.       }
  802.       
  803.       if (curHeight <= 0) {
  804.     curHeight += r.height - curY;
  805.     if (curHeight < 1)
  806.       curHeight = 1;
  807.       }
  808.       
  809.       px = curX + curWidth;
  810.       py = curY + curHeight;
  811.       
  812.       for (i = curX; i < (curX + curWidth); i++) { yMax[i] = py; }
  813.       for (i = curY; i < (curY + curHeight); i++) { xMax[i] = px; }
  814.       
  815.       /* Make negative sizes start a new row/column */
  816.       if (constraints.gridheight == 0 && constraints.gridwidth == 0)
  817.     curRow = curCol = -1;
  818.       if (constraints.gridheight == 0 && curRow < 0)
  819.     curCol = curX + curWidth;
  820.       else if (constraints.gridwidth == 0 && curCol < 0)
  821.         curRow = curY + curHeight;
  822.       
  823.       /* Assign the new values to the gridbag slave */
  824.       constraints.tempX = curX;
  825.       constraints.tempY = curY;
  826.       constraints.tempWidth = curWidth;
  827.       constraints.tempHeight = curHeight;
  828.     }
  829.     
  830.     /*
  831.      * Apply minimum row/column dimensions and weights
  832.      */
  833.     if (columnWidths != null)
  834.       System.arraycopy(columnWidths, 0, r.minWidth, 0, columnWidths.length);
  835.     if (rowHeights != null)
  836.       System.arraycopy(rowHeights, 0, r.minHeight, 0, rowHeights.length);
  837.     if (columnWeights != null)
  838.       System.arraycopy(columnWeights, 0, r.weightX, 0, columnWeights.length);
  839.     if (rowWeights != null)
  840.       System.arraycopy(rowWeights, 0, r.weightY, 0, rowWeights.length);
  841.  
  842.     /*
  843.      * Pass #3
  844.      *
  845.      * Distribute the minimun widths and weights:
  846.      */
  847.     
  848.     nextSize = Integer.MAX_VALUE;
  849.     
  850.     for (i = 1;
  851.      i != Integer.MAX_VALUE;
  852.      i = nextSize, nextSize = Integer.MAX_VALUE) {
  853.       for (compindex = 0 ; compindex < components.length ; compindex++) {
  854.     comp = components[compindex];
  855.     if (!comp.isVisible())
  856.       continue;
  857.     constraints = lookupConstraints(comp);
  858.       
  859.     if (constraints.tempWidth == i) {
  860.       px = constraints.tempX + constraints.tempWidth; /* right column */
  861.       
  862.       /* 
  863.        * Figure out if we should use this slave\'s weight.  If the weight
  864.        * is less than the total weight spanned by the width of the cell,
  865.        * then discard the weight.  Otherwise split the difference
  866.        * according to the existing weights.
  867.        */
  868.       
  869.       weight_diff = constraints.weightx;
  870.       for (k = constraints.tempX; k < px; k++)
  871.         weight_diff -= r.weightX[k];
  872.       if (weight_diff > 0.0) {
  873.         weight = 0.0;
  874.         for (k = constraints.tempX; k < px; k++)
  875.           weight += r.weightX[k];
  876.         for (k = constraints.tempX; weight > 0.0 && k < px; k++) {
  877.           double wt = r.weightX[k];
  878.           double dx = (wt * weight_diff) / weight;
  879.           r.weightX[k] += dx;
  880.           weight_diff -= dx;
  881.           weight -= wt;
  882.         }
  883.         /* Assign the remainder to the rightmost cell */
  884.         r.weightX[px-1] += weight_diff;
  885.       }
  886.       
  887.       /*
  888.        * Calculate the minWidth array values.
  889.        * First, figure out how wide the current slave needs to be.
  890.        * Then, see if it will fit within the current minWidth values.
  891.        * If it will not fit, add the difference according to the
  892.        * weightX array.
  893.        */
  894.       
  895.       pixels_diff =
  896.         constraints.minWidth + constraints.ipadx +
  897.         constraints.insets.left + constraints.insets.right;
  898.  
  899.       for (k = constraints.tempX; k < px; k++)
  900.         pixels_diff -= r.minWidth[k];
  901.       if (pixels_diff > 0) {
  902.         weight = 0.0;
  903.         for (k = constraints.tempX; k < px; k++)
  904.           weight += r.weightX[k];
  905.         for (k = constraints.tempX; weight > 0.0 && k < px; k++) {
  906.           double wt = r.weightX[k];
  907.           int dx = (int)((wt * ((double)pixels_diff)) / weight);
  908.           r.minWidth[k] += dx;
  909.           pixels_diff -= dx;
  910.           weight -= wt;
  911.         }
  912.         /* Any leftovers go into the rightmost cell */
  913.         r.minWidth[px-1] += pixels_diff;
  914.       }
  915.     }
  916.     else if (constraints.tempWidth > i && constraints.tempWidth < nextSize)
  917.       nextSize = constraints.tempWidth;
  918.     
  919.     
  920.     if (constraints.tempHeight == i) {
  921.       py = constraints.tempY + constraints.tempHeight; /* bottom row */
  922.       
  923.       /* 
  924.        * Figure out if we should use this slave\'s weight.  If the weight
  925.        * is less than the total weight spanned by the height of the cell,
  926.        * then discard the weight.  Otherwise split it the difference
  927.        * according to the existing weights.
  928.        */
  929.       
  930.       weight_diff = constraints.weighty;
  931.       for (k = constraints.tempY; k < py; k++)
  932.         weight_diff -= r.weightY[k];
  933.       if (weight_diff > 0.0) {
  934.         weight = 0.0;
  935.         for (k = constraints.tempY; k < py; k++)
  936.           weight += r.weightY[k];
  937.         for (k = constraints.tempY; weight > 0.0 && k < py; k++) {
  938.           double wt = r.weightY[k];
  939.           double dy = (wt * weight_diff) / weight;
  940.           r.weightY[k] += dy;
  941.           weight_diff -= dy;
  942.           weight -= wt;
  943.         }
  944.         /* Assign the remainder to the bottom cell */
  945.         r.weightY[py-1] += weight_diff;
  946.       }
  947.       
  948.       /*
  949.        * Calculate the minHeight array values.
  950.        * First, figure out how tall the current slave needs to be.
  951.        * Then, see if it will fit within the current minHeight values.
  952.        * If it will not fit, add the difference according to the
  953.        * weightY array.
  954.        */
  955.       
  956.       pixels_diff =
  957.         constraints.minHeight + constraints.ipady +
  958.         constraints.insets.top + constraints.insets.bottom;
  959.       for (k = constraints.tempY; k < py; k++)
  960.         pixels_diff -= r.minHeight[k];
  961.       if (pixels_diff > 0) {
  962.         weight = 0.0;
  963.         for (k = constraints.tempY; k < py; k++)
  964.           weight += r.weightY[k];
  965.         for (k = constraints.tempY; weight > 0.0 && k < py; k++) {
  966.           double wt = r.weightY[k];
  967.           int dy = (int)((wt * ((double)pixels_diff)) / weight);
  968.           r.minHeight[k] += dy;
  969.           pixels_diff -= dy;
  970.           weight -= wt;
  971.         }
  972.         /* Any leftovers go into the bottom cell */
  973.         r.minHeight[py-1] += pixels_diff;
  974.       }
  975.     }
  976.     else if (constraints.tempHeight > i &&
  977.          constraints.tempHeight < nextSize)
  978.       nextSize = constraints.tempHeight;
  979.       }
  980.     }
  981.  
  982.     return r;
  983.   }
  984.   
  985.   /*
  986.    * Adjusts the x, y, width, and height fields to the correct
  987.    * values depending on the constraint geometry and pads.
  988.    */
  989.   protected void AdjustForGravity(GridBagConstraints constraints,
  990.                   Rectangle r) {
  991.     int diffx, diffy;
  992.  
  993.     r.x += constraints.insets.left;
  994.     r.width -= (constraints.insets.left + constraints.insets.right);
  995.     r.y += constraints.insets.top;
  996.     r.height -= (constraints.insets.top + constraints.insets.bottom);
  997.     
  998.     diffx = 0;
  999.     if ((constraints.fill != GridBagConstraints.HORIZONTAL &&
  1000.      constraints.fill != GridBagConstraints.BOTH)
  1001.     && (r.width > (constraints.minWidth + constraints.ipadx))) {
  1002.       diffx = r.width - (constraints.minWidth + constraints.ipadx);
  1003.       r.width = constraints.minWidth + constraints.ipadx;
  1004.     }
  1005.     
  1006.     diffy = 0;
  1007.     if ((constraints.fill != GridBagConstraints.VERTICAL &&
  1008.      constraints.fill != GridBagConstraints.BOTH)
  1009.     && (r.height > (constraints.minHeight + constraints.ipady))) {
  1010.       diffy = r.height - (constraints.minHeight + constraints.ipady);
  1011.       r.height = constraints.minHeight + constraints.ipady;
  1012.     }
  1013.     
  1014.     switch (constraints.anchor) {
  1015.     case GridBagConstraints.CENTER:
  1016.       r.x += diffx/2;
  1017.       r.y += diffy/2;
  1018.       break;
  1019.     case GridBagConstraints.NORTH:
  1020.       r.x += diffx/2;
  1021.       break;
  1022.     case GridBagConstraints.NORTHEAST:
  1023.       r.x += diffx;
  1024.       break;
  1025.     case GridBagConstraints.EAST:
  1026.       r.x += diffx;
  1027.       r.y += diffy/2;
  1028.       break;
  1029.     case GridBagConstraints.SOUTHEAST:
  1030.       r.x += diffx;
  1031.       r.y += diffy;
  1032.       break;
  1033.     case GridBagConstraints.SOUTH:
  1034.       r.x += diffx/2;
  1035.       r.y += diffy;
  1036.       break;
  1037.     case GridBagConstraints.SOUTHWEST:
  1038.       r.y += diffy;
  1039.       break;
  1040.     case GridBagConstraints.WEST:
  1041.       r.y += diffy/2;
  1042.       break;
  1043.     case GridBagConstraints.NORTHWEST:
  1044.       break;
  1045.     default:
  1046.       throw new IllegalArgumentException("illegal anchor value");
  1047.     }
  1048.   }
  1049.  
  1050.   /*
  1051.    * Figure out the minimum size of the
  1052.    * master based on the information from GetLayoutInfo()
  1053.    */
  1054.   protected Dimension GetMinSize(Container parent, GridBagLayoutInfo info) {
  1055.     Dimension d = new Dimension();
  1056.     int i, t;
  1057.     Insets insets = parent.getInsets();
  1058.  
  1059.     t = 0;
  1060.     for(i = 0; i < info.width; i++)
  1061.       t += info.minWidth[i];
  1062.     d.width = t + insets.left + insets.right;
  1063.  
  1064.     t = 0;
  1065.     for(i = 0; i < info.height; i++)
  1066.       t += info.minHeight[i];
  1067.     d.height = t + insets.top + insets.bottom;
  1068.  
  1069.     return d;
  1070.   }
  1071.  
  1072.   /*
  1073.    * Lay out the grid
  1074.    */
  1075.   protected void ArrangeGrid(Container parent) {
  1076.     Component comp;
  1077.     int compindex;
  1078.     GridBagConstraints constraints;
  1079.     Insets insets = parent.getInsets();
  1080.     Component components[] = parent.getComponents();
  1081.     Dimension d;
  1082.     Rectangle r = new Rectangle();
  1083.     int i, diffw, diffh;
  1084.     double weight;
  1085.     GridBagLayoutInfo info;
  1086.     
  1087.     /*
  1088.      * If the parent has no slaves anymore, then don't do anything
  1089.      * at all:  just leave the parent's size as-is.
  1090.      */
  1091.     if (components.length == 0 &&
  1092.     (columnWidths == null || columnWidths.length == 0) &&
  1093.     (rowHeights == null || rowHeights.length == 0)) {
  1094.       return;
  1095.     }
  1096.     
  1097.     /*
  1098.      * Pass #1: scan all the slaves to figure out the total amount
  1099.      * of space needed.
  1100.      */
  1101.     
  1102.     info = GetLayoutInfo(parent, PREFERREDSIZE);
  1103.     d = GetMinSize(parent, info);
  1104.  
  1105.     if (parent.width < d.width || parent.height < d.height) {
  1106.       info = GetLayoutInfo(parent, MINSIZE);
  1107.       d = GetMinSize(parent, info);
  1108.     }
  1109.  
  1110.     layoutInfo = info;
  1111.     r.width = d.width;
  1112.     r.height = d.height;
  1113.  
  1114.     /*
  1115.      * DEBUG
  1116.      *
  1117.      * DumpLayoutInfo(info);
  1118.      * for (compindex = 0 ; compindex < components.length ; compindex++) {
  1119.      * comp = components[compindex];
  1120.      * if (!comp.isVisible())
  1121.      *    continue;
  1122.      * constraints = lookupConstraints(comp);
  1123.      * DumpConstraints(constraints);
  1124.      * }
  1125.      * System.out.println("minSize " + r.width + " " + r.height);
  1126.      */
  1127.     
  1128.     /*
  1129.      * If the current dimensions of the window don't match the desired
  1130.      * dimensions, then adjust the minWidth and minHeight arrays
  1131.      * according to the weights.
  1132.      */
  1133.     
  1134.     diffw = parent.width - r.width;
  1135.     if (diffw != 0) {
  1136.       weight = 0.0;
  1137.       for (i = 0; i < info.width; i++)
  1138.     weight += info.weightX[i];
  1139.       if (weight > 0.0) {
  1140.     for (i = 0; i < info.width; i++) {
  1141.       int dx = (int)(( ((double)diffw) * info.weightX[i]) / weight);
  1142.       info.minWidth[i] += dx;
  1143.       r.width += dx;
  1144.       if (info.minWidth[i] < 0) {
  1145.         r.width -= info.minWidth[i];
  1146.         info.minWidth[i] = 0;
  1147.       }
  1148.     }
  1149.       }
  1150.       diffw = parent.width - r.width;
  1151.     }
  1152.     else {
  1153.       diffw = 0;
  1154.     }
  1155.     
  1156.     diffh = parent.height - r.height;
  1157.     if (diffh != 0) {
  1158.       weight = 0.0;
  1159.       for (i = 0; i < info.height; i++)
  1160.     weight += info.weightY[i];
  1161.       if (weight > 0.0) {
  1162.     for (i = 0; i < info.height; i++) {
  1163.       int dy = (int)(( ((double)diffh) * info.weightY[i]) / weight);
  1164.       info.minHeight[i] += dy;
  1165.       r.height += dy;
  1166.       if (info.minHeight[i] < 0) {
  1167.         r.height -= info.minHeight[i];
  1168.         info.minHeight[i] = 0;
  1169.       }
  1170.     }
  1171.       }
  1172.       diffh = parent.height - r.height;
  1173.     }
  1174.     else {
  1175.       diffh = 0;
  1176.     }
  1177.  
  1178.     /*
  1179.      * DEBUG
  1180.      *
  1181.      * System.out.println("Re-adjusted:");
  1182.      * DumpLayoutInfo(info);
  1183.      */
  1184.     
  1185.     /*
  1186.      * Now do the actual layout of the slaves using the layout information
  1187.      * that has been collected.
  1188.      */
  1189.     
  1190.     info.startx = diffw/2 + insets.left;
  1191.     info.starty = diffh/2 + insets.top;
  1192.  
  1193.     for (compindex = 0 ; compindex < components.length ; compindex++) {
  1194.       comp = components[compindex];
  1195.       if (!comp.isVisible())
  1196.     continue;
  1197.       constraints = lookupConstraints(comp);
  1198.  
  1199.       r.x = info.startx;
  1200.       for(i = 0; i < constraints.tempX; i++)
  1201.     r.x += info.minWidth[i];
  1202.       
  1203.       r.y = info.starty;
  1204.       for(i = 0; i < constraints.tempY; i++)
  1205.     r.y += info.minHeight[i];
  1206.       
  1207.       r.width = 0;
  1208.       for(i = constraints.tempX;
  1209.       i < (constraints.tempX + constraints.tempWidth);
  1210.       i++) {
  1211.     r.width += info.minWidth[i];
  1212.       }
  1213.       
  1214.       r.height = 0;
  1215.       for(i = constraints.tempY;
  1216.       i < (constraints.tempY + constraints.tempHeight);
  1217.       i++) {
  1218.     r.height += info.minHeight[i];
  1219.       }
  1220.       
  1221.       AdjustForGravity(constraints, r);
  1222.       
  1223.       /*
  1224.        * If the window is too small to be interesting then
  1225.        * unmap it.  Otherwise configure it and then make sure
  1226.        * it's mapped.
  1227.        */
  1228.       
  1229.       if ((r.width <= 0) || (r.height <= 0)) {
  1230.     comp.setBounds(0, 0, 0, 0);
  1231.       }
  1232.       else {
  1233.     if (comp.x != r.x || comp.y != r.y ||
  1234.         comp.width != r.width || comp.height != r.height) {
  1235.       comp.setBounds(r.x, r.y, r.width, r.height);
  1236.     }
  1237.       }
  1238.     }
  1239.   }
  1240. }
  1241.