// The UMLet source code is distributed under the terms of the GPL; see license.txt
package com.umlet.element.custom; 

import java.awt.*;
import java.util.*;
import com.umlet.control.*;
import com.umlet.element.base.Entity;

/** 
* <p>Title: </p> 
* <p>Description: </p> 
* <p>Copyright: Copyright (c) 2001</p> 
* <p>Company: </p> 
* @author unascribed 
* @version 1.0 
*/ 

public class EER_Entity extends Entity {
	int ySave = 0;
	boolean hasAttributes = false;
	
	private Vector getStringVector() {
	    Vector ret=Constants.decomposeStrings(this.getPanelAttributes(), "\n");
	    return ret;
	}
	
	public void paint(Graphics g) {
		Graphics2D g2=(Graphics2D) g;
	    g2.setFont(Constants.getFont());
	    Composite[] composites = colorize(g2); //enable colors
	    g2.setColor(_activeColor);

	    Vector tmp=getStringVector();
	    boolean CENTER=true;
	    boolean drawInnerRect = false;
	    int yPos=Constants.getDistLineToText()*2;

	    //### draw rectangles and lines (some duplicated code)
	    Polygon poly = new Polygon();
	    for (int i=0; i<tmp.size(); i++) {
	      String s=(String)tmp.elementAt(i);
	      if (s.equals("--")) {
	        CENTER=false;
	        ySave = yPos+Constants.getDistLineToText()*2;
	        yPos+=Constants.getDistLineToText()*3;
	      } else {
	        yPos+=Constants.getFontsize();
	        if (CENTER && s.startsWith("##")) drawInnerRect = true;
	        yPos+=Constants.getDistTextToText();
	        if(CENTER) ySave = yPos;
	      }
	    }
    	poly.addPoint(0,0);
    	poly.addPoint(this.getWidth()-1,0);
	    if(CENTER) {
	    	hasAttributes = false; //see getStickingBorder()
	    	ySave = this.getHeight();
	    	poly.addPoint(this.getWidth()-1,this.getHeight()-1);
	    	poly.addPoint(0,this.getHeight()-1);
	    } else {
	    	hasAttributes = true; //see getStickingBorder()
	    	g.drawLine(10,ySave,10,yPos+Constants.getDistTextToText()-Constants.getDistLineToText());
			poly.addPoint(this.getWidth()-1,ySave);
			poly.addPoint(0,ySave);
	    }
    
	    g2.setComposite(composites[1]);
	    g2.setColor(_fillColor);
	    g2.fillPolygon(poly);
	    g2.setComposite(composites[0]);
	    if(_selected) g2.setColor(_activeColor); else g2.setColor(_deselectedColor);
	    g2.drawPolygon(poly);
	    
	    if(drawInnerRect) {
	    	if(CENTER) g.drawRect(3,3,this.getWidth()-7,this.getHeight()-7);
	    	else g.drawRect(3,3,this.getWidth()-7,ySave -6);
	    }

		    
	    //#### draw text
    	CENTER=true;
	    yPos=Constants.getDistLineToText()*2;

	    for (int i=0; i<tmp.size(); i++) {
	      String s=(String)tmp.elementAt(i);
	      if (s.equals("--")) {
	        CENTER=false;
	        ySave = yPos+Constants.getDistLineToText()*2;
	        yPos+=Constants.getDistLineToText()*3;
	      } else {
	        yPos+=Constants.getFontsize();
	        if (CENTER) {
	        	String s1 = s;
	        	if(s.startsWith("##")) {
	    	      	drawInnerRect = true;
	    	      	s1 = s1.substring(2);
	        	}
	        	Constants.write(g2,s1,(int)this.getWidth()/2, yPos, true);
	        } else {
	        	Constants.write(g2,s,Constants.getFontsize(), yPos, false);
	        }
	        yPos+=Constants.getDistTextToText();
	        if(CENTER) ySave = yPos;
	      }
	    }
	}
	
	public StickingPolygon getStickingBorder() { //LME: define the polygon on which relations stick on
		StickingPolygon p = new StickingPolygon();
		p.addLine(new Point(0,0), new Point(this.getWidth()-1,0),Constants.RESIZE_TOP);
		p.addLine(new Point(this.getWidth()-1,0), new Point(this.getWidth()-1,ySave-1),Constants.RESIZE_RIGHT);
		if(!hasAttributes) p.addLine(new Point(this.getWidth()-1,ySave-1), new Point(0,ySave-1),Constants.RESIZE_BOTTOM);
		p.addLine(new Point(0,ySave-1), new Point(0,0),Constants.RESIZE_LEFT);
		return p;
	}
} 

