import java.awt.*;
import java.awt.geom.*;

/**
   A class that draws a UML symbol that symbolizes a "has-a" relationship
   @author Sugiharto Widjaja
   @version 05/01/02
*/

public class DiamondEdge extends AbstractEdge
{
   /**
      Draw the line with the diamond edge
      @param g2 the Graphics
   */
   public void doDraw(Graphics2D g2)
   {
      AbstractObjectNode node1 = (AbstractObjectNode) getStart();
      AbstractObjectNode node2 = (AbstractObjectNode) getEnd();
      Rectangle2D vrect = node1.getTitleRectangle();
      Rectangle2D trect = node2.getTitleRectangle();

      Point2D p = new Point2D.Double( vrect.getMinX() + vrect.getWidth() / 2,
                                      vrect.getMinY() + vrect.getHeight());
      theRombus = new Polygon();
      Point2D t1 = new Point2D.Double(p.getX() - POLY_SIDE, p.getY() + TRI_HEIGHT / 2);
      Point2D t2 = new Point2D.Double(p.getX() + POLY_SIDE, p.getY() + TRI_HEIGHT / 2);
      Point2D t3 = new Point2D.Double(p.getX(), p.getY() + TRI_HEIGHT);
      theRombus.addPoint((int) p.getX(), (int) p.getY());
      theRombus.addPoint((int) t1.getX(), (int) t1.getY());
      theRombus.addPoint((int) t3.getX(), (int) t3.getY());
      theRombus.addPoint((int) t2.getX(), (int) t2.getY());
      g2.draw(theRombus);
      Point2D p2 = new Point2D.Double( vrect.getMinX() + vrect.getWidth() / 2,
                                      vrect.getMinY() + vrect.getHeight() + TRI_HEIGHT);
      Point2D q2 = new Point2D.Double(trect.getCenterX(),
                                      trect.getCenterY() - trect.getHeight() / 2);
      line = new Line2D.Double(p2, q2);
      g2.draw(line);
   }

   /**
      Checks whether a point is contained in the line with the diamond edge
      @param aPoint a point to be checked
      @return true if it is contained, false otherwise
   */
   public boolean contains(Point2D aPoint)
   {
      final double MAX_DIST = 2;
      return line.ptSegDist(aPoint) < MAX_DIST || theRombus.contains(aPoint.getX(), aPoint.getY());
   }

   // The line that connects two classes
   private Line2D line;
   // The Diamond edge
   private Polygon theRombus;
   // The height of the diamond
   private final int TRI_HEIGHT = 10;
   // The length of each side of the diamond
   private final int POLY_SIDE = 12;
}
