/*****************************************************************************
 ObjectListTest

 An application to test the ObjectList class.

 bruce.miller@nist.gov
 Contribution of the National Institute of Standards and Technology,
 not subject to copyright.
******************************************************************************/

package test;
import java.applet.*;
import java.awt.*;
import java.util.*;
import gui.*;

public class ObjectListTest extends Applet {
  NodeList list;
  TextField text;
  Button bopen,bclose,benable,bdisable,bhide;
  Button bnewroot,bremroot,bopennode,bclosenode,
         benablenode,bdisablenode,bhidenode,bshownode;
  Hashtable nodes = new Hashtable();
  int nums[] = {10,5,5,5};
  
  public void init() {
    setLayout(new BorderLayout());
    Label label = new Label("ObjectList Demo & Tester");
    label.setFont(new Font("Helvetica",Font.BOLD,14));
    add("North",label);
    list = new NodeList(40,10);
    list.setBackground(Color.white);
    list.setForeground(Color.black);
    Object roots[] = Node.makeNodes("node",null,nums,0,nodes);
    list.addItems(roots);
    add("Center",list);
    Panel p = new Panel(),p1 = new Panel(), p2 = new Panel();
    p.setLayout(new BorderLayout());
    p1.add(bopen = new Button("Open"));
    p1.add(bclose = new Button("Close"));
    p1.add(benable = new Button("Enable"));
    p1.add(bdisable = new Button("Disable"));
    p1.add(bhide = new Button("Hide"));
    p2.add(text = new TextField(10));
    p2.add(bnewroot = new Button("New"));
    p2.add(bremroot = new Button("Remove"));
    p2.add(bopennode = new Button("Open"));
    p2.add(bclosenode = new Button("Close"));
    p2.add(benablenode = new Button("Enable"));
    p2.add(bdisablenode = new Button("Disable"));
    p2.add(bhidenode = new Button("Hide"));
    p2.add(bshownode = new Button("Show"));
    p.add("North",p1);
    p.add("South",p2);
    add("South",p);
    enableButtons();
  }
  
  Object findNode() {
    return nodes.get(text.getText()); }

  void enableButtons() {
    Object item = list.getSelectedItem();
    bopen.enable((item != null) && list.isOpenable(item));
    bclose.enable((item != null) && list.isOpen(item));
    benable.enable((item != null) && !list.isEnabled(item));
    bdisable.enable((item != null) && list.isEnabled(item));
    bhide.enable((item != null) && list.isVisible(item));

    boolean txt = text.getText().length() > 0;
    item = findNode();
    bremroot.enable((item != null));
    bnewroot.enable(txt);
    bopennode.enable((item != null) && list.isOpenable(item));
    bclosenode.enable((item != null) && list.isOpen(item));
    benablenode.enable((item != null) && !list.isEnabled(item));
    bdisablenode.enable((item != null) && list.isEnabled(item));
    bhidenode.enable((item != null) && list.isVisible(item));
    bshownode.enable((item != null) && !list.isVisible(item));
  }

  public boolean handleEvent(Event e) {
    enableButtons();
    if (e.id == Event.ACTION_EVENT) {
      Object item = list.getSelectedItem();
      if (e.target == bopen)       list.open(item);
      else if (e.target == bclose) list.close(item);
      else if (e.target == benable) list.enable(item);
      else if (e.target == bdisable) list.disable(item);
      else if (e.target == bhide) list.hide(item);
      else if (e.target == bnewroot) 
	list.addItem(new Node(text.getText(),null,nums,0,nodes));
      else if (e.target == bremroot)   list.delItem(findNode()); 
      else if (e.target == bopennode)  list.open(findNode());
      else if (e.target == bclosenode) list.close(findNode());
      else if (e.target == benablenode) list.enable(findNode());
      else if (e.target == bdisablenode) list.disable(findNode());
      else if (e.target == bhidenode) list.hide(findNode());
      else if (e.target == bshownode) list.show(findNode());
      else if (e.target == list) {
	if (list.isOpenable(e.arg)) list.open(e.arg);
	else if (list.isOpen(e.arg)) list.close(e.arg); }
      else return false; }
    else if (e.id == Event.LIST_SELECT) {}
    else if (e.id == Event.LIST_DESELECT) {}
    else if (e.id == ObjectList.MOUSE_EXIT_ITEM) {
      showStatus("");
      list.highlightRect(list.itemBounds(e.arg)); }
    else if (e.id == ObjectList.MOUSE_ENTER_ITEM) {
      showStatus("Select " + ((Node) e.arg).name);
      list.highlightRect(list.itemBounds(e.arg)); }
    else return false;
    enableButtons();
    return true; }
}

class Node {
  String name;
  Node papa;
  Node[] kids;

  Node(String name, Node parent, int n[], int depth, Hashtable table) {
    this.name = name;
    this.papa = parent;
    table.put(name,this);
    if (depth < n.length)
      this.kids = makeNodes(name,this,n,depth,table);  }

  static Node[] makeNodes(String name, Node parent, int n[], int depth, Hashtable table) {
    Node[] nodes = new Node[n[depth]];
    for (int i=0; i<n[depth]; i++)
      nodes[i] = new Node(name + "." + i, parent, n, depth+1,table);
    return nodes; }
}


class NodeList extends ObjectList {
  NodeList(int width, int height) {
    super(width,height); }

  public Object getParent(Object node) {
    return ((Node) node).papa; }

  public Object[] getChildren(Object node) { 
    return ((Node) node).kids; }

  public String getString(Object node) {
    return  ((Node) node).name; }

  public void open(Object node) {
    super.open(node);
    makeVisible(node); 
    select(node); }
}