package cnp.ew.tdemo;

import java.util.*;
import java.awt.*;

import cnp.ew.properties.*;
import cnp.ew.diagram.*;
import cnp.ew.converter.*;
import cnp.ew.displayer.*;
import cnp.ew.util.*;

// This would probably be done in the real world as a CpPersonLc with a CpPerson model,
// The lc would pass along all the property info, but it would implement the painting & diagramming stuff

public class CpPersonLc extends CpAbstractDiagrammableLc
{
    public final static int NAME_PROP = 0;
    public final static int AGE_PROP = 1;
    public final static int BIRTHDAY_PROP = 2;
    public final static int MARRIED_PROP = 3;

    static Vector properties;

    String name;
    int age;
    Date birthday;
    boolean married;

    public CpPersonLc(String newName, int newAge, Date newBirth, boolean newMarried)
    {
        super();
        name = newName;
        age = newAge;
        birthday = newBirth;
        married = newMarried;
        setFont(CpFonts.helveticaPlainTwelve());
    }

    public Object getProperty(CpProperty prop)
    {
        switch (prop.getId()) {
            case NAME_PROP : return name;
            case AGE_PROP : return new Integer(age);
            case BIRTHDAY_PROP : return birthday;
            case MARRIED_PROP : return new Boolean(married);
        }
        return super.getProperty(prop);
    }

    public void setProperty(CpProperty prop, Object o)
    {
        switch (prop.getId()) {
            case NAME_PROP : name = (String)o; break;
            case AGE_PROP : age = ((Integer)o).intValue(); break;
            case BIRTHDAY_PROP : birthday = (Date)o; break;
            case MARRIED_PROP : married = ((Boolean)o).booleanValue(); break;
            default : super.setProperty(prop, o); return;
        }
        repaint();
    }

    public Vector getProperties()
    {
        if (properties == null) {
            properties = super.getProperties();
            properties.addElement(new CpProperty("Name", CpBasicType.STRING, NAME_PROP));
            properties.addElement(new CpProperty("Age", CpBasicType.NUMBER, AGE_PROP));
            properties.addElement(new CpProperty("Birthday", CpBasicType.DATE, BIRTHDAY_PROP));
            properties.addElement(new CpProperty("Married", CpBasicType.BOOLEAN, MARRIED_PROP));
        }
        return properties;
    }

    public Dimension preferredSize()
    {
        return paintAndSize(null, null, false);
    }

    public void paint(Graphics g, Rectangle clip)
	{
	    paintAndSize(g, clip, true);
	}

    public Dimension paintAndSize(Graphics g, Rectangle clip, boolean paint)
    {
        Polygon p = null;
        if (paint) {
    	    g.setColor(getParent().getBackground());
	        g.fillRect(0, 0, size().width, size().height);

            p = new Polygon();
            p.addPoint(0, 20);
            p.addPoint(0, size().height - 20);
            p.addPoint(size().width / 2, size().height - 1);
            p.addPoint(size().width - 1, size().height - 20);
            p.addPoint(size().width - 1, 20);
            p.addPoint(size().width / 2, 0);
            p.addPoint(0, 20);

    	    g.setColor(getBackground());
    	    g.fillPolygon(p);
	       // g.fillRect(0, 0, size().width, size().height);
            g.setColor(getForeground());
         //   g.drawPolygon(p);
//            g.drawRect(0, 0, size().width - 1, size().height - 1);
        }

	    int curY = 22;
	    int width = 0;
	    for (int i = 0; i < getProperties().size(); i++) {
	        CpProperty prop = (CpProperty)getProperties().elementAt(i);
	        Object model = getProperty(prop);

	        Object convertedModel = model;
	        CpToStringConverter converter = prop.getType().getDefaultConverter();
	        if (converter != null) {
    	        convertedModel = converter.convert(model);
    	    }
    	    CpModelSelectableDisplayable displayer =  prop.getType().getDefaultDisplayer();
    	    displayer.setModel(convertedModel);
	        Dimension dim = displayer.preferredSize(this);
	        width = Math.max(width, dim.width);
	        if (paint) {
    	        displayer.paintIn(this, g, 2, curY, size().width - 4, dim.height);
    	    }
	        curY += dim.height;
	    }

        if (paint) {
    	    g.setColor(getParent().getBackground());

            Polygon p2 = new Polygon();
            p2.addPoint(0, size().height - 20);
            p2.addPoint(0, size().height);
            p2.addPoint(size().width / 2, size().height - 1);
            p2.addPoint(0, size().height - 20);

    	    g.fillPolygon(p2);

            p2 = new Polygon();
            p2.addPoint(size().width, size().height - 20);
            p2.addPoint(size().width, size().height);
            p2.addPoint(size().width / 2, size().height - 1);
            p2.addPoint(size().width, size().height - 20);

    	    g.fillPolygon(p2);



            g.setColor(getForeground());
            g.drawPolygon(p);
        }

	    return new Dimension(width + 4, curY + 22);
    }
}

