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

package com.sun.solarisinstall.panels;

import java.awt.*;

/**
 * This class is the layout manager for a <CODE>Scroller</CODE>.<p>
 *
 * It lays out four <CODE>Components</CODE>: a horizontal scrollbar,
 * a vertical scrollbar, column header panel, and a viewport.<p>
 *
 * Valid names and <CODE>Components</CODE> that can be added using 
 * <CODE>addLayoutComponent(String, Component)<CODE> are:<P>
 *
 * <PRE>
 * "HorizontalScrollbar" Scrollbar
 * "VerticalScrollbar"   Scrollbar
 * "Scroll"              Component (viewport)
 * "Header"              Component (column header panel)
 * </PRE>
 *
 * @version	1.10	10/02/96
 * @author	Sun Microsystems, Inc.
 * @see		sunw.admin.avm.base.Scroller
 */
public class ScrollerLayout implements LayoutManager {
    private static final String sccs_id = "@(#)ScrollerLayout.java 1.10 96/10/02 SMI";

    private Scroller scroller;
    private Scrollbar hbar, vbar;
    private Component viewport;
    private Component label;
    private Component pinnedColumn;
    private int border = 2;

    /**
     * Constructs a <CODE>ScrollerLayout</CODE> with a centered alignment.
	 *
     * @param <VAR>scroller</VAR> The associated scroller.
     */
    public ScrollerLayout(Scroller scroller) {
	this.scroller = scroller;
    }

    /**
     * Adds the specified component to the layout. Does not apply.
	 *
     * @param <VAR>name</VAR> The name of the component.
     * @param <VAR>comp</VAR> The the component to be added.
     */
    public void addLayoutComponent(String name, Component comp) {
	if ("VerticalScrollbar".equals(name)) {
		if (comp instanceof Scrollbar) {
			vbar = (Scrollbar)comp;
			return;
		}
	}
	else if ("HorizontalScrollbar".equals(name)) {
		if (comp instanceof Scrollbar) {
			hbar = (Scrollbar)comp;
			return;
		}
	}
	else if ("Scroll".equals(name)) { viewport = comp; return; }
	else if ("Header".equals(name)) { label = comp; return; }
	else if ("Pin".equals(name))    { pinnedColumn = comp; return; }

	throw new IllegalArgumentException("Illegal scroll component");
    }

    /**
     * Removes the specified component from the layout. Does not apply.
	 *
     * @param <VAR>comp</VAR> The component to remove.
     */
    public void removeLayoutComponent(Component comp) {
	if      (comp == vbar)         { vbar = null; }
	else if (comp == hbar)         { hbar = null; }
	else if (comp == viewport)     { viewport = null; }
	else if (comp == label)        { label = null; }
	else if (comp == pinnedColumn) { pinnedColumn = null; }
    }

    /**
     * Returns the preferred dimensions for this layout given the
     * components in the specified target container.
	 *
     * @param <VAR>target</VAR> The component which needs to be laid out
     * @see java.awt.Container
     * @see #minimumLayoutSize
     */
    public Dimension preferredLayoutSize(Container target) {
	return target.getSize();
    }

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

	if ((vbar != null) && (vbar.isVisible())) {
		Dimension d = vbar.getMinimumSize();
		dim.width += d.width;
	}
	if ((hbar != null) && (hbar.isVisible())) {
		Dimension d = hbar.getMinimumSize();
		dim.height += d.height;
	}
	if ((label != null) && (label.isVisible())) {
		Dimension d = label.getMinimumSize();
		dim.height += d.height;
	}

	Insets insets = target.getInsets();
	dim.width  += insets.left + insets.right + 2*border;
	dim.height += insets.top + insets.bottom + 2*border;
	return dim;
    }

    /**
     * Lays out the container. This method reshapes the components in
     * target to satisfy the constraints of the <CODE>ScrollerLayout</CODE>
     * object.
	 *
     * @param <VAR>target</VAR> The specified component being laid out.
     * @see java.awt.Container
     * @see sunw.admin.avm.base.Scroller
     */
    public void layoutContainer(Container target) {
	Dimension dim = target.getSize();
	Dimension ldim = new Dimension(0, 0);
	Dimension pdim = new Dimension(0, 0);
	Dimension vdim = new Dimension(0, 0);
	Dimension hdim = new Dimension(0, 0);

	scroller.manageScrollbars();

	if ((vbar != null) && vbar.isVisible())
		vdim = vbar.getPreferredSize();
	if ((hbar != null) && hbar.isVisible())
		hdim = hbar.getPreferredSize();

	if ((label != null) && label.isVisible()) {
		ldim = label.getPreferredSize();
		label.setBounds(0, 0, dim.width-vdim.width, ldim.height);
	}

	if ((pinnedColumn != null) && pinnedColumn.isVisible()) {
		pdim = pinnedColumn.getPreferredSize();
		pinnedColumn.setBounds(0, ldim.height+border, pdim.width,
			dim.height-ldim.height-hdim.height-(2*border));
	}
	if ((viewport != null) && viewport.isVisible()) {
		viewport.setBounds(pdim.width+border, ldim.height+border,
			dim.width-pdim.width-vdim.width-(2*border)-1,
			dim.height-ldim.height-hdim.height-(2*border)-1);
	}
	if ((vbar != null) && vbar.isVisible()) {
		vbar.setBounds(dim.width - vdim.width, ldim.height,
			vdim.width, dim.height-ldim.height-hdim.height);
	}
	if ((hbar != null) && hbar.isVisible()) {
		hbar.setBounds(pdim.width, dim.height - hdim.height,
			dim.width-pdim.width-vdim.width, hdim.height);
	}
    }

    /**
     * Returns the <CODE>String</CODE> representation of this
     * <CODE>ScrollerLayout</CODE>'s values.
     */
    public String toString() {
	return getClass().getName() + "[scroll=" + viewport + "]";
    }

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