package org.openspml.browser;

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.font.*;
import javax.swing.*;
import javax.swing.event.*;

/**
 * A simple layout manager that packs components together on a single line.
 * Functinally similar to GridLayout with one row, but the cells don't have to
 * be the same size.
 * <p>
 * This is almost the same as sun.awt.HorizBagLayout but that is not
 * always accessible?
 * <p>
 * It also looks pretty much like the Swing BoxLayout, but supports a gap
 * The Box class can accomplish a gap using a "strut" object, which is a bit
 * more flexible.
 */
public class LinearLayout implements LayoutManager {

    public static final int HORIZONTAL = 0;
    public static final int VERTICAL = 1;

    int _orientation;
    int _gap;

    public LinearLayout() {
        _orientation = HORIZONTAL;
    }

    public LinearLayout(int orientation) {
        _orientation = orientation;
    }

    public LinearLayout(int orientation, int gap) {
	_orientation = orientation;
	_gap = gap;
    }

    public void addLayoutComponent(String name, Component c) {
    }

    public void removeLayoutComponent(Component c) {
    }

    public Dimension minimumLayoutSize(Container target) {
	
	int width = 0;
	int height = 0;

	int count = target.getComponentCount();
	for (int i = 0 ; i < count ; i++) {
	    Component c = target.getComponent(i);

	    Dimension d = c.getMinimumSize();
		
	    if (_orientation == HORIZONTAL) {
		height = Math.max(height, d.height);
		width += d.width;
		width += _gap;
	    }
	    else {
		width = Math.max(width, d.width);
		height += d.height;
		height += _gap;
	    }
	}

	return new Dimension(width, height);
    }

    public Dimension preferredLayoutSize(Container target) {
	
	int width = 0;
	int height = 0;

	int count = target.getComponentCount();
	for (int i = 0 ; i < count ; i++) {
	    Component c = target.getComponent(i);

	    Dimension d = c.getPreferredSize();
		
	    if (_orientation == HORIZONTAL) {
		height = Math.max(height, d.height);
		width += d.width;
		width += _gap;
	    }
	    else {
		width = Math.max(width, d.width);
		height += d.height;
		height += _gap;
	    }
	}

	return new Dimension(width, height);
    }

    public void layoutContainer(Container target) {

	// determine our available area
	Insets insets = target.getInsets();
	int left = insets.left;
	int top = insets.top;
	int right = target.getSize().width - insets.right;
	int bottom = target.getSize().height - insets.bottom;
	int width = right - left + 1;
	int height = bottom - top + 1;

	int xpsn = left;
	int ypsn = top;

	// not very smart about resizing, just assume the components
	// advertise a reasonable preferred size
	
	int count = target.getComponentCount();
	for (int i = 0 ; i < count ; i++) {
	    Component c = target.getComponent(i);
	    if (c.isVisible()) {
		Dimension d = c.getPreferredSize();

		int cwidth = d.width;
		int cheight = d.height;

		if (_orientation == HORIZONTAL) {
		    // jsl -  formerly let these fill the
		    // space alotted, but I'm not sure I like that
		    // should recognize the alignment constants
		    // if (cheight < height)
		    // cheight = height;
		}
		else {
		    // if (cwidth < width)
		    // cwidth = width;
		}

		c.setBounds(xpsn, ypsn, cwidth, cheight);

		if (_orientation == HORIZONTAL) {
		    xpsn += cwidth;
		    xpsn += _gap;
		}
		else {
		    ypsn += cheight;
		    ypsn += _gap;
		}
	    }
	}
    }

}
