package j3deditor.bin.components;

import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.LayoutManager;


/**
 * The <code>ButtonPanelLayout</code> class is a layout manager
 * that lays out a container's components in one row/column.
 * The container is divided into equal-sized rectangles, which
 * width and height can be specified.
 *
 * @author Risto Seene
 */
public class ButtonPanelLayout implements LayoutManager{


	/** specifies that container's components are layd out in a row. */
	public static final int HORIZONTAL = 0;

	/** specifies that container's components are layd out in a column. */
	public static final int VERTICAL = 1;

	/** specifies that container's components are layd out from top/left to bottom/right. */
	public static final int POSITIVE = 2;

	/** specifies that container's components are layd out from bottom/right to top/left. */
	public static final int NEGATIVE = 3;


	/** width of components. */
	private int width;

	/** height of components. */
	private int height;

	/** vertical space between components. */
	private int vspace = 0;

	/** horizontal space between components. */
	private int hspace = 0;

	/** type of this layout manager: HORIZONTAL/VERTICAL. */
	private int type;

	/** direction of components: POSITIVE/NEGATIVE. */
	private int direction;


	/**
	 * Creates an instance of <code>ButtonPanelLayout</code>
	 * with specified width and height of components and
	 * layout order.
	 *
	 * @param width
	 *	width of componets
	 * @param height
	 *	height of components
	 * @param type
	 *	the way the components are layd out: HORIZONTAL, VERTICAL
	 * @param direction
	 * 	direction of components: POSITIVE, NEGATIVE
	 */
	public ButtonPanelLayout(int width, int height, int type, int direction){
		this.width = width;
		this.height = height;
		this.type = type;
		this.direction = direction;
	}


	/**
	 * Creates an instance of <code>ButtonPanelLayout</code>
	 * with specified width and height of components,
	 * space between them and layout order.
	 *
	 * @param width
	 *	width of componets
	 * @param height
	 *	height of components
	 * @param space
	 *	space between components
	 * @param type
	 *        the way the components are layd out: HORIZONTAL, VERTICAL
	 * @param direction
	 *        direction of components: POSITIVE, NEGATIVE
	 */
	public ButtonPanelLayout(int width, int height, int space, int type, int direction){
		this(width, height, type, direction);
		this.vspace = space;
		this.hspace = space;
	}


	/**
	 * Creates an instance of <code>ButtonPanelLayout</code>
	 * with specified width and height of components,
	 * space between them and layout order.
	 *
	 * @param width
	 *	width of componets
	 * @param height
	 *	height of components
	 * @param vspace
	 *	vertical space between components
	 * @param hspace
	 *	horizontal space between components
	 * @param type
	 *        the way the components are layd out: HORIZONTAL, VERTICAL
	 * @param direction
	 *        direction of components: POSITIVE, NEGATIVE
	 */
	public ButtonPanelLayout(int width, int height, int vspace, int hspace, int type, int direction){
		this(width, height, type, direction);
		this.vspace = vspace;
		this.hspace = hspace;
	}


	/**
	 * Required by LayoutManager, not in use.
	 * 
	 * @param name
	 * 	doesn't do anything
	 * @param comp
	 * 	doesn't do anything
	 */
	public void addLayoutComponent(String name, Component comp){
	}

	/**
	 * Required by LayoutManager, not in use.
	 * 
	 * @param comp
	 * 	doesn't do anything
	 */
	public void removeLayoutComponent(Component comp){
	}

	/**
	 * Required by LayoutManager, not in use.
	 * 
	 * @param parent
	 * 	doesn't do anything
	 * @return
	 * 	<code>null</code>
	 */
	public Dimension preferredLayoutSize(Container parent){
		return null;
	}

	/**
	 * Required by LayoutManager, not in use.
	 * 
	 * @param parent
	 * 	doesn't do anything
	 * @return
	 * 	<code>null</code>
	 */
	public Dimension minimumLayoutSize(Container parent){
		return null;
	}

	/**
	 * Required by LayoutManager.
	 * Lays out the components into the given container.
	 *
	 * @param parent
	 *	container which components will be layd out
	 */
	public void layoutContainer(Container parent){

		int numComps = parent.getComponentCount();

		for(int i = 0; i < numComps; i++){
			Component c = parent.getComponent(i);

			if(type == 0){
				if(direction == 2){
					c.setBounds(i * (width + vspace) + vspace, hspace, width, height);
				} else {
					c.setBounds(parent.getWidth() - (i + 1) * (width + vspace), hspace, width, height);
				}
			} else if(type == 1){
				if(direction == 2){
					c.setBounds(vspace, i * (height + hspace) + hspace, width, height);
				} else {
					c.setBounds(vspace, parent.getHeight() - (i + 1) * (height + hspace), width, height);
				}
			}
		}

	}
}

