package IR2Vis;

import IR2.*;
import java.util.*;
import java.awt.*;

class IRVisTreeNode extends Label
{
    Object the_node;        // the actual tree node
    boolean expanded;       // true if the node is current expanded
    boolean added;          // true if added to some drawing board
    boolean dirty;          // for recalc children positions
    IRVisTreeNode[] children;   // list of children

    public IRVisTreeNode(Object n)
        // public constructor
    {
        super("",Label.CENTER);
        the_node = n;
        expanded = false;
        added = false;
        dirty = true;
        if (n instanceof Walkable)
            setText(((Walkable)the_node).node_name());
        else
            setText(n.toString());

        if (n instanceof Walkable) {
		  Vector v = new Vector();
		  for (Enumeration e = ((Walkable)the_node).neighbors();
			   e.hasMoreElements(); ) {
			v.addElement(e.nextElement());
		  }
		  children = new IRVisTreeNode[v.size()];
		  v.copyInto(children);
        }
    }

    final String getTitle()
        // return name of the node
    {
        if (the_node instanceof Walkable)
            return ((Walkable)the_node).node_name();
        else
            return the_node.toString();
        
    }

    String Description()
        // return description information, should be overriden
    {
        String s = "Class: "+the_node.getClass().getName();
        s += "\nDescription: \n";
        if (the_node instanceof Walkable)
            s += ((Walkable)the_node).pretty_print(0,false);
        else
            s += the_node.toString();

        /*if (the_node instanceof IRObject) { // really want to see if it's Annotatable
            Enumeration tags = ((IRObject)the_node).listTags();
            Enumeration elts = ((IRObject)the_node).listAnnotations();
            if (tags.hasMoreElements())
                s += "\nAnnotations:\n";
            while (tags.hasMoreElements()) {
                s += "tag: "+tags.nextElement();
                s += "\n data: "+elts.nextElement().toString();
                s += "\n";
            }
		}*/
        
        return s;
    }

    int NumberOfChildren()
        // return the number of leaves of this IR tree node
    {
        if (the_node instanceof Walkable)
            return children.length;
        else return 0;
    }

    IRVisTreeNode ChildNode(int cn)
        // return a particular child node
    {
        if (!(the_node instanceof Walkable)) return null;
        return children[cn];
    }
} 
