/** (C) 1999 World Xiangqi League, Confidential, All Rights Reserved */

import java.applet.*;
import java.awt.*;
import java.net.*;

class FormLayout extends GridBagLayout {
  private Container container;
  private int columns, rows;
  private GridBagConstraints bc;
  
  private final static String fillSet = "NHVB";
  private final static int[]  fillVal = {
    GridBagConstraints.NONE, GridBagConstraints.HORIZONTAL,
    GridBagConstraints.VERTICAL, GridBagConstraints.BOTH
  };
  private final static String anchorSet = "CNESW";
  private final static int[]  anchorVal = {
    GridBagConstraints.CENTER,
    GridBagConstraints.NORTH,
    GridBagConstraints.EAST,
    GridBagConstraints.SOUTH,
    GridBagConstraints.WEST
  };

  /*
    FormLayout layout = new FormLayout(this,5,1);
    //         component,   pos,span,fill,stretch,anchor
    layout.add(new Label(), 5,1, 1,1, 'H', 5,0, 'C');
   */
  public FormLayout(Container container, int columns, int rows) {
    super();
    this.container = container;
    this.columns   = columns;
    this.rows      = rows;
    this.bc        = new GridBagConstraints();
    container.setLayout(this);
  }
  
  public void add(Component component, int xpos,int ypos, int xspan,int yspan,
             char fill, double xgrow,double ygrow, char anchor) {
    bc.gridx     = xpos;  bc.gridy = ypos;
    bc.gridwidth = xspan; bc.gridheight = yspan;
    // GridBagConstraints.REMAINDER = 0
    // GridBagConstraints.RELATIVE = -1
    bc.fill      = fillVal[fillSet.indexOf(fill)];
    bc.weightx   = xgrow; bc.weighty = ygrow;
    bc.anchor    = anchorVal[anchorSet.indexOf(anchor)];
    validate(component,bc);
    this.setConstraints(component,bc);
    container.add(component);
    //Club.debug(component+": "+constraintsToString(bc));
  }

  private static String constraintsToString(GridBagConstraints bc) {
    return "pos="+bc.gridx+","+bc.gridy+
           ", span="+bc.gridwidth+","+bc.gridheight+
           ", fill="+bc.fill+
           ", stretch="+bc.weightx+","+bc.weighty+
           ", anchor="+bc.anchor;
  }
    
  private void validate(Component component, GridBagConstraints bc) {
    assert(bc.gridx>=1 && bc.gridx<=columns);
    assert(bc.gridy>=1 && bc.gridy<=rows);
    assert(bc.gridwidth>=-1 && bc.gridwidth<=columns);
    assert(bc.gridheight>=-1 && bc.gridheight<=rows);
    //assert(fillSet.indexOf(bc.fill)>=0);
    assert(bc.weightx>=0 && bc.weightx<=100);
    assert(bc.weighty>=0 && bc.weighty<=100);
    //assert(anchorSet.indexOf(bc.anchor)>=0);
  }
  private static void assert(boolean truth) {
    if (!truth)
      Club.debug("FormLayout bug");
  }
}
