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

class btreeDebug extends Frame implements ActionListener {

    private boolean done = false;
    public static final String kShowNodeText = "Show Node...";
    public static final String kHideNodeText = "Hide Node...";
    public static final String kBShowNodeText = "Show Node";
    public static final String kBHideNodeText = "Hide Node";
    public static final String kQuitText = "Quit";
    public static final String kBNextTreeText = "Next Tree";
    public static final String kBNextTreeTenText = "Next x 10";

    Dialog nodeQuery;
    TextField tf, tf1;
    treeDisplayPane t;
    //MAIN
    public static void main(String argv[]) { 
	if (argv.length < 1) {
	    System.err.println("btreeDebug debugFile");
	    System.exit(0);
	}

	new btreeDebug(argv[0]); 
	
    }

    //constructor
    public btreeDebug(String inFile) {
	super("BTree Debug");

	this.setLayout(new BorderLayout());
	//setup the content
	ScrollPane sPane = new ScrollPane();
	this.add(sPane,"Center");
	t = new treeDisplayPane(this, 1024, 400, inFile);
	sPane.add(t);


	//alternate interface
	Button addButton = new Button(kBShowNodeText);
	Button hideButton = new Button(kBHideNodeText);
	Button nextTree = new Button(kBNextTreeText);
	Button next10Tree = new Button(kBNextTreeTenText);
	addButton.addActionListener(this);
	hideButton.addActionListener(this);
	nextTree.addActionListener(this);
	next10Tree.addActionListener(this);
	tf1 = new TextField(6);
	Panel p = new Panel();
	p.setLayout(new FlowLayout());
	p.add(tf1);
	p.add(addButton);
	p.add(hideButton);
	p.add(nextTree);
	p.add(next10Tree);

	this.add(p, "South");

	//setup the menubar
	MenuBar menubar = new MenuBar();
	this.setMenuBar(menubar);
	
	//setup menus
	Menu tree = new Menu("Tree");
	menubar.add(tree);	

	//add menu items
	MenuItem showNode, hideNode, quit;
	//setup the items
	showNode = new MenuItem(kShowNodeText);
	hideNode = new MenuItem(kHideNodeText);
	quit = new MenuItem(kQuitText);

	//add items to add menu		
	tree.add(showNode);
	tree.add(hideNode);
	tree.add(quit);

	nodeQuery = new Dialog(this, "Which node?", true);
	tf = new TextField(4);
	nodeQuery.add(tf, "East");
	Button b = new Button("ok");
	b.addActionListener(this);
	nodeQuery.add(b, "South");
	nodeQuery.pack();

	//setup listeners for menu items
	showNode.addActionListener(this);
	hideNode.addActionListener(this);
	quit.addActionListener(this);

	this.pack();
	this.show();

    }

    public void actionPerformed(ActionEvent e) {
	String cmd = e.getActionCommand();
	if (cmd.equals(kShowNodeText)) {
	    nodeQuery.show();
	    String nodeText = tf.getText();
	    Integer I = new Integer(nodeText);
	    int nodeID = I.intValue();
	    t.setNodeVisibility(nodeID, true);
	    t.repaint();
	}
	else if (cmd.equals(kHideNodeText)) {
	    nodeQuery.show();
	    String nodeText = tf.getText();
	    Integer I = new Integer(nodeText);
	    int nodeID = I.intValue();
	    t.setNodeVisibility(nodeID, false);
	    t.repaint();
	}
	else if (cmd.equals(kBShowNodeText)) {
	    String nodeText = tf1.getText();
	    Integer I = new Integer(nodeText);
	    int nodeID = I.intValue();
	    t.setNodeVisibility(nodeID, true);
	    t.repaint();
	}
	else if (cmd.equals(kBHideNodeText)) {
	    String nodeText = tf1.getText();
	    Integer I = new Integer(nodeText);
	    int nodeID = I.intValue();
	    t.setNodeVisibility(nodeID, false);
	    t.repaint();
	}
	else if (cmd.equals(kBNextTreeText)) {
	    t.nextTree();
	    String nodeText = tf1.getText();
	    Integer I = new Integer(nodeText);
	    int nodeID = I.intValue();
	    t.setNodeVisibility(nodeID, true);
	    t.repaint();
	}
	else if (cmd.equals(kBNextTreeTenText)) {
	    for (int i = 0; i < 10; i++) t.nextTree();
	    String nodeText = tf1.getText();
	    Integer I = new Integer(nodeText);
	    int nodeID = I.intValue();
	    t.setNodeVisibility(nodeID, true);
	    t.repaint();
	}
	else if (cmd.equals("ok")) {
	    nodeQuery.hide();
	}
	else if (cmd.equals(kQuitText)) {
	    System.exit(0);
	}
    }
}
