// TransLabel.java

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

/** A TransLabel labels a transition with a symbol from the NFA's input alphabet.
  * <a href="State.html">State</a>.
 *  @author <a href="mailto:bergmann@rowan.edu"> Seth Bergmann </a>
 *  @author <a href="mailto:gspyo@jersey.net"> Greg Safko </a>
 */
public class TransLabel extends JLabel
{

/** The Transition which this label is attached to.
 */
    public Transition trans; 	
    State toState, fromState;

    static int lblWidth=18;
    static int lblHeight=18;

/** Create a transition
 * @param t  The transition being labelled.
 * @param s The input symbol for this transition.
 */
public TransLabel (String s, Transition t)
{	super (s);
	trans = t;
	toState = trans.toState;
	fromState = trans.fromState;
	setHorizontalAlignment (JLabel.LEFT);
	setVerticalAlignment (JLabel.BOTTOM);
	setBounds (lblX(), lblY(),  lblWidth, lblHeight);
	setOpaque (false);			// transparent
	trans.toState.nfa.getContentPane().add (this);
}

/**  Establish the x,y coordinates of the upperleft corner of this label.
 */
public void setLocation ()
{  super.setLocation (lblX(), lblY());
}


public int lblX ()
{ double theta;
  if (Math.abs (toState.locX-fromState.locX) < 1) 	// Vertical transition
	return round (fromState.locX + State.r);
  else if (toState.locX > fromState.locX)			// Transition to the right?
    {	theta = Math.atan ((fromState.locY-toState.locY)/(toState.locX-fromState.locX));
  	return  round (fromState.locX + State.r + 3.5*State.r*Math.cos(theta));
    }
  else						// Transition to the left
    {	theta = Math.atan ((fromState.locY-toState.locY)/(fromState.locX-toState.locX));
	return round (fromState.locX - 1.5*State.r*Math.cos(theta));
    }
}

public int lblY ()
{  double theta;
   if (Math.abs (toState.locX-fromState.locX) < 1)	// Vertical transition
     if (toState.locY>fromState.locY) 
	return round (fromState.locY + 0.8*State.r);		// downward
     else return round (fromState.locY - 2.3*lblHeight);	// upward
   else if (toState.locX > fromState.locX)		// Transition to the right
     {	theta = Math.atan ((fromState.locY-toState.locY)/(toState.locX-fromState.locX));
	return round (fromState.locY - lblHeight - 3.5*State.r*Math.sin(theta));
     }
   else 					// Transition to the left
    {	theta = Math.atan ((fromState.locY-toState.locY)/(fromState.locX-toState.locX));
	return round (fromState.locY - lblHeight - 1.5*State.r*Math.sin(theta));
    }
}

// Round to nearest int.
static int round (double d)
{  return (new Double (d+0.5)).intValue();
}

}
