import javax.servlet.*;
import java.util.Vector;

import HTML.*;

/*
    This class serves a container for the Navigation Table.
    It gets a reference to the DOMAIN_LIST in the
    ServletContext and iterates over it listing the Categories.
    It is derived from HTMLTable.
*/

public class NavigationTable extends HTMLTable {

  // Method Name: NavigationTable()
  // Purpose: This is the only constructor defined for the
  // NavigationTable.  All of this class'es functionality
  // is defined in this method.
  public NavigationTable(ServletContext context) {

    // Define a fixed table width
    setWidthByPixel(150);
    //setWidth(15);
    
    setHorizontalAlign(LEFT);
    setVerticalAlign(TOP);
    HTMLText caption = new HTMLText("Categories");
    caption.setBold(true);
    setCaption(caption);

    // Get a reference to the Vector that contains the Category
    // objects.
    Vector categories =
      (Vector)context.getAttribute("DOMAIN_LIST");

    // Iterate over this Vector creating links to
    // the TitleListServlet including a category
    // id to perform a lookup on.
    for (int x = 0; x < categories.size(); x++ ) {

      Category category = (Category)categories.elementAt(x);

      HTMLTableRow row = new HTMLTableRow();
      HTMLTableCell cell
        = new HTMLTableCell(HTMLTableCell.DATA);

      // Create the link and add it to the cell
      cell.addObject(
        new HTMLLink("/servlet/TitleListServlet?category_id=" +
          category.getId(),
        new HTMLText(category.getName())));

      // add the cell to the row
      row.addObject(cell);
      // add the row to the table
      addObject(row);
    }

    // Create Other Navigation Items
    HTMLTableRow row;
    HTMLTableCell cell;

    // Create a simple separator
    row = new HTMLTableRow();
    cell = new HTMLTableCell(HTMLTableCell.DATA);
    cell.addObject(new HTMLHorizontalRule());
    row.addObject(cell);
    addObject(row);

    // Link to the WelcomeServlet
    row = new HTMLTableRow();
    cell = new HTMLTableCell(HTMLTableCell.DATA);
    cell.addObject(new HTMLLink("/servlet/WelcomeServlet",
      new HTMLText("Home")));
    row.addObject(cell);
    addObject(row);

    // Link to the ShoppingBasketServlet
    row = new HTMLTableRow();
    cell = new HTMLTableCell(HTMLTableCell.DATA);
    cell.addObject(new HTMLLink("/servlet/ShoppingBasketServlet",
      new HTMLText("Checkout")));
    row.addObject(cell);
    addObject(row);
  }
}
