package Nodepad;

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

/**
 * Node.java
 *
 *
 * Created: Mon Dec 30 13:56:15 2002
 *
 * @author <a href="mailto:shapiro@cse.buffalo.edu">Stuart C. Shapiro</a>
 * @version
 *
 * A bordered, integer-valued node that can be moved around a sketch Pad.
 */

public class PointerNode extends ParentNode {

    private Pointer pointer;
    private Point pointerLoc;

    /**
     * Creates a bordered Node, with a given Pad, location, and value.
     *
     * @param p the Pad this node is on.
     * @param x the x value of this node's initial location.
     * @param y the y value of this node's initial location.
     * @param v the integer value of this node.
     */
    public PointerNode (Pad p, int x, int y, String name){
	super(p, x, y, 0);
	label = name;
	pointer = new Pointer(pad,
			      getX()+getWidth()/2,
			      getY()-Pointer.halfwidth);
	pointerLoc = pointer.getLocation();
	pad.add(pointer);
	this.setBorder(BorderFactory.createLineBorder(Color.black,1));
    }

    /**
     * Paints this node as a rectangle with its value centered inside.
     *
     * @param g the Graphics component of this node.
     */
    public void paintComponent(Graphics g) {
	setSize(size);
	setLocation(locpt);
	g.setColor(Color.black);
	//Draw the label, centered, twice the size of the standard font.
	{Font currFont = g.getFont();
	g.setFont(currFont.deriveFont(2*currFont.getSize2D()));}
	g.drawString(label,
		     (size.width - g.getFontMetrics().stringWidth(label))/2,
		     30);
    }

    /**
     * Initializes cursor following
     * if the mouse is pressed inside this node.
     */
    public void mousePressed (MouseEvent evt){
	if (this.getBounds().contains(evt.getX(), evt.getY())) {
	    shouldmove = true;
	    dx = getLocation().x - evt.getX();
	    dy = getLocation().y - evt.getY();
	}
    }

    /**
     * Terminates cursor following.
     */
    public void mouseReleased (MouseEvent evt){
	shouldmove = false;
    }

    /**
     * Moves this node to a new location.
     * 
     * @param x the new x location.
     * @param y the new y location.
     */
    protected void moveTo(int x, int y){
	super.moveTo(x,y);
	pointer.moveTo(x+getSize().width/2, y);
    }
    
}// PointerNode
