package com.sun.solarisinstall.panels;

// Copyright (c) 1996 Sun Microsystems, Inc. All Rights Reserved.

import java.awt.*;

/**
 * <CODE>RowLayout</CODE> lays out components in a single row. The
 * row is as tall as the tallest component.<P>
 * 
 * @version	1.5	08/04/96
 * @author	Sun Microsystems, Inc.
 */
public class RowLayout implements LayoutManager {
    private static final String sccs_id = "@(#)RowLayout.java 1.10 96/10/24 SMI";

    LAYOUT_ALIGNMENT align;
    int hgap;
    int vgap;

    /**
     * Constructs a new <CODE>RowLayout</CODE> with a expand alignment.
     */
    public RowLayout() {
	this(LAYOUT_ALIGNMENT.EXPAND, 5, 5);
    }

    /**
     * Constructs a new <CODE>RowLayout</CODE> with the specified alignment.
	 *
     * @param <VAR>align</VAR> The alignment value.
     */
    public RowLayout(LAYOUT_ALIGNMENT align) {
	this(align, 5, 5);
    }

    /**
     * Constructs a new <CODE>RowLayout</CODE> with the specified
     * alignment and gap values.
	 *
     * @param <VAR>align</VAR> The alignment value.
     * @param <VAR>hgap</VAR> The horizontal gap variable.
     * @param <VAR>vgap</VAR> The vertical gap variable.
     */
    public RowLayout(LAYOUT_ALIGNMENT align, int hgap, int vgap) {
	this.align = align;
	this.hgap = hgap;
	this.vgap = vgap;
    }

    public void addLayoutComponent(String name, Component comp) {}
    public void removeLayoutComponent(Component comp) {}

	public int              getHorizontalGap() { return hgap;  }
	public int              getVerticalGap  () { return hgap;  }
	public LAYOUT_ALIGNMENT getAlignment    () { return align; }

    /**
     * Returns the preferred dimensions for this layout given the components
     * in the specified target container.
	 *
     * @param <VAR>target</VAR> The component that needs to be laid out.
     * @see java.awt.Container
     * @see #minimumLayoutSize
     */
    public Dimension preferredLayoutSize(Container target) {
	Dimension dim = new Dimension(0, 0);
	int nmembers = target.getComponentCount();

	for (int i = 0 ; i < nmembers ; i++) {
	    Component m = target.getComponent(i);
	    if (m.isVisible()) {
		Dimension d = m.getPreferredSize();
		dim.height = Math.max(dim.height, d.height);
		dim.width += d.width;
	    }
	}
	Insets insets = target.getInsets();
	dim.width  += insets.left + insets.right + (nmembers-1)*hgap;
	dim.height += insets.top + insets.bottom;
	return dim;
    }

    /**
     * Returns the minimum dimensions needed to layout the components
     * contained in the specified target container.
	 *
     * @param <VAR>target</VAR> The component that needs to be laid out.
     * @see #preferredLayoutSize
     */
    public Dimension minimumLayoutSize(Container target) {
	Dimension dim = new Dimension(0, 0);
	int nmembers = target.getComponentCount();

	for (int i = 0 ; i < nmembers ; i++) {
	    Component m = target.getComponent(i);
	    if (m.isVisible()) {
		Dimension d = m.getMinimumSize();
		dim.height = Math.max(dim.height, d.height);
		dim.width += d.width;
	    }
	}
	Insets insets = target.getInsets();
	dim.width  += insets.left + insets.right + (nmembers-1)*hgap;
	dim.height += insets.top + insets.bottom;
	return dim;
    }

    /**
     * Lays out the container.  Components are packed horizontally
     * into a single row and made as tall as the tallest component.
	 *
     * @param <VAR>target</VAR> The specified component being laid out.
     * @see java.awt.Container
     */
    public void layoutContainer(Container target) {
		Insets insets = target.getInsets();
		Dimension dim = target.getSize();
		int nmembers = target.getComponentCount();
		int x = insets.left, y = insets.top;
		int yoff = 0;
		int tallest = 0;

		// find tallest component
		for (int i = 0 ; i < nmembers ; i++) {
			Component m = target.getComponent(i);
			if (m.isVisible()) {
				Dimension d = m.getPreferredSize();
				tallest = Math.max(tallest, d.height);
			}
		}

		// lay 'em out
		for (int i = 0 ; i < nmembers ; i++) {
	   		Component m = target.getComponent(i);
			Dimension d = m.getPreferredSize();

			int targetHeight = dim.height - insets.top - insets.bottom;
			int compHeight   = d.height, compWidth = d.width;

			if (m.isVisible()) {
				if (align == LAYOUT_ALIGNMENT.LEFT) 
					yoff = 0;
				
				else if (align == LAYOUT_ALIGNMENT.CENTER) 
					yoff = (targetHeight - compHeight) / 2;
				
				else if (align == LAYOUT_ALIGNMENT.RIGHT) 
					yoff = targetHeight - compHeight;
				
				else if (align == LAYOUT_ALIGNMENT.EXPAND) {
					yoff = 0;
					compHeight = tallest;
				}
				else if (align == LAYOUT_ALIGNMENT.FIT) {
					yoff = 0;
					compHeight = targetHeight;
				}
				m.setBounds(x, y+yoff, compWidth, compHeight);
				x += hgap + d.width;
			}
		}
    }

    /**
     * Returns the <CODE>String</CODE> representation of this
     * <CODE>RowLayout</CODE>'s values.
     */
    public String toString() {
	String str = "";
	if      (align == LAYOUT_ALIGNMENT.LEFT)   str = ",align=left";
	else if (align == LAYOUT_ALIGNMENT.CENTER) str = ",align=center";
	else if (align == LAYOUT_ALIGNMENT.RIGHT)  str = ",align=right";
	else if (align == LAYOUT_ALIGNMENT.EXPAND) str = ",align=expand";
	return getClass().getName() + "[hgap=" + hgap + ",vgap=" + vgap + str + "]";
    }

    /**
     * Returns the version of this class.
     */
    public String getClassVersion() {
        return (sccs_id);
    }
}
