package cnp.ew.diagram;

import java.awt.*;

import cnp.ew.lightweight.*;

public class CpSelectionHandle extends CpHandle
{
    Point anchorPoint, dragPoint;
    CpDiagram diagram;

    public CpSelectionHandle(CpDiagram newDiagram)
    {
        this(newDiagram, null);
    }

    public CpSelectionHandle(CpDiagram newDiagram, Point newAnchorPoint)
    {
        super();
        diagram = newDiagram;
        anchorPoint = newAnchorPoint;
    }

    public void startDrag(int x, int y)
	{
  	    dragPoint = new Point(x, y);
	    if (anchorPoint == null) {
      	    anchorPoint = dragPoint;
      	}
   	    diagram.drawXORRect(CpGraphicsUtilities.RectangleFromPoints(anchorPoint, dragPoint));
    }

	public void drag(int x, int y)
	{
	    Rectangle vis = diagram.getVisibleRect();
	    x = Math.max(vis.x, x);
	    x = Math.min(vis.x + vis.width, x);
	    y = Math.max(vis.y, y);
	    y = Math.min(vis.y + vis.height, y);
   	    diagram.drawXORRect(CpGraphicsUtilities.RectangleFromPoints(anchorPoint, dragPoint));
   	    dragPoint = new Point(x, y);
   	    diagram.drawXORRect(CpGraphicsUtilities.RectangleFromPoints(anchorPoint, dragPoint));
	}

	public void endDrag(int x, int y)
	{
   	    diagram.drawXORRect(CpGraphicsUtilities.RectangleFromPoints(anchorPoint, dragPoint));
    }

    public Rectangle getRectangle()
    {
        return CpGraphicsUtilities.RectangleFromPoints(anchorPoint, dragPoint);
    }
}