home *** CD-ROM | disk | FTP | other *** search
/ Java Programmer's Toolkit / Java Programmer's Toolkit.iso / solaris2 / jdk / src / java / awt / gridbagl.jav < prev    next >
Encoding:
Text File  |  1995-10-30  |  29.3 KB  |  993 lines

  1. /*
  2.  * @(#)GridBagLayout.java    1.2 95/10/19 Doug Stein
  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. package java.awt;
  20.  
  21. import java.util.Hashtable;
  22. import java.util.Vector;
  23.  
  24. class GridBagLayoutInfo {
  25.   int width, height;        /* number of cells horizontally, vertically */
  26.   int minWidth[];        /* largest minWidth in each column */
  27.   int minHeight[];        /* largest minHeight in each row */
  28.   double weightX[];        /* largest weight in each column */
  29.   double weightY[];        /* largest weight in each row */
  30.  
  31.   GridBagLayoutInfo () {
  32.     minWidth = new int[GridBagLayout.MAXGRIDSIZE];
  33.     minHeight = new int[GridBagLayout.MAXGRIDSIZE];
  34.     weightX = new double[GridBagLayout.MAXGRIDSIZE];
  35.     weightY = new double[GridBagLayout.MAXGRIDSIZE];
  36.   }
  37. }
  38.  
  39. /**
  40.     GridBagLayout is a flexible layout manager
  41.     that aligns components vertically and horizontally,
  42.     without requiring that the components be the same size.
  43.     Each GridBagLayout uses a rectangular grid of cells,
  44.     with each component occupying one or more cells
  45.     (called its <em>display area</em>).
  46.     Each component managed by a GridBagLayout 
  47.     is associated with a
  48.     <a href=java.awt.GridBagConstraints.html>GridBagConstraints</a> instance
  49.     that specifies how the component is laid out
  50.     within its display area.
  51.     How a GridBagLayout places a set of components
  52.     depends on each component's GridBagConstraints and minimum size,
  53.     as well as the preferred size of the components' container.
  54.     <p>
  55.  
  56.     To use a GridBagLayout effectively,
  57.     you must customize one or more of its components' GridBagConstraints.
  58.     You customize a GridBagConstraints object by setting one or more
  59.     of its instance variables:
  60.     <dl>
  61.     <dt> <a href=java.awt.GridBagConstraints.html#gridx>gridx</a>,
  62.          <a href=java.awt.GridBagConstraints.html#gridy>gridy</a>
  63.     <dd> Specifies the cell at the upper left of the component's display area,
  64.      where the upper-left-most cell has address gridx=0, gridy=0.
  65.      Use GridBagConstraints.RELATIVE (the default value)
  66.      to specify that the component be just placed
  67.      just to the right of (for gridx)
  68.      or just below (for gridy)
  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>gridwidth</a>,
  72.          <a href=java.awt.GridBagConstraints.html#gridheight>gridheight</a>
  73.     <dd> Specifies the number of cells in a row (for gridwidth)
  74.      or column (for gridheight)
  75.      in the component's display area.
  76.      The default value is 1.
  77.      Use GridBagConstraints.REMAINDER to specify 
  78.      that the component be the last one in its row (for gridwidth)
  79.      or column (for gridheight).
  80.      Use GridBagConstraints.RELATIVE to specify 
  81.      that the component be the next to last one
  82.      in its row (for gridwidth) or column (for gridheight).
  83.     <dt> <a href=java.awt.GridBagConstraints.html#fill>fill</a>
  84.     <dd> Used when the component's display area
  85.         is larger than the component's requested size
  86.      to determine whether (and how) to resize the component.
  87.      Valid values are
  88.           GridBagConstraint.NONE
  89.           (the default),
  90.           GridBagConstraint.HORIZONTAL
  91.           (make the component wide enough to fill its display area
  92.           horizontally, but don't change its height),
  93.           GridBagConstraint.VERTICAL
  94.           (make the component tall enough to fill its display area
  95.           vertically, but don't change its width),
  96.           and 
  97.           GridBagConstraint.BOTH
  98.           (make the component fill its display area entirely).
  99.     <dt> <a href=java.awt.GridBagConstraints.html#ipadx>ipadx</a>,
  100.          <a href=java.awt.GridBagConstraints.html#ipady>ipady</a>
  101.     <dd> Specifies the internal padding: 
  102.      how much to add to the minimum size of the component.
  103.      The width of the component will be at least
  104.      its minimum width plus ipadx*2 pixels
  105.      (since the padding applies to both sides of the component).
  106.      Similarly, the height of the component will be at least
  107.      the minimum height plus ipady*2 pixels.
  108.     <dt> <a href=java.awt.GridBagConstraints.html#insets>insets</a>
  109.     <dd> Specifies the external padding of the component --
  110.      the minimum amount of space between the component 
  111.      and the edges of its display area.
  112.     <dt> <a href=java.awt.GridBagConstraints.html#anchor>anchor</a>
  113.     <dd> Used when the component is smaller than its display area
  114.      to determine where (within the area) to place the component.
  115.      Valid values are
  116.      GridBagConstraints.CENTER (the default),
  117.      GridBagConstraints.NORTH,
  118.      GridBagConstraints.NORTHEAST,
  119.      GridBagConstraints.EAST,
  120.      GridBagConstraints.SOUTHEAST,
  121.      GridBagConstraints.SOUTH,
  122.      GridBagConstraints.SOUTHWEST,
  123.      GridBagConstraints.WEST, and
  124.      GridBagConstraints.NORTHWEST.
  125.     <dt> <a href=java.awt.GridBagConstraints.html#weightx>weightx</a>,
  126.          <a href=java.awt.GridBagConstraints.html#weighty>weighty</a>
  127.     <dd> Used to determine how to distribute space;
  128.      this is important for specifying resizing behavior.
  129.      Unless you specify a weight
  130.      for at least one component in a row (weightx)
  131.      and column (weighty),
  132.      all the components clump together in the center of
  133.      their container.
  134.      This is because when the weight is zero (the default),
  135.      the GridBagLayout puts any extra space 
  136.      between its grid of cells and the edges of the container.
  137.     </dl>
  138.  
  139.     The following figure shows ten components (all buttons)
  140.     managed by a GridBagLayout:
  141.     <blockquote>
  142.     <img src=images/java.awt/GridBagEx.gif width=262 height=155>
  143.     </blockquote>
  144.  
  145.     All the components have fill=GridBagConstraints.BOTH.
  146.     In addition, the components have the following non-default constraints:
  147.     <ul>
  148.     <li>Button1, Button2, Button3:
  149.         weightx=1.0
  150.     <li>Button4:
  151.         weightx=1.0,
  152.         gridwidth=GridBagConstraints.REMAINDER
  153.     <li>Button5:
  154.         gridwidth=GridBagConstraints.REMAINDER
  155.     <li>Button6:
  156.         gridwidth=GridBagConstraints.RELATIVE
  157.     <li>Button7:
  158.         gridwidth=GridBagConstraints.REMAINDER
  159.     <li>Button8:
  160.         gridheight=2, weighty=1.0,
  161.     <li>Button9, Button 10:
  162.         gridwidth=GridBagConstraints.REMAINDER
  163.     </ul>
  164.  
  165.     Here is the code that implements the example shown above:
  166.     <blockquote>
  167.     <pre>
  168. import java.awt.*;
  169. import java.util.*;
  170. import java.applet.Applet;
  171.  
  172. public class GridBagEx1 extends Applet {
  173.  
  174.     protected void makebutton(String name,
  175.                               GridBagLayout gridbag,
  176.                               GridBagConstraints c) {
  177.         Button button = new Button(name);
  178.         gridbag.setConstraints(button, c);
  179.         add(button);
  180.     }
  181.  
  182.     public void init() {
  183.         GridBagLayout gridbag = new GridBagLayout();
  184.         GridBagConstraints c = new GridBagConstraints();
  185.  
  186.         setFont(new Font("Helvetica", Font.PLAIN, 14));
  187.         setLayout(gridbag);
  188.    
  189.         c.fill = GridBagConstraints.BOTH;
  190.         c.weightx = 1.0;
  191.         makebutton("Button1", gridbag, c);
  192.         makebutton("Button2", gridbag, c);
  193.         makebutton("Button3", gridbag, c);
  194.     
  195.     c.gridwidth = GridBagConstraints.REMAINDER; //end row
  196.         makebutton("Button4", gridbag, c);
  197.     
  198.         c.weightx = 0.0;           //reset to the default
  199.         makebutton("Button5", gridbag, c); //another row
  200.     
  201.     c.gridwidth = GridBagConstraints.RELATIVE; //next-to-last in row
  202.         makebutton("Button6", gridbag, c);
  203.     
  204.     c.gridwidth = GridBagConstraints.REMAINDER; //end row
  205.         makebutton("Button7", gridbag, c);
  206.     
  207.     c.gridwidth = 1;              //reset to the default
  208.     c.gridheight = 2;
  209.         c.weighty = 1.0;
  210.         makebutton("Button8", gridbag, c);
  211.     
  212.         c.weighty = 0.0;           //reset to the default
  213.     c.gridwidth = GridBagConstraints.REMAINDER; //end row
  214.     c.gridheight = 1;           //reset to the default
  215.         makebutton("Button9", gridbag, c);
  216.         makebutton("Button10", gridbag, c);
  217.     
  218.         resize(300, 100);
  219.     }
  220.     
  221.     public static void main(String args[]) {
  222.     Frame f = new Frame("GridBag Layout Example");
  223.     GridBagEx1 ex1 = new GridBagEx1();
  224.     
  225.     ex1.init();
  226.     
  227.     f.add("Center", ex1);
  228.     f.pack();
  229.     f.resize(f.preferredSize());
  230.     f.show();
  231.     }
  232. }
  233.     </pre>
  234.     </blockquote>
  235.  *
  236.  * @version 1.2, 10/19/95
  237.  * @author Doug Stein
  238.  */
  239. public class GridBagLayout implements LayoutManager {
  240.  
  241.   protected static final int MAXGRIDSIZE = 128;
  242.   protected static final int MINSIZE = 1;
  243.   protected static final int PREFERREDSIZE = 2;
  244.  
  245.   protected Hashtable comptable;
  246.   protected GridBagConstraints defaultConstraints;
  247.  
  248.   /**
  249.    * Creates a gridbag layout.
  250.    */
  251.   public GridBagLayout () {
  252.     comptable = new Hashtable();
  253.     defaultConstraints = new GridBagConstraints();
  254.   }
  255.  
  256.   /**
  257.    * Sets the constraints for the specified component.
  258.    * @param comp the component to be modified
  259.    * @param constraints the constraints to be applied
  260.    */
  261.   public void setConstraints(Component comp, GridBagConstraints constraints) {
  262.     comptable.put(comp, constraints.clone());
  263.   }
  264.  
  265.   /**
  266.    * Retrieves the constraints for the specified component.  A copy of
  267.    * the constraints is returned.
  268.    * @param comp the component to be queried
  269.    */
  270.   public GridBagConstraints getConstraints(Component comp) {
  271.     GridBagConstraints constraints = (GridBagConstraints)comptable.get(comp);
  272.     if (constraints == null) {
  273.       setConstraints(comp, defaultConstraints);
  274.       constraints = (GridBagConstraints)comptable.get(comp);
  275.     }
  276.     return (GridBagConstraints)constraints.clone();
  277.   }
  278.  
  279.   /**
  280.    * Retrieves the constraints for the specified component.  The return
  281.    * value is not a copy, but is the actual constraints class used by the
  282.    * layout mechanism.
  283.    * @param comp the component to be queried
  284.    */
  285.   protected GridBagConstraints lookupConstraints(Component comp) {
  286.     GridBagConstraints constraints = (GridBagConstraints)comptable.get(comp);
  287.     if (constraints == null) {
  288.       setConstraints(comp, defaultConstraints);
  289.       constraints = (GridBagConstraints)comptable.get(comp);
  290.     }
  291.     return constraints;
  292.   }
  293.  
  294.   /**
  295.    * Adds the specified component with the specified name to the layout.
  296.    * @param name the name of the component
  297.    * @param comp the component to be added
  298.    */
  299.   public void addLayoutComponent(String name, Component comp) {
  300.   }
  301.  
  302.   /**
  303.    * Removes the specified component from the layout. Does not apply.
  304.    * @param comp the component to be removed
  305.    */
  306.   public void removeLayoutComponent(Component comp) {
  307.   }
  308.  
  309.   /** 
  310.    * Returns the preferred dimensions for this layout given the components
  311.    * in the specified panel.
  312.    * @param parent the component which needs to be laid out 
  313.    * @see #minimumSize
  314.    */
  315.   public Dimension preferredLayoutSize(Container parent) {
  316.     GridBagLayoutInfo info = GetLayoutInfo(parent, PREFERREDSIZE);
  317.     return GetMinSize(parent, info);
  318.   }
  319.  
  320.   /**
  321.    * Returns the minimum dimensions needed to layout the components 
  322.    * contained in the specified panel.
  323.    * @param parent the component which needs to be laid out 
  324.    * @see #preferredSize
  325.    */
  326.   public Dimension minimumLayoutSize(Container parent) {
  327.     GridBagLayoutInfo info = GetLayoutInfo(parent, MINSIZE);
  328.     return GetMinSize(parent, info);
  329.   }
  330.  
  331.   /** 
  332.    * Lays out the container in the specified panel.  
  333.    * @param parent the specified component being laid out
  334.    * @see Container
  335.    */
  336.   public void layoutContainer(Container parent) {
  337.     ArrangeGrid(parent);
  338.   }
  339.  
  340.   /**
  341.    * Returns the String representation of this GridLayout's values.
  342.    */
  343.   public String toString() {
  344.     return "";
  345.   }
  346.  
  347.   /**
  348.    * Print the layout information.  Useful for debugging.
  349.    */
  350.   protected void DumpLayoutInfo(GridBagLayoutInfo s) {
  351.     int x;
  352.  
  353.     System.out.println("Col\tWidth\tWeight");
  354.     for (x=0; x<s.width; x++) {
  355.       System.out.println(x + "\t" +
  356.              s.minWidth[x] + "\t" +
  357.              s.weightX[x]);
  358.     }
  359.     System.out.println("Row\tHeight\tWeight");
  360.     for (x=0; x<s.height; x++) {
  361.       System.out.println(x + "\t" +
  362.              s.minHeight[x] + "\t" +
  363.              s.weightY[x]);
  364.     }
  365.   }
  366.  
  367.   /**
  368.    * Print the layout constraints.  Useful for debugging.
  369.    */
  370.   protected void DumpConstraints(GridBagConstraints constraints) {
  371.     System.out.println(
  372.                "wt " +
  373.                constraints.weightx +
  374.                " " +
  375.                constraints.weighty +
  376.                ", " +
  377.  
  378.                "box " +
  379.                constraints.gridx +
  380.                " " +
  381.                constraints.gridy +
  382.                " " +
  383.                constraints.gridwidth +
  384.                " " +
  385.                constraints.gridheight +
  386.                ", " +
  387.  
  388.                "min " +
  389.                constraints.minWidth +
  390.                " " +
  391.                constraints.minHeight +
  392.                ", " +
  393.  
  394.                "pad " +
  395.                constraints.insets.bottom +
  396.                " " +
  397.                constraints.insets.left +
  398.                " " +
  399.                constraints.insets.right +
  400.                " " +
  401.                constraints.insets.top +
  402.                " " +
  403.                constraints.ipadx +
  404.                " " +
  405.                constraints.ipady);
  406.   }
  407.  
  408.   /*
  409.    * Fill in an instance of the above structure for the current set
  410.    * of managed children.  This requires three passes through the
  411.    * set of children:
  412.    *
  413.    * 1) Figure out the dimensions of the layout grid
  414.    * 2) Determine which cells the components occupy
  415.    * 3) Distribute the weights and min sizes amoung the rows/columns.
  416.    *
  417.    * This also caches the minsizes for all the children when they are
  418.    * first encountered (so subsequent loops don't need to ask again).
  419.    */
  420.   
  421.   protected GridBagLayoutInfo GetLayoutInfo(Container parent, int sizeflag) {
  422.     GridBagLayoutInfo r = new GridBagLayoutInfo();
  423.     Component comp;
  424.     GridBagConstraints constraints;
  425.     Dimension d;
  426.     int ncomponents = parent.countComponents();
  427.  
  428.     int compindex, i, j, k, px, py, pixels_diff, nextSize;
  429.     int curX, curY, curWidth, curHeight, curRow, curCol;
  430.     double weight_diff, weight, start, size;
  431.     int xMax[], yMax[];
  432.  
  433.     /*
  434.      * Pass #1
  435.      *
  436.      * Figure out the dimensions of the layout grid (use a value of 1 for
  437.      * zero or negative widths and heights).
  438.      */
  439.     
  440.     r.width = r.height = 0;
  441.     curRow = curCol = -1;
  442.     xMax = new int[MAXGRIDSIZE];
  443.     yMax = new int[MAXGRIDSIZE];
  444.  
  445.     for (compindex = 0 ; compindex < ncomponents ; compindex++) {
  446.       comp = parent.getComponent(compindex);
  447.       constraints = lookupConstraints(comp);
  448.       
  449.       curX = constraints.gridx;
  450.       curY = constraints.gridy;
  451.       curWidth = constraints.gridwidth;
  452.       if (curWidth <= 0)
  453.     curWidth = 1;
  454.       curHeight = constraints.gridheight;
  455.       if (curHeight <= 0)
  456.     curHeight = 1;
  457.       
  458.       /* If x or y is negative, then use relative positioning: */
  459.       if (curX < 0 && curY < 0) {
  460.     if (curRow >= 0)
  461.       curY = curRow;
  462.     else if (curCol >= 0)
  463.       curX = curCol;
  464.     else
  465.       curY = 0;
  466.       }
  467.       if (curX < 0) {
  468.     px = 0;
  469.     for (i = curY; i < (curY + curHeight); i++)
  470.       px = Math.max(px, xMax[i]);
  471.     
  472.     curX = px - curX - 1;
  473.     if(curX < 0)
  474.       curX = 0;
  475.       }
  476.       else if (curY < 0) {
  477.     py = 0;
  478.     for (i = curX; i < (curX + curWidth); i++)
  479.       py = Math.max(py, yMax[i]);
  480.     
  481.     curY = py - curY - 1;
  482.     if(curY < 0)
  483.       curY = 0;
  484.       }
  485.       
  486.       /* Adjust the grid width and height */
  487.       for (px = curX + curWidth; r.width < px; r.width++);
  488.       for (py = curY + curHeight; r.height < py; r.height++);
  489.       
  490.       /* Adjust the xMax and yMax arrays */
  491.       for (i = curX; i < (curX + curWidth); i++) { yMax[i] = py; }
  492.       for (i = curY; i < (curY + curHeight); i++) { xMax[i] = px; }
  493.       
  494.       /* Cache the current slave's size. */
  495.       if (sizeflag == PREFERREDSIZE)
  496.     d = comp.preferredSize();
  497.       else
  498.     d = comp.minimumSize();
  499.       constraints.minWidth = d.width;
  500.       constraints.minHeight = d.height;
  501.       
  502.       /* Zero width and height must mean that this is the last item (or
  503.        * else something is wrong). */
  504.       if (constraints.gridheight == 0 && constraints.gridwidth == 0)
  505.     curRow = curCol = -1;
  506.       
  507.       /* Zero width starts a new row */
  508.       if (constraints.gridheight == 0 && curRow < 0)
  509.     curCol = curX + curWidth;
  510.       
  511.       /* Zero height starts a new column */
  512.       else if (constraints.gridwidth == 0 && curCol < 0)
  513.     curRow = curY + curHeight;
  514.     }
  515.     
  516.     /*
  517.      * Pass #2
  518.      *
  519.      * Negative values for gridX are filled in with the current x value.
  520.      * Negative values for gridY are filled in with the current y value.
  521.      * Negative or zero values for gridWidth and gridHeight end the current
  522.      *  row or column, respectively.
  523.      */
  524.     
  525.     curRow = curCol = -1;
  526.     xMax = new int[MAXGRIDSIZE];
  527.     yMax = new int[MAXGRIDSIZE];
  528.     
  529.     for (compindex = 0 ; compindex < ncomponents ; compindex++) {
  530.       comp = parent.getComponent(compindex);
  531.       constraints = lookupConstraints(comp);
  532.       
  533.       curX = constraints.gridx;
  534.       curY = constraints.gridy;
  535.       curWidth = constraints.gridwidth;
  536.       curHeight = constraints.gridheight;
  537.       
  538.       /* If x or y is negative, then use relative positioning: */
  539.       if (curX < 0 && curY < 0) {
  540.     if(curRow >= 0)
  541.       curY = curRow;
  542.     else if(curCol >= 0)
  543.       curX = curCol;
  544.     else
  545.       curY = 0;
  546.       }
  547.       
  548.       if (curX < 0) {
  549.     if (curHeight <= 0) {
  550.       curHeight += r.height - curY;
  551.       if (curHeight < 1)
  552.         curHeight = 1;
  553.     }
  554.     
  555.     px = 0;
  556.     for (i = curY; i < (curY + curHeight); i++)
  557.       px = Math.max(px, xMax[i]);
  558.     
  559.     curX = px - curX - 1;
  560.     if(curX < 0)
  561.       curX = 0;
  562.       }
  563.       else if (curY < 0) {
  564.     if (curWidth <= 0) {
  565.       curWidth += r.width - curX;
  566.       if (curWidth < 1)
  567.         curWidth = 1;
  568.     }
  569.     
  570.     py = 0;
  571.     for (i = curX; i < (curX + curWidth); i++)
  572.       py = Math.max(py, yMax[i]);
  573.     
  574.     curY = py - curY - 1;
  575.     if(curY < 0)
  576.       curY = 0;
  577.       }
  578.       
  579.       if (curWidth <= 0) {
  580.     curWidth += r.width - curX;
  581.     if (curWidth < 1)
  582.       curWidth = 1;
  583.       }
  584.       
  585.       if (curHeight <= 0) {
  586.     curHeight += r.height - curY;
  587.     if (curHeight < 1)
  588.       curHeight = 1;
  589.       }
  590.       
  591.       px = curX + curWidth;
  592.       py = curY + curHeight;
  593.       
  594.       for (i = curX; i < (curX + curWidth); i++) { yMax[i] = py; }
  595.       for (i = curY; i < (curY + curHeight); i++) { xMax[i] = px; }
  596.       
  597.       /* Make negative sizes start a new row/column */
  598.       if (constraints.gridheight == 0 && constraints.gridwidth == 0)
  599.     curRow = curCol = -1;
  600.       if (constraints.gridheight == 0 && curRow < 0)
  601.     curCol = curX + curWidth;
  602.       else if (constraints.gridwidth == 0 && curCol < 0)
  603.         curRow = curY + curHeight;
  604.       
  605.       /* Assign the new values to the gridbag slave */
  606.       constraints.tempX = curX;
  607.       constraints.tempY = curY;
  608.       constraints.tempWidth = curWidth;
  609.       constraints.tempHeight = curHeight;
  610.     }
  611.     
  612.     /*
  613.      * Pass #3
  614.      *
  615.      * Distribute the minimun widths and weights:
  616.      */
  617.     
  618.     /* Initialize arrays to zero */
  619.     nextSize = Integer.MAX_VALUE;
  620.     
  621.     for (i = 1;
  622.      i != Integer.MAX_VALUE;
  623.      i = nextSize, nextSize = Integer.MAX_VALUE) {
  624.       for (compindex = 0 ; compindex < ncomponents ; compindex++) {
  625.     comp = parent.getComponent(compindex);
  626.     constraints = lookupConstraints(comp);
  627.       
  628.     if (constraints.tempWidth == i) {
  629.       px = constraints.tempX + constraints.tempWidth; /* right column */
  630.       
  631.       /* 
  632.        * Figure out if we should use this slave\'s weight.  If the weight
  633.        * is less than the total weight spanned by the width of the cell,
  634.        * then discard the weight.  Otherwise split it the difference
  635.        * according to the existing weights.
  636.        */
  637.       
  638.       weight_diff = constraints.weightx;
  639.       for (k = constraints.tempX; k < px; k++)
  640.         weight_diff -= r.weightX[k];
  641.       if (weight_diff > 0.0) {
  642.         weight = 0.0;
  643.         for (k = constraints.tempX; k < px; k++)
  644.           weight += r.weightX[k];
  645.         for (k = constraints.tempX; weight > 0.0; k++) {
  646.           double wt = r.weightX[k];
  647.           double dx = (wt * weight_diff) / weight;
  648.           r.weightX[k] += dx;
  649.           weight_diff -= dx;
  650.           weight -= wt;
  651.         }
  652.         /* Assign the remainder to the rightmost cell */
  653.         r.weightX[px-1] += weight_diff;
  654.       }
  655.       
  656.       /*
  657.        * Calculate the minWidth array values.
  658.        * First, figure out how wide the current slave needs to be.
  659.        * Then, see if it will fit within the current minWidth values.
  660.        * If it will not fit, add the difference according to the
  661.        * weightX array.
  662.        */
  663.       
  664.       pixels_diff =
  665.         constraints.minWidth + constraints.ipadx +
  666.         constraints.insets.left + constraints.insets.right;
  667.  
  668.       for (k = constraints.tempX; k < px; k++)
  669.         pixels_diff -= r.minWidth[k];
  670.       if (pixels_diff > 0) {
  671.         weight = 0.0;
  672.         for (k = constraints.tempX; k < px; k++)
  673.           weight += r.weightX[k];
  674.         for (k = constraints.tempX; weight > 0.0; k++) {
  675.           double wt = r.weightX[k];
  676.           int dx = (int)((wt * ((double)pixels_diff)) / weight);
  677.           r.minWidth[k] += dx;
  678.           pixels_diff -= dx;
  679.           weight -= wt;
  680.         }
  681.         /* Any leftovers go into the rightmost cell */
  682.         r.minWidth[px-1] += pixels_diff;
  683.       }
  684.     }
  685.     else if (constraints.tempWidth > i && constraints.tempWidth < nextSize)
  686.       nextSize = constraints.tempWidth;
  687.     
  688.     
  689.     if (constraints.tempHeight == i) {
  690.       py = constraints.tempY + constraints.tempHeight; /* bottom row */
  691.       
  692.       /* 
  693.        * Figure out if we should use this slave\'s weight.  If the weight
  694.        * is less than the total weight spanned by the height of the cell,
  695.        * then discard the weight.  Otherwise split it the difference
  696.        * according to the existing weights.
  697.        */
  698.       
  699.       weight_diff = constraints.weighty;
  700.       for (k = constraints.tempY; k < py; k++)
  701.         weight_diff -= r.weightY[k];
  702.       if (weight_diff > 0.0) {
  703.         weight = 0.0;
  704.         for (k = constraints.tempY; k < py; k++)
  705.           weight += r.weightY[k];
  706.         for (k = constraints.tempY; weight > 0.0; k++) {
  707.           double wt = r.weightY[k];
  708.           double dy = (wt * weight_diff) / weight;
  709.           r.weightY[k] += dy;
  710.           weight_diff -= dy;
  711.           weight -= wt;
  712.         }
  713.         /* Assign the remainder to the bottom cell */
  714.         r.weightY[py-1] += weight_diff;
  715.       }
  716.       
  717.       /*
  718.        * Calculate the minHeight array values.
  719.        * First, figure out how tall the current slave needs to be.
  720.        * Then, see if it will fit within the current minHeight values.
  721.        * If it will not fit, add the difference according to the
  722.        * weightY array.
  723.        */
  724.       
  725.       pixels_diff =
  726.         constraints.minHeight + constraints.ipady +
  727.         constraints.insets.top + constraints.insets.bottom;
  728.       for (k = constraints.tempY; k < py; k++)
  729.         pixels_diff -= r.minHeight[k];
  730.       if (pixels_diff > 0) {
  731.         weight = 0.0;
  732.         for (k = constraints.tempY; k < py; k++)
  733.           weight += r.weightY[k];
  734.         for (k = constraints.tempY; weight > 0.0; k++) {
  735.           double wt = r.weightY[k];
  736.           int dy = (int)((wt * ((double)pixels_diff)) / weight);
  737.           r.minHeight[k] += dy;
  738.           pixels_diff -= dy;
  739.           weight -= wt;
  740.         }
  741.         /* Any leftovers go into the bottom cell */
  742.         r.minHeight[py-1] += pixels_diff;
  743.       }
  744.     }
  745.     else if (constraints.tempHeight > i &&
  746.          constraints.tempHeight < nextSize)
  747.       nextSize = constraints.tempHeight;
  748.       }
  749.     }
  750.  
  751.     return r;
  752.   }
  753.   
  754.   /*
  755.    * Adjusts the x, y, width, and height fields to the correct
  756.    * values depending on the constraint geometry and pads.
  757.    */
  758.   protected void AdjustForGravity(GridBagConstraints constraints,
  759.                   Rectangle r) {
  760.     int diffx, diffy;
  761.  
  762.     r.x += constraints.insets.left;
  763.     r.width -= (constraints.insets.left + constraints.insets.right);
  764.     r.y += constraints.insets.top;
  765.     r.height -= (constraints.insets.top + constraints.insets.bottom);
  766.     
  767.     diffx = 0;
  768.     if ((constraints.fill != GridBagConstraints.HORIZONTAL &&
  769.      constraints.fill != GridBagConstraints.BOTH)
  770.     && (r.width > (constraints.minWidth + constraints.ipadx))) {
  771.       diffx = r.width - (constraints.minWidth + constraints.ipadx);
  772.       r.width = constraints.minWidth + constraints.ipadx;
  773.     }
  774.     
  775.     diffy = 0;
  776.     if ((constraints.fill != GridBagConstraints.VERTICAL &&
  777.      constraints.fill != GridBagConstraints.BOTH)
  778.     && (r.height > (constraints.minHeight + constraints.ipady))) {
  779.       diffy = r.height - (constraints.minHeight + constraints.ipady);
  780.       r.height = constraints.minHeight + constraints.ipady;
  781.     }
  782.     
  783.     switch (constraints.anchor) {
  784.     case GridBagConstraints.CENTER:
  785.       r.x += diffx/2;
  786.       r.y += diffy/2;
  787.       break;
  788.     case GridBagConstraints.NORTH:
  789.       r.x += diffx/2;
  790.       break;
  791.     case GridBagConstraints.NORTHEAST:
  792.       r.x += diffx;
  793.       break;
  794.     case GridBagConstraints.EAST:
  795.       r.x += diffx;
  796.       r.y += diffy/2;
  797.       break;
  798.     case GridBagConstraints.SOUTHEAST:
  799.       r.x += diffx;
  800.       r.y += diffy;
  801.       break;
  802.     case GridBagConstraints.SOUTH:
  803.       r.x += diffx/2;
  804.       r.y += diffy;
  805.       break;
  806.     case GridBagConstraints.SOUTHWEST:
  807.       r.y += diffy;
  808.       break;
  809.     case GridBagConstraints.WEST:
  810.       r.y += diffy/2;
  811.       break;
  812.     case GridBagConstraints.NORTHWEST:
  813.       break;
  814.     default:
  815.       throw new IllegalArgumentException("illegal anchor value");
  816.     }
  817.   }
  818.  
  819.   /*
  820.    * Figure out the minimum size of the
  821.    * master based on the information from GetLayoutInfo()
  822.    */
  823.   protected Dimension GetMinSize(Container parent, GridBagLayoutInfo info) {
  824.     Dimension d = new Dimension();
  825.     int i, t;
  826.     Insets insets = parent.insets();
  827.  
  828.     t = 0;
  829.     for(i = 0; i < info.width; i++)
  830.       t += info.minWidth[i];
  831.     d.width = t + insets.left + insets.right;
  832.  
  833.     t = 0;
  834.     for(i = 0; i < info.height; i++)
  835.       t += info.minHeight[i];
  836.     d.height = t + insets.top + insets.bottom;
  837.  
  838.     return d;
  839.   }
  840.  
  841.   /*
  842.    * Lay out the grid
  843.    */
  844.   protected void ArrangeGrid(Container parent) {
  845.     Component comp;
  846.     int compindex;
  847.     GridBagConstraints constraints;
  848.     Insets insets = parent.insets();
  849.     int ncomponents = parent.countComponents();
  850.     Dimension d;
  851.     Rectangle r = new Rectangle();
  852.     int i, diffw, diffh;
  853.     double weight;
  854.     GridBagLayoutInfo info;
  855.     
  856.     /*
  857.      * If the parent has no slaves anymore, then don't do anything
  858.      * at all:  just leave the parent's size as-is.
  859.      */
  860.     if (ncomponents == 0) {
  861.       return;
  862.     }
  863.     
  864.     /*
  865.      * Pass #1: scan all the slaves to figure out the total amount
  866.      * of space needed.
  867.      */
  868.     
  869.     info = GetLayoutInfo(parent, PREFERREDSIZE);
  870.     d = GetMinSize(parent, info);
  871.  
  872.     if (d.width < parent.width || d.height < parent.height) {
  873.       info = GetLayoutInfo(parent, MINSIZE);
  874.       d = GetMinSize(parent, info);
  875.     }
  876.  
  877.     r.width = d.width;
  878.     r.height = d.height;
  879.  
  880.     /*
  881.      * DEBUG
  882.      *
  883.      * DumpLayoutInfo(info);
  884.      * for (compindex = 0 ; compindex < ncomponents ; compindex++) {
  885.      * comp = parent.getComponent(compindex);
  886.      * constraints = lookupConstraints(comp);
  887.      * DumpConstraints(constraints);
  888.      * }
  889.      * System.out.println("minSize " + r.width + " " + r.height);
  890.      */
  891.     
  892.     /*
  893.      * If the current dimensions of the window don't match the desired
  894.      * dimensions, then adjust the minWidth and minHeight arrays
  895.      * according to the weights.
  896.      */
  897.     
  898.     diffw = parent.width - (r.width + insets.left + insets.right);
  899.     if (diffw != 0) {
  900.       weight = 0.0;
  901.       for (i = 0; i < info.width; i++)
  902.     weight += info.weightX[i];
  903.       if (weight > 0.0) {
  904.     for (i = 0; i < info.width; i++) {
  905.       int dx = (int)(( ((double)diffw) * info.weightX[i]) / weight);
  906.       info.minWidth[i] += dx;
  907.       r.width += dx;
  908.     }
  909.       }
  910.       diffw = parent.width - (r.width + insets.left + insets.right);
  911.     }
  912.     else {
  913.       diffw = 0;
  914.     }
  915.     
  916.     diffh = parent.height - (r.height + insets.top + insets.bottom);
  917.     if (diffh != 0) {
  918.       weight = 0.0;
  919.       for (i = 0; i < info.height; i++)
  920.     weight += info.weightY[i];
  921.       if (weight > 0.0) {
  922.     for (i = 0; i < info.height; i++) {
  923.       int dy = (int)(( ((double)diffh) * info.weightY[i]) / weight);
  924.       info.minHeight[i] += dy;
  925.       r.height += dy;
  926.     }
  927.       }
  928.       diffh = parent.height - (r.height + insets.top + insets.bottom);
  929.     }
  930.     else {
  931.       diffh = 0;
  932.     }
  933.  
  934.     /*
  935.      * DEBUG
  936.      *
  937.      * System.out.println("Re-adjusted:");
  938.      * DumpLayoutInfo(info);
  939.      */
  940.     
  941.     /*
  942.      * Now do the actual layout of the slaves using the layout information
  943.      * that has been collected.
  944.      */
  945.     
  946.     for (compindex = 0 ; compindex < ncomponents ; compindex++) {
  947.       comp = parent.getComponent(compindex);
  948.       constraints = lookupConstraints(comp);
  949.  
  950.       r.x = diffw/2 + insets.left;
  951.       for(i = 0; i < constraints.tempX; i++)
  952.     r.x += info.minWidth[i];
  953.       
  954.       r.y = diffh/2 + insets.top;
  955.       for(i = 0; i < constraints.tempY; i++)
  956.     r.y += info.minHeight[i];
  957.       
  958.       r.width = 0;
  959.       for(i = constraints.tempX;
  960.       i < (constraints.tempX + constraints.tempWidth);
  961.       i++) {
  962.     r.width += info.minWidth[i];
  963.       }
  964.       
  965.       r.height = 0;
  966.       for(i = constraints.tempY;
  967.       i < (constraints.tempY + constraints.tempHeight);
  968.       i++) {
  969.     r.height += info.minHeight[i];
  970.       }
  971.       
  972.       AdjustForGravity(constraints, r);
  973.       
  974.       /*
  975.        * If the window is too small to be interesting then
  976.        * unmap it.  Otherwise configure it and then make sure
  977.        * it's mapped.
  978.        */
  979.       
  980.       if ((r.width <= 0) || (r.height <= 0)) {
  981.     comp.hide();
  982.       }
  983.       else {
  984.     if (comp.x != r.x || comp.y != r.y ||
  985.         comp.width != r.width || comp.height != r.height) {
  986.       comp.reshape(r.x, r.y, r.width, r.height);
  987.     }
  988.     comp.show();
  989.       }
  990.     }
  991.   }
  992. }
  993.