/*****************************************************************************\
                        Copyright (C) 2007 Sean D Reilly                     

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:
 
 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
\*****************************************************************************/

package com.moneydance.awt;

import java.awt.GridBagConstraints;

/** A class that makes building GridBagConstraints objects easy, efficient and
 *  readable for layout out GUI elements using the GridBagLayout layout manager
 *  To use this, call GridC.getc() and then call any of the methods below on the
 *  result in order to set different attributes for the component that is being
 *  laid out.  For example:
 *  <pre>
 *     JPanel p = new JPanel(new GridBagLayout());
 *     p.add(myLabel, GridC.getc(0,0).east());
 *     p.add(myField, GridC.getc(1,0).wx(1).fillx().colspan(2));
 *     p.add(myLabel2, GridC.getc(0,1).east());
 *     p.add(myField2, GridC.getc(1,1).wx(1).fillx());
 *     p.add(myButton2, GridC.getc(1,2));
 *  </pre>
 */
public class GridC 
  extends GridBagConstraints
{
  private static GridC staticRef = new GridC();
  
  /** Reset and return the singleton GridC instance */
  public static final GridC getc() {
    return staticRef.reset();
  }
  
  /** Reset and return the singleton GridC instance, with x and y initialized to
   *  the given values.  This is a shortcut to GridC.getc().xy(x,y) */
  public static final GridC getc(int x, int y) {
    return staticRef.reset().xy(x, y);
  }
  
  /** Reset all of the constraints to the default value and return ourself. */
  public GridC reset() {
    anchor = CENTER;
    fill = NONE;
    gridheight = 1;
    gridwidth = 1;
    gridx = RELATIVE;
    gridy = RELATIVE;
    insets.top = 0;
    insets.left = 0;
    insets.bottom = 0;
    insets.right = 0;
    ipadx = 0;
    ipady = 0;
    weightx = 0;
    weighty = 0;
    return this;
  }
  
  /** Set our gridx value and return self */
  public GridC x(int x) { this.gridx = x; return this; }
  
  /** Set our gridx value and return self */
  public GridC y(int y) { this.gridy = y; return this; }
  
  /** Set both the gridx and gridy values and return self */
  public GridC xy(int x, int y) { this.gridx = x; this.gridy = y; return this; }
  
  /** Set our weightx value and return self */
  public GridC wx(float xWeight) { this.weightx = xWeight; return this; }
  
  /** Set our weighty value and return self */
  public GridC wy(float yWeight) { this.weighty = yWeight; return this; }
  
  /** Set our weightx and weighty value and return self */
  public GridC wxy(float xWeight, float yWeight) {
    this.weightx = xWeight;
    this.weighty = yWeight;
    return this;
  }
  
  /** Set the gridwidth: number of columns to span. */
  public GridC colspan(int numColumns) { this.gridwidth = numColumns; return this; }
  
  /** Set the gridheight: number of rows to span. */
  public GridC rowspan(int numRows) { this.gridheight = numRows; return this; }
  
  /** Set the fill type to horizontal */
  public GridC fillx() { this.fill = HORIZONTAL; return this; }
  
  /** Set the fill type to vertical */
  public GridC filly() { this.fill = VERTICAL; return this; }
  
  /** Set the fill type to both horizontal and vertical */
  public GridC fillboth() { this.fill = BOTH; return this; }
  
  /** Set the fill type to no filling (the default) */
  public GridC fillnone() { this.fill = NONE; return this; }
  
  /** Set the insets */
  public GridC insets(int top, int left, int bottom, int right) {
    insets.top = top;
    insets.left = left;
    insets.bottom = bottom;
    insets.right = right;
    return this;
  }
  
  /** Anchor the component to the east */
  public GridC east() { this.anchor = EAST; return this; }
  
  /** Anchor the component to the west */
  public GridC west() { this.anchor = WEST; return this; }
  
  /** Anchor the component to the north */
  public GridC north() { this.anchor = NORTH; return this; }
  
  /** Anchor the component to the south */
  public GridC south() { this.anchor = SOUTH; return this; }
  
  /** Anchor the component to the northeast */
  public GridC northEast() { this.anchor = NORTHEAST; return this; }
  
  /** Anchor the component to the northwest */
  public GridC northWest() { this.anchor = NORTHWEST; return this; }
  
  /** Anchor the component to the southeast */
  public GridC southEast() { this.anchor = SOUTHEAST; return this; }
  
  /** Anchor the component to the southwest */
  public GridC southWest() { this.anchor = SOUTHWEST; return this; }
  
  /** Anchor the component to the center (the default) */
  public GridC center() { this.anchor = CENTER; return this; }
  
  /** Set the internal padding along the X axis */
  public GridC padx(int xPadding) { this.ipadx = xPadding; return this; }

  /** Set the internal padding along the Y axis */
  public GridC pady(int yPadding) { this.ipady = yPadding; return this; }
  
  
  /** Shortcut for the settings associated with a label in a standard dialog */
  public GridC label() { return east().insets(2,0,2,2); }
  
  /** Shortcut for the constraints associated with a field in a standard dialog */
  public GridC field() { return wx(1).fillx().insets(2,2,2,0); }
  
  
}

