// Copyright (c) 1995, 1996 Regents of the University of California.
// All rights reserved.
//
// This software was developed by the Arcadia project
// at the University of California, Irvine.
//
// Redistribution and use in source and binary forms are permitted
// provided that the above copyright notice and this paragraph are
// duplicated in all such forms and that any documentation,
// advertising materials, and other materials related to such
// distribution and use acknowledge that the software was developed
// by the University of California, Irvine.  The name of the
// University may not be used to endorse or promote products derived
// from this software without specific prior written permission.
// THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
// IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
// WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.

// File: PropsGridLayout.java
// Interfaces: PropsGridLayout
// Original Author: jrobbins@ics.uci.edu
// $Id: PropsGridLayout.java,v 1.4 1997/06/10 23:39:25 jrobbins Exp $

// Modified by : Kedar Patankar
// Last Modified 12 Oct 1997

package EDU.neu.ccs.demeter.tools.apstudio.ui;

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

/** A layout manager used by the PropSheets. Property names are in a
 *  fixed width column on the left. Property values are in an
 *  infinately wide column on the right. Rows are as tall as needed to
 *  display a value. */

public class PropsGridLayout implements LayoutManager 
{
	private int hgap,vgap;
	private int _minColumnWidth=150,_minRowHeight = 20;

	public PropsGridLayout(int hgap, int vgap) 
	{
		this.hgap = hgap;
		this.vgap = vgap;
	}

  /** Adds the specified component with the specified name to the layout.
   *
   * @param name the name of the component
   * @param comp the component to be added
   */
	public void addLayoutComponent(String name, Component comp){}

  /** Removes the specified component from the layout. Does not apply.
   *
   * @param comp the component to be removed
   */
	public void removeLayoutComponent(Component comp){}

  /** Returns the preferred dimensions for this layout given the components
   *  int the specified panel.
   *
   * @param parent the component which needs to be laid out
   * @see #minimumLayoutSize
   */
	public Dimension preferredLayoutSize(Container parent)
	{
		Insets insets = parent.getInsets();
		int ncomponents = parent.getComponentCount();
		int w = _minColumnWidth * 2 + hgap;
		int h = 0;
		for (int i = 0 ; i < ncomponents ; i++)
		{
			Component comp = parent.getComponent(i);
			Dimension d = comp.getPreferredSize();
			h += d.height + vgap;
		}
		return new Dimension(insets.left + insets.right + w,insets.top + insets.bottom + h);
	}

  /**
   * Returns the minimum dimensions needed to layout the components
   * contained in the specified panel.
   *
   * @param parent the component which needs to be laid out
   * @see #preferredLayoutSize
   */
	public Dimension minimumLayoutSize(Container parent)
	{
		Insets insets = parent.getInsets();
		int ncomponents = parent.getComponentCount();
		int w = _minColumnWidth * 2 + hgap;
		int h = 0;
		for (int i = 0 ; i < ncomponents ; i++)
		{
			Component comp = parent.getComponent(i);
			Dimension d = comp.getMinimumSize();
			h += d.height + vgap;
		}
		return new Dimension(insets.left + insets.right + w,insets.top + insets.bottom + h);
	}

  /** Lays out the container in the specified panel.
   *
   * @param parent the specified component being laid out
   * @see Container
   */
	public void layoutContainer(Container parent)
	{
		Insets insets = parent.getInsets();
		int ncomponents = parent.getComponentCount();
		if (ncomponents == 0) return;
		int used = insets.left + insets.right + hgap;
		int columnWidth = Math.max((parent.getSize().width - used)/2 , _minColumnWidth );

		int h = 0;
		for (int i = 0 ; i < ncomponents ; i++)
		{
			Component comp = parent.getComponent(i);
			Dimension d = comp.getPreferredSize();
			int rowHeight = Math.max(d.height, _minRowHeight);
			if (i %2 == 0) comp.setBounds(0, h, columnWidth,rowHeight);
			else 
			{
				comp.setLocation(columnWidth + hgap, h);
				comp.setSize(columnWidth,rowHeight);
				//comp.resize(Math.min(d.width, propWidth),
				//	  Math.min(d.height, _labelHeight));
				h += rowHeight + vgap;
			}
		}
	}

} /* end class PropsGridLayout */

