/*
 * $Source: c:\\cvshome/website/explicit_2_0/FormDemo.java,v $
 * $Revision: 1.1 $
 * $Date: 2001/08/08 12:18:56 $
 *
 * Copyright (c) 2001 Zooki Technologies. All rights reserved.
 *
 */
package com.zookitec.layout.demo;

import java.awt.*;
import java.awt.event.*;
import com.zookitec.layout.*;

/**
 * Demonstrates how to create a simple form.
 */
public class FormDemo extends Container {


    private static final int HGAP = 20;
    private static final int VGAP = 10;

    private ExplicitConstraints defaultConstraints = new ExplicitConstraints();

    private DemoLabel titleLabel = new DemoLabel("Title");
    private DemoLabel firstnameLabel = new DemoLabel("First Name");
    private DemoLabel surnameLabel = new DemoLabel("Surname");
    private DemoLabel emailLabel = new DemoLabel("E-mail");
    private DemoLabel commentLabel = new DemoLabel("Comment");

    private DemoField titleField = new DemoField("Mr", 30, 21); // preferred size 30, 21
    private DemoField firstnameField = new DemoField("Joe", 80, 21);
    private DemoField surnameField = new DemoField("Bloggs", 80, 21);
    private DemoField commentField = new DemoField("bla bla bla bla bla", 200, 60);
    private DemoField emailField = new DemoField("info@zookitec.com", 200, 21);

    private ExplicitLayout layout = new ExplicitLayout();

    public FormDemo() {
        setLayout(layout);


        //A group of components is defined using an array.
        Component [] labelGroup = {titleLabel, firstnameLabel, surnameLabel, emailLabel, commentLabel};

        //Set the default x coordinate to a fixed distance from the left of the container.
        defaultConstraints.setX(ContainerEF.left().add(20));

        //Add the first label a fixed distance from the top of the container.
        ExplicitConstraints ec = new ExplicitConstraints(titleLabel, defaultConstraints);
        ec.setY(ContainerEF.top().add(20));
        add(titleLabel, ec);

        //Add the remaining labels, each VGAP pixels below the previous label and field.
        add(firstnameLabel, createBelowConstraints(firstnameLabel, titleLabel, titleField, VGAP));
        add(surnameLabel, createBelowConstraints(surnameLabel, firstnameLabel, firstnameField, VGAP));
        add(commentLabel, createBelowConstraints(commentLabel, surnameLabel, surnameField, VGAP));
        add(emailLabel, createBelowConstraints(emailLabel, commentLabel, commentField, VGAP));

        //Sets the default x coordinate to GAP pixels to the right of
        //the group of label components (see labelGroup defined above).
        defaultConstraints.setX(GroupEF.right(labelGroup).add(HGAP));

        //Add the field components so that each is alined with its label.
        add(titleField,  createAlignConstraints(titleField, titleLabel));
        add(firstnameField, createAlignConstraints(firstnameField, firstnameLabel));
        add(surnameField, createAlignConstraints(surnameField, surnameLabel));
        add(commentField, createAlignConstraints(commentField, commentLabel));
        add(emailField, createAlignConstraints(emailField, emailLabel));

    }

    /**
     * Creates constraints for component c such that it is positioned
     * gap pixels below the lowest bottom of components a1 and a2.
     * This can be used to position a label, gap pixels below both the label
     * and field above it; the field may have a different height to the label.
     *
     */
    public ExplicitConstraints createBelowConstraints(Component c, Component above1, Component above2, int gap) {
        ExplicitConstraints ec = new ExplicitConstraints(c, defaultConstraints);
        ec.setY(MathEF.max(ComponentEF.bottom(above1),
                           ComponentEF.bottom(above2)).add(gap));
        return ec;
    }

    /**
     * Creates constraints for the specified field such that its top
     * is aligned with the top of the specified label.
     *
     */
    public ExplicitConstraints createAlignConstraints(Component field, Component label) {
        ExplicitConstraints ec = new ExplicitConstraints(field, defaultConstraints);
        ec.setY(ComponentEF.top(label));
        return ec;
    }

}


