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.*;

public class CpOmtLc extends CpAbstractDiagrammableLc
{
    static CpGeneralStringDisplayer displayer = new CpGeneralStringDisplayer();
    static CpGeneralStringDisplayer centeredDisplayer = new CpGeneralStringDisplayer();

    static {
        centeredDisplayer.setHorizontalAlignment(CpAlignable.ALIGN_CENTER);
    }

    String name;
    Vector variables, methods;
    boolean isInstance;  // Ok, I know this should just be a subclass...

    Vector handles; // Lets try caching these

    public CpOmtLc(String newName, Vector newVariables, Vector newMethods, boolean newIsInstance)
    {
        super();
        name = newName;
        variables = newVariables;
        methods = newMethods;
        isInstance = newIsInstance;
        setFont(CpFonts.helveticaPlainTen());
    }

    public String getName()
    {
        return name;
    }

	public Vector getHandles()
	{
	    if (handles == null) {
	        handles = new Vector();
    		handles.addElement(new CpSizingHandle(this, CpBasicLocator.rightBottomLocator, CpBasicLocator.leftTopLocator, getWantsXorSizing()));
    		handles.addElement(new CpSizingHandle(this, CpBasicLocator.leftTopLocator, CpBasicLocator.rightBottomLocator, getWantsXorSizing()));
    		handles.addElement(new CpSizingHandle(this, CpBasicLocator.leftBottomLocator, CpBasicLocator.rightTopLocator, getWantsXorSizing()));
    		handles.addElement(new CpSizingHandle(this, CpBasicLocator.rightTopLocator, CpBasicLocator.leftBottomLocator, getWantsXorSizing()));
    	    CpDependentLineLc connectionLine = new CpDependentLineLc();
    	    connectionLine.startDecoration = connectionLine.DIAMOND;
    	    connectionLine.endDecoration = connectionLine.ARROW;
    	    CpHandle handle;
    	    CpDependentConnector connector;
     		handles.addElement(handle = new CpConnectingHandle(this, new CpOffsetLocator(CpBasicLocator.rightMiddleLocator, new Point(3, -10)), connector = new CpDependentConnector(connectionLine), new CpConnectionPolicy()));
     		handle.decoration = handle.DIAMOND;
    	    connectionLine = new CpDependentLineLc();
    	    connectionLine.startDecoration = connectionLine.NONE;
    	    connectionLine.endDecoration = connectionLine.DOT;
     		handles.addElement(handle = new CpConnectingHandle(this, new CpOffsetLocator(CpBasicLocator.rightMiddleLocator, new Point(3, 10)), connector = new CpDependentConnector(connectionLine), new CpConnectionPolicy()));
     		handle.decoration = handle.DOT;
     	}
		return handles;
	}

	public Dimension getConnectionOverlap()
	{
	    if (isInstance) {
	        return new Dimension(0, getHexagonOffset() + 10);
	    }
	    return new Dimension(20, 20);
	}

    public int getHexagonOffset()
    {
        return (int)(getScale() * 20);
    }

	public boolean getConnectionLinesFromCenterX()
	{
	    return isInstance;
	}

    public Dimension preferredSize()
    {
        int margin;
        if (isInstance) {
            margin = getHexagonOffset() * 2 + 5;
        } else {
            margin = 10;
        }
        int fontHeight = Toolkit.getDefaultToolkit().getFontMetrics(getFont()).getHeight();
        return new Dimension(100, margin + 10 + fontHeight * (variables.size() + methods.size() + 1));
    }

    public void paint(Graphics g, Rectangle clip)
	{

        int curY = 5;
        Polygon p = null;
	    if (isInstance) {
	        int hexOffset = getHexagonOffset();
           // g.setColor(getParent().getBackground());
          //  g.fillRect(0, 0, size().width, size().height);
            p = new Polygon();
            p.addPoint(0, hexOffset);
            p.addPoint(0, size().height - hexOffset);
            p.addPoint(size().width / 2, size().height - 1);
            p.addPoint(size().width - 1, size().height - hexOffset);
            p.addPoint(size().width - 1, hexOffset);
            p.addPoint(size().width / 2, 0);
            p.addPoint(0, hexOffset);

    	    g.setColor(getBackground());
    	    g.fillPolygon(p);
    	    curY = 2 + hexOffset;
        } else {
            g.setColor(getBackground());
            g.fillRect(0, 0, size().width, size().height);
        }

        g.setColor(getForeground());

        int fontHeight = g.getFontMetrics().getHeight();

        centeredDisplayer.setModel(name);
        centeredDisplayer.paintIn(this, g, 2, curY, size().width - 4, fontHeight);
        curY += fontHeight + 2;

        g.drawLine(0, curY, size().width, curY);
	    for (int i = 0; i < variables.size(); i++) {
	        displayer.setModel(variables.elementAt(i));
    	    displayer.paintIn(this, g, 2, curY, size().width - 4, fontHeight);
            curY += fontHeight;
        }
        if (!methods.isEmpty()) {
            curY += 2;

            g.drawLine(0, curY, size().width, curY);
    	    for (int i = 0; i < methods.size(); i++) {
    	        displayer.setModel(methods.elementAt(i));
        	    displayer.paintIn(this, g, 2, curY, size().width - 4, fontHeight);
                curY += fontHeight;
            }
        }

        if (isInstance) {
    	  /*  g.setColor(getParent().getBackground());

            Polygon p2 = new Polygon();
            p2.addPoint(0, size().height - hexOffset);
            p2.addPoint(0, size().height);
            p2.addPoint(size().width / 2, size().height - 1);
            p2.addPoint(0, size().height - hexOffset);

    	    g.fillPolygon(p2);

            p2 = new Polygon();
            p2.addPoint(size().width, size().height - hexOffset);
            p2.addPoint(size().width, size().height);
            p2.addPoint(size().width / 2, size().height - 1);
            p2.addPoint(size().width, size().height - hexOffset);

    	    g.fillPolygon(p2);
    	    */
            g.setColor(getForeground());
            g.drawPolygon(p);
        } else {
            g.drawRect(0, 0, size().width - 1, size().height - 1);
        }

    }

    public boolean isTransparent()
    {
        return true;
    }
}

