// T1.java, version 1.03, February 14, 1998.
// Copyright 1998 by Rick Wagner, all rights reserved.
// This file defines a test applet for the DataNode class.

import java.applet.*;
import java.awt.*;
import java.util.*;

public class T1 extends Applet
{
  // Test the name constructor
  private DataNode dnRoot = new DataNode("Root");

  public void init()
  {
    this.setBackground(Color.lightGray);
  }

  public void start()
  {
    int i = 0;

    // Test the default constructor:
    DataNode dnArray[] = new DataNode[2];
    dnArray[0] = new DataNode();
    dnArray[1] = new DataNode();
    
    // Build a DataNode tree for test:
    DataNode dnC1 = new DataNode("Child1");
    DataNode dnGC01 = new DataNode("Grandchild01");
    dnC1.addChild(dnGC01);
    DataNode dnGC02 = new DataNode("Grandchild02");
    dnC1.addChild(dnGC02);
    DataNode dnGC03 = new DataNode("Grandchild03");
    dnC1.addChild(dnGC03);
    dnRoot.addChild(dnC1);

    DataNode dnC2 = new DataNode("Child2");
    DataNode dnGC04 = new DataNode("Grandchild04");
    dnC2.addChild(dnGC04);
    DataNode dnGC05 = new DataNode("Grandchild05");
    dnC2.addChild(dnGC05);
    DataNode dnGC06 = new DataNode("Grandchild06");
    dnC2.addChild(dnGC06);
    dnRoot.addChild(dnC2);

    DataNode dnC3 = new DataNode("Child3");
    DataNode dnGC07 = new DataNode("Grandchild07");
    dnC3.addChild(dnGC07);
    DataNode dnGC08 = new DataNode("Grandchild08");
    dnC3.addChild(dnGC08);
    DataNode dnGC09 = new DataNode("Grandchild09");
    dnC3.addChild(dnGC09);
    dnRoot.addChild(dnC3);

    DataNode dnC4 = new DataNode("Child4");
    DataNode dnGC10 = new DataNode("Grandchild10");
    dnC4.addChild(dnGC10);
    DataNode dnGC11 = new DataNode("Grandchild11");
    dnC4.addChild(dnGC11);
    DataNode dnGC12 = new DataNode("Grandchild12");
    dnC4.addChild(dnGC12);
    dnRoot.addChild(dnC4);

    DataNode dnC5 = new DataNode("Child5");
    dnRoot.addChild(dnC5);
    DataNode dnC6 = new DataNode("Child6");
    dnRoot.addChild(dnC6);
    DataNode dnC7 = new DataNode("Child7");
    dnRoot.addChild(dnC7);

    // Test the findChild() method
    System.out.println('\n' + "Found " + dnRoot.findChild("GrandChild12").getName());

    // Test the copy constructor:
    DataNode dnRootCopy = new DataNode(dnRoot);
    System.out.println('\n' + "dnRootCopy name = " + dnRoot.getName());
    System.out.println('\n' + "Found " + dnRootCopy.findChild("GrandChild08").getName() + '\n');

    // Test the general vector constructor and all-children accessor:
    DataNode dnRootChildrenCopy = new DataNode(dnRoot.getChildVector());
    System.out.println('\n' + "Found " + dnRootChildrenCopy.findChild("GrandChild04").getName() + '\n');
    dnRootChildrenCopy.setName("RootChildrenCopy");
    for (i = 0; i < dnRootChildrenCopy.numChildren(); i++) dnRootChildrenCopy.removeChild(0);

    // Verify the need to use a copy of the vector in the all-children accessor:
    Vector vChildTest = dnRoot.getChildVector();
    for (i = 0; i < vChildTest.size(); i++) vChildTest.removeElementAt(0);
    System.out.println('\n' + "Found " + dnRoot.findChild("GrandChild01").getName());
  }

  public void paint(Graphics g)
  {
    Dimension d = new Dimension(this.size());
    g.setColor(Color.white);
    g.drawLine(0, 0, d.width - 1, 0);
    g.drawLine(0, 0, 0, d.height - 1);
    g.setColor(Color.black);
    g.drawLine(0, d.height - 1, d.width -1 , d.height - 1);
    g.drawLine(d.width - 1, 0, d.width - 1, d.height - 1);
    g.drawString("DataNode Test Applet", 20, 30);
    g.drawString("See the java console for output.", 20, 60);
  }

} // End class T1
