import java.util.Vector;
import java.util.StringTokenizer;
import java.awt.*;

class displayNode {

    public static final int kLeaf = 1;
    public static final int kIndex = 2;
    public static final int kData = 3;
    public static final int kRoot = 4;

    private int nodeType;
    private int id = 0;
    private int numElems = 0;
    private Vector keys = new Vector();
    private Vector data = new Vector();
    public String rep;
    private int offsets[] = new int[1024];
    private boolean visible = false;

    public displayNode(String strRep) {
	rep = new String(strRep);
	
	StringTokenizer st = new StringTokenizer(strRep);
	int i = 0;
	while (st.hasMoreTokens()) {
	    String nextTok = st.nextToken();
	    if (i == 0) {
		if (nextTok.equals("l")) nodeType = kLeaf;
		else if (nextTok.equals("r")) nodeType = kRoot;
		else nodeType = kIndex;
	    } else if (i == 1) {
		Integer I = new Integer(nextTok);
		id = I.intValue();
	    }
	    else {
		keys.addElement(nextTok);
		data.addElement(st.nextToken());
	    }
	    i++;
	}
	numElems = i-2;
    }

    public void drawSelf(int x, int y, Graphics g) {
	FontMetrics fm = g.getFontMetrics();
	if (nodeType == kLeaf) g.setColor(new Color(0, 0, 255));
	else g.setColor(new Color(255,0,0));

	int curOffset = 0;
	int height = fm.getHeight() + 2;

	if (nodeType != kLeaf) {
	    for (int i = 0; i < numElems; i++) {
		int trunc = java.lang.Math.min(5, ((String)keys.elementAt(i)).length());
		String thisKeyDataPairString = ((String)keys.elementAt(i)).substring(0,trunc) +  ":" + (String)data.elementAt(i);
		g.drawString(thisKeyDataPairString, x + curOffset, y - 2);
		int w = fm.stringWidth(thisKeyDataPairString) + 2;
		curOffset += w;
		g.drawLine(x + curOffset, y - height, x + curOffset, y);
		offsets[i] = curOffset;
	    }    
	    g.drawRect(x - 1, y - height, curOffset + 2, height + 1);
	}
	else {
	    int maxWidth = 0;
	    for (int i = 0; i < numElems; i++) {
		String thisKeyDataPairString = (String)keys.elementAt(i) +
		    ":" + (String)data.elementAt(i);
		g.drawString(thisKeyDataPairString, x, y + curOffset);
		int w = fm.stringWidth(thisKeyDataPairString) + 2;
		if (w > maxWidth) maxWidth = w;
		//g.drawLine(x, y + curOffset,x + w, y + curOffset);
		curOffset += height;		
		offsets[i] = maxWidth;
	    }
	  g.drawRect(x, y - height, maxWidth, curOffset);
	}
    }

    public int nodeType() { return nodeType; }
    public int nodeID() { return id; }
    public String keyAt(int i) { return (String)keys.elementAt(i);}
    public String dataAt(int i) { return (String)data.elementAt(i);}
    public int size() { return numElems; }

    public int totalWidth() { if (numElems == 0) return 10; else return offsets[numElems - 1]; }
    public int width(int i) { return offsets[i]; }
    public void setVisible(boolean vis) { visible = vis;}
    public boolean isVisible() { return visible; }
}
    











