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

public class ComradesLabel extends JButton // for ComradesNode.java ?
{
    ComradesNode N;
    MoveTree MT;
    boolean END;

    public MouseAdapter ComradesLabelMouseAdapter () // adapt
    {
	MouseAdapter MA = new MouseAdapter ()
	    {
		public void mouseClicked (MouseEvent mou_evt)
		{
		    if (END)
			N.is_expanded = false; // un do // focus ?
		    else if (mou_evt.getClickCount () == 2)
			MT.JumpToNode (N);
		    else
			{
			    MT.FOCUS = true;
			    if (N.MainLineParent != null)
				N.MainLineParent.is_expanded = true;
			}
		    MT.PaintPanel ();
		}
	    };
	    return MA;    
    }

    public KeyAdapter ComradesLabelKeyAdapter ()
    {
	KeyAdapter KA = new KeyAdapter ()
	    {
		public void keyReleased (KeyEvent key_evt)
		{
		    if (MT.NOW != N)
			return; // HACK
		    if (key_evt.getKeyCode () == key_evt.VK_RIGHT)
			MT.BP.CF.AbruptForward ();
		    if (key_evt.getKeyCode () == key_evt.VK_LEFT)
			MT.BP.CF.AbruptBack ();
		    if (key_evt.getKeyCode () == key_evt.VK_UP)
			{
			    ComradesNode T = N;
			    while (!T.is_expanded && T.getParent () != null)
				T = T.MainLineParent;
			    T.is_expanded = false;
			    MT.PaintPanel ();
			}
		    if (key_evt.getKeyCode () == key_evt.VK_DOWN)
			{
			    if (!N.is_expanded)
				{
				    N.is_expanded = true;
				    MT.PaintPanel ();
				}
			}
		}
	    };
	    return KA;
    }

    public ComradesLabel (TreeNode n, MoveTree mt, boolean end, int ht)
    {
	super ();
	MT = mt;
	END = end;
	if (END)
	    setText ("X");
	else if (ht == 0)
	    setText (n.toString ());
	else if ((ht % 2) == 0)
	    setText ("" + (ht / 2) + ". " + n.toString ());
	else if ((ht % 2) == 1)
	    setText ("" + (ht / 2) + "... " + n.toString ());
	if (n.getParent () == null && !END)
	    setText ("R");
	setBorder (BorderFactory.createEmptyBorder ()); // save
	//	setMargin (new Insets (1, 1, 1, 1)); // save
	if (!END)
	    setFont (new Font ("Serif", 0, 12)); // save
	else
	    setFont (new Font ("Monospaced", Font.BOLD, 12)); // save
	setBackground (Color.white);
	if (END)
	    setForeground (Color.black);
	else if (n.getParent () == null)
	    setForeground (Color.blue);
	else
	    setForeground ((n.getParent ().getChildCount () > 1) // transpose ?
			   ? Color.red : Color.black);
	N = (ComradesNode) n; // involve
	addMouseListener (ComradesLabelMouseAdapter ()); // in direct
	addKeyListener (ComradesLabelKeyAdapter ());
	setFocusable (true); // value ?
	if (!END)
	    N.LABEL = this;
    }
}
