import java.awt.*;

/** Helper class for using GridBagLayout and GridBagConstraints.
 *
 * GridBagLayout differs from GridLayout,
 * because all Rows and Columns need not be the same size,
 * and because a Component may span more than one Row or Column.
 *
 * Any given Row or Column has a constant size,
 * based on the largest Cell it contains.
 * If any Cell in a given Row or Column is occupied,
 * then every Cell is present even if not occupied.
 *
 * If a large Component would "stretch" a Row or Column,
 * so that it is too large for the other Components it holds,
 * the large Component can span two or more Rows or Columns,
 * reducing the effect on their size.
 *
 * The Component spanning Rows or Columns affects their total size,
 * but does not determine how the sizes are distributed.
 *
 * These GridBagConstraints.CONSTANTs allow this control.
 *
 * NONE, HORIZONTAL, VERTICAL, BOTH fill -- how to expand in cell,
 * default is NONE, which is good for Buttons and Labels.
 *
 * REMAINDER width or height -- use rest of row or column.
 * RELATIVE width or height -- use all but last cell of row or column.
 * default is 1 (use a single cell)
 *
 * RELATIVE gridx or gridy -- follow most recent filled row or column,
 * this is the default in a new GridBagConstraints.
 *
 * Weights (by convention between 0.0 and 1.0) apportion extra space,
 * default zero means give any extra space to something else.
 * <IMG SRC="/cgi-bin/counter">
 *
 * @author Morris Hirsch
 * IPD Inc Newport RI
 */

public class GBHelper {

   private GridBagLayout GBL;
   private GridBagConstraints GBC;
   private Container cr;

   private int
      default_ipadx = 0,
      default_ipady = 0,
      default_fill = GridBagConstraints.BOTH;

   private double
      default_weightx = 0.0,
      default_weighty = 0.0;

   private Insets default_insets = new Insets (4, 4, 4, 4);

   public String getVersion () { return "$Id: GBHelp.java,v 1.5 1998/09/30 14:25:38 mhirsch Exp $"; }

/** Construct a Helper for using GridBagLayout and GridBagConstraints.
 * GridBag Helper does setLayout on the Container.
 * Use one of the GridBag Helper addGB methods (below) to add Components. */

  public GBHelper (Container cr) {
      this.cr = cr;
      cr.setLayout (GBL = new GridBagLayout ());
      GBC = new GridBagConstraints ();
   }

/** Set default padding x and y,
 * the amount of space that will be added to Component requests.
 * @param x -- the default padx.
 * @param y -- the default pady.
 */

   public void setDefaultPads (int x, int y) {
       default_ipadx = x;
       default_ipady = y;
   }

/** Set default fill policy,
 * what a component will do with excess space it is assigned.
 * @param fill -- one of GridBagConstraints constants:
 * NONE, HORIZONTAL, VERTICAL, BOTH */

    public void setDefaultFill (int fill) {
	default_fill = fill;
    }

/** Set default weights x and y.
 * Weights (by convention between 0.0 and 1.0) apportion extra space,
 * default zero means give any extra space to something else.
 */

   public void setDefaultWeights (double wx, double wy) {
       default_weightx = wx;
       default_weighty = wy;
   }

/** Set default insets,
 * the spaces that layout will leave around Components,
 * that will remain as the Panel background.
 * @param top -- the default top inset.
 * @param left -- the default left inset.
 * @param bottom -- the default bottom inset.
 * @param right -- the default right inset.
 */

   public void setDefaultInsets (int top, int left, int bottom, int right) {
       default_insets = new Insets (top, left, bottom, right);
   }

/** Set default insets,
 * the spaces that layout will leave around Components,
 * that will remain as the Panel background.
 * @param insets -- the default Insets (top, left, bottom, right).
 */

   public void setDefaultInsets (Insets insets) {
       default_insets = insets;
   }

/** Add a Component to the Container with this Helper.
 * @param ct -- the Component to add.
 * @param col -- the layout column.
 * @param row -- the layout row.
 * @param width -- the number of layout columns to span, default one.
 * @param height -- the number of layout rows to span, default one.
 * @param fill -- how the Component should make use of extra space if any.
 * @param weightx -- relative desire for extra width if available.
 * @param weighty -- relative desire for extra height if available.
 */

   public Component addGB (Component ct, int col, int row, int width, int height, int fill, double weightx, double weighty) {
	GBC.gridx = col;
	GBC.gridy = row;
	GBC.gridwidth = width;
	GBC.gridheight = height;
	GBC.fill = fill;
	GBC.weightx = weightx;
	GBC.weighty = weighty;
        GBC.insets = default_insets;
	GBC.ipadx = default_ipadx;
	GBC.ipady = default_ipady;
	GBL.setConstraints (ct, GBC);
	return cr.add (ct);
    }

/** Add a Component to the Container with this Helper.
 * @param ct -- the Component to add.
 * @param col -- the layout column.
 * @param row -- the layout row.
 * @param weightx -- relative desire for extra width if available.
 * @param weighty -- relative desire for extra height if available.
 */

   public Component addGB (Component ct, int col, int row, double weightx, double weighty) {
       return addGB (ct, col, row, 1, 1, default_fill, weightx, weighty);
   }

/** Add a Component to the Container with this Helper.
 * @param ct -- the Component to add.
 * @param col -- the layout column.
 * @param row -- the layout row.
 */

   public Component addGB (Component ct, int col, int row) {
       return addGB (ct, col, row, 1, 1, default_fill, default_weightx, default_weighty);
   }

/* WE NEED A FEW CUSTOMIZED ONES WITH USEFUL NAMES,
  e.g. these use RELATIVE for other axis --
  addRow (Component ct, int col, ...)
  addColumn (Component ct, int row, ...)
  */

}
/* <IMG SRC="/cgi-bin/counter">*/
