import THtmlFont;

/*-------------------------------------------------- 
* CLASS: THtmlTableCell
* DESCRIPTION: Is used to produce a formatted Html
   Table Cell tag ("<td>...</td>") with a number of 
   attributes
* STATUS: cludged together
* HISTORY: 
    25th Jan 2001- written
* DOCUMENTED AT: ? 
* CODE LOCATION: 
    \LibraryCode\Java\Classes\SourceCode1\ 
* TO DO: create a setContents method for a THtmlInputElement object
   Add a Colspan attribute, this is esssential.
------------------------------------------------------*/ 

public class THtmlTableCell
{
  public static int ALIGNTOP = 1;
  public static int ALIGNBOTTOM = 2;
  public static int ALIGNLEFT = 3;
  public static int ALIGNRIGHT = 4;

  private int iContentsAlignment;
  private StringBuffer sbBackgroundColour;
  private Object ooContents;     
  //-- ooContents Usually (only?) THtmlFont or THtmlInputElement
  //-- Perhaps just could be a string since the THtmlFont element
  //-- has a to string method.?? 

  //-----------------------------------
  // Constructors
  //-----------------------------------
  public THtmlTableCell
   (THtmlFont hfContents, String sBackgroundColour, int iContentsAlignment)
  {
    this.iContentsAlignment = iContentsAlignment;
    this.sbBackgroundColour = new StringBuffer(sBackgroundColour);
    this.ooContents = hfContents;
  } /* Constructor: (S, SB, i) */

  public THtmlTableCell
   (String sContents, String sBackgroundColour, int iContentsAlignment)
    { this(new THtmlFont(sContents), sBackgroundColour, iContentsAlignment); }

  public THtmlTableCell(String sContents, String sBackgroundColour)
    { this(sContents, sBackgroundColour, THtmlTableCell.ALIGNRIGHT); }

  public THtmlTableCell(String sContents)
    { this(sContents, "white", THtmlTableCell.ALIGNRIGHT); }

  public THtmlTableCell()
    { this("", "white", THtmlTableCell.ALIGNRIGHT); }

  //-----------------------------------
  // Accessor methods
  //-----------------------------------
  public void setContentsAlignment(int iContentsAligment)
    { this.iContentsAlignment = iContentsAlignment; }

  public void setBackgroundColour(String sBackgroundColour)
    { this.sbBackgroundColour = new StringBuffer(sBackgroundColour); }

  //-- Need to write set methods for the THtmlInputElement
  //-- and the THtmlFont objects

  public void setContents(String sContents)
    { this.ooContents = new THtmlFont(sContents); }

  public void setContents(THtmlFont hfContents)
    { this.ooContents = hfContents; }

  public int getContentsAlignment()
    { return this.iContentsAlignment; }

  public String getBackgroundColour()
    { return this.sbBackgroundColour.toString(); }
 
  public String getContents()
    { return this.ooContents.toString(); }

  //-----------------------------------
  // toString method
  //-----------------------------------
  public String toString()
  {
    String sReturn;
    
    //-- This is a bad implementation since it prevents concurrent
    //-- horizontal and vertical alignments. Must be changed.
    //-- Add another attribute- iVerticalAlignment, iHorizontalAlignment
 
    sReturn = 
      "<td   bgcolor = \"" + this.sbBackgroundColour + "\" \n";
    if (this.iContentsAlignment == THtmlTableCell.ALIGNTOP)
      { sReturn += "       valign = 'top'> \n"; }
    else if (this.iContentsAlignment == THtmlTableCell.ALIGNBOTTOM)
      { sReturn += "       valign = 'bottom'> \n"; }
    else if (this.iContentsAlignment == THtmlTableCell.ALIGNLEFT)
      { sReturn += "        align = 'left'> \n"; }
    else if (this.iContentsAlignment == THtmlTableCell.ALIGNRIGHT)
      { sReturn += "        align = 'right'> \n"; }

    sReturn += this.ooContents + " \n";
    sReturn += "</td> \n";

    return sReturn;
  } /* method: toString */
  
  //-- Test script
  public static void main(String[] args)
  {
    String sDisplay;
    THtmlTableCell htcTest;
    THtmlFont hfFont;
   
    sDisplay = 
      "+----------------------------------------------------------+ \n" +
      "|  The THtmlTableCell class:                               | \n" +
      "|                                                          | \n" +
      "|  This purpose of this class is to produce an Html        | \n" +
      "|  'td' tag with a (small) number of attributes and        | \n" +
      "|  containing some text.                                   | \n" +
      "|  The toString() method produces the formatted Html text. | \n" +
      "|  The THtmlTableCell class has a dependency on the        | \n" +
      "|  THtmlFont class                                         | \n" +
      "|                                                          | \n" +
      "+----------------------------------------------------------+ \n" +
      "|  Testing the toString() method with a                    | \n" +
      "|  constructor (THtmlFont, String, int)...                 | \n" +
      "+----------------------------------------------------------+ \n\n";       

    hfFont = new THtmlFont("hello font in cell");
    htcTest = new THtmlTableCell(hfFont, "red", THtmlTableCell.ALIGNTOP);    
    sDisplay += htcTest;

    sDisplay +=
      "+----------------------------------------------------------+ \n" +
      "|  Testing the toString() method with a THREE argument     | \n" +
      "|  constructor(String, String, int)...                     | \n" +
      "+----------------------------------------------------------+ \n\n";       

    htcTest = new THtmlTableCell(
       "some string", "red", THtmlTableCell.ALIGNTOP);    

    sDisplay += htcTest;
    System.out.println(sDisplay);
  } //-- main
} //--class: THtmlTableCell