package Nodepad;

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;

/**
 * MovablePointer.java
 *
 *
 * Created: Fri Jan  3 14:48:32 2003
 *
 * @author <a href="mailto:shapiro@cse.buffalo.edu">Stuart C. Shapiro</a>
 * @version
 */

public class MovablePointer extends Pointer
    implements MouseListener, MouseMotionListener {

    private boolean shouldmove = false;
    
    /**
     * Creates a new MovablePointer whose tail is at position (x,y).
     *
     * @param x the x position of the tail of this pointer.
     * @param y the y position of the tail of this pointer.
     */
    public MovablePointer (Pad p, int x, int y){
	super(p,x,y);
	pad.addMouseListener(this);
	pad.addMouseMotionListener(this);
    }

    public MovablePointer (Pad p, int tx, int ty, int hx, int hy){
	super(p, tx, ty, hx, hy);
	pad.addMouseListener(this);
	pad.addMouseMotionListener(this);
    }

    /**
     * Ignores mouse clicking.
     */
    public void mouseClicked (MouseEvent evt){}

    /**
     * Initializes cursor following
     * if the mouse is pressed in the head of this MovablePointer.
     */
    public void mousePressed (MouseEvent evt){
	if (new Rectangle(headx-width,
			  heady-width,
			  2*width,2*width)
	    .contains(evt.getX(), evt.getY())) {
	    Toolkit.getDefaultToolkit().beep();
	    shouldmove = true;
	}
    }

    public void mouseReleased (MouseEvent evt){
	shouldmove = false;
    }

    /**
     * Ignores mouse entering.
     */
    public void mouseEntered (MouseEvent evt){}

    /**
     * Ignores mouse exiting.
     */
    public void mouseExited (MouseEvent evt){}

    /**
     * Ignores mouse movement.
     */
    public void mouseMoved (MouseEvent evt){}

    /**
     * Moves the head of this node to the cursor
     * if the mouse had been pressed inside it.
     */
    public void mouseDragged (MouseEvent evt){
	if (shouldmove) {
	    Rectangle bounds = getBounds();
	    headx = evt.getX();
	    heady = evt.getY();
	    pad.repaint(bounds.union(getBounds()));
	}
    }

}// MovablePointer
