/*
 * Created on Jun 25, 2003
 *
 * To change this generated comment go to 
 * Window>Preferences>Java>Code Generation>Code Template
 */
package jdraw.framework;

import java.awt.*;
import java.awt.event.*;

public class StdMoveHandle implements Handle {
	private static final int NW = 0;
	private static final int SW = 1;
	private static final int SE = 2;
	private static final int NE = 3;

	private static final int WIDTH = 6;
	private static final int HEIGHT = 6;
	private final int corner;
	private Point startDrag = null;
	private Point lastDrag = null;
	private Figure owner;

	public StdMoveHandle(int corner, Figure owner) {
		this.corner = corner;
		this.owner = owner;
	}
		
	public Figure getOwner() {
		return owner;
	}

	public Rectangle getBounds() {
		Rectangle r = owner.getBounds();
		int cx, cy;
		switch (corner) {
		case NW: cx = r.x; cy = r.y; break;
		case SW: cx = r.x; cy = r.y + r.height; break;
		case SE: cx = r.x + r.width; cy = r.y + r.height; break;
		case NE: default: cx = r.x + r.width; cy = r.y; break; 
		}
		return new Rectangle(cx-WIDTH/2, cy-HEIGHT/2, WIDTH, HEIGHT);
	}

	/* (non-Javadoc)
	 * @see jdraw.framework.Handle#draw(java.awt.Graphics)
	 */
	public void draw(Graphics g) {
		Rectangle r = getBounds();
		Color tmp = g.getColor();
		g.setColor(Color.GRAY);
		g.fillRect(r.x, r.y, r.width, r.height);
		g.setColor(Color.BLACK);
		g.drawRect(r.x, r.y, r.width, r.height);
		g.setColor(tmp);
	}

	public Cursor getCursor() {
		return Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR);
	}

	public boolean contains(int x, int y) {
		return getBounds().contains(x, y);
	}

	public void startInteraction(int x, int y, MouseEvent e, DrawView v) {
		Rectangle r = getBounds();
		startDrag = new Point((int)r.getCenterX(), (int)r.getCenterY());
		lastDrag = startDrag;
		v.getEditor().showStatus("Move - dx = 0, dy = 0");
	}

	/* (non-Javadoc)
	 * @see jdraw.framework.Handle#dragInteraction(int, int, java.awt.event.MouseEvent, jdraw.framework.DrawView)
	 */
	public void dragInteraction(int x, int y, MouseEvent e, DrawView v) {
		int dx = x - lastDrag.x;
		int dy = y - lastDrag.y;
		lastDrag = e.getPoint();
		owner.move(dx, dy);
		v.getEditor().showStatus("Move - dx = " + (x-startDrag.x) + ", dy = " + (y-startDrag.y));
	}

	/* (non-Javadoc)
	 * @see jdraw.framework.Handle#stopInteraction(int, int, java.awt.event.MouseEvent, jdraw.framework.DrawView)
	 */
	public void stopInteraction(int x, int y, MouseEvent e, DrawView v) {
		v.getEditor().showStatus("");
	}

}


