package splppp0001;

/**
    Class HTMLBody helps the programmer to build the body of
    an HTML page. Only a subset of possible tags and of their
    options is supported.
*/
public class HTMLBody {
    private String body;
    private static final String prologue = 
        "<HTML> <HEAD>\n" +
        "<TITLE>Dipartimento di Informatica - Universit&agrave degli Studi di Torino </TITLE>\n" +
        "</HEAD>\n" + 
        "<BODY kBGCOLOR=\"#FFFFFF\" TEXT=\"#540218\" LINK=\"#032A0A\" VLINK=\"#808080\" ALINK=\"#032A0A\"" +
        " BACKGROUND=\"/images/bgr3.jpg\">\n" +
        "<CENTER>\n" + 
        "<DIV ALIGN=right>\n" +
        "<IMG SRC=\"/images/titolo7.jpg\" HEGHT=73 WIDTH=560>\n" +
        "</DIV>\n" +
        "<BR>&nbsp; <BR>&nbsp;";
    private static final String epilogue = 
        "<BR> <BR> <TABLE> <TR>\n" +
        "<TD valign=\"center\">\n" +
        "<A TARGET=\"_top\" HREF=\"http://www.educ.di.unito.it/\">\n" +
        "<IMG ALT=\"Home page didattica\" BORDER=0\n SRC=\"/images/home.jpg\"></A>\n" +
        "</TD>\n" +
        "<TD>\n" +
        "[<A TARGET=\"_top\" HREF=\"http://www.educ.di.unito.it/presentazione/\">Presentazione</A>]\n" +
        "[<A TARGET=\"_top\" HREF=\"http://www.educ.di.unito.it/infostudenti/\">Info per studenti</A>]\n" +
        "[<A TARGET=\"_top\" HREF=\"http://www.educ.di.unito.it/infoaziende/\">Info per aziende</A>]\n" +
        "[<A TARGET=\"_top\" HREF=\"http://www.educ.di.unito.it/cgi-bin/bacheca/elencobac.cgi\">Bacheca</A>]\n" +
        "</td>\n" +
        "</TR>\n" +
        "</TABLE>\n" +
        "<TABLE width=90%>\n" +
        "<TR>\n" +
        "<TD colspan=2><HR NOSHADE></TD>\n" +
        "</TR>\n" +
        "<TR>\n" +
        "<TD ALIGN=\"left\" nowrap width=50%>\n" +
        "<A HREF=\"mailto:wwwadm@educ.di.unito.it\"><img src=\"/images/wwwadm.jpg\" border=0></A>" +
        "</TD>\n" +
        "<TD ALIGN=\"right\" nowrap width=50%>\n" +
        "</TD>\n" +
        "</TR>\n" +
        "<TR><TD colspan=2><HR NOSHADE></TD>\n" +
        "</TR>\n" +
        "</TABLE>\n" +
        "</CENTER>\n" +
        "</BODY>\n" +
        "</HTML>\n"; 
    private static final String iniziotabella = 
        "<CENTER>\n" +
        "<TABLE BORDER=0 COLS=2 WIDTH=\"70%\">\n" +
        "<TR>\n" +
        "<td Align=right>\n"; 
    private static final String finetabella = 
        "<td WIDTH=\"10%\"></td>\n" +
        "</TR>\n" +
        "</TABLE>\n" +
        "</CENTER>\n"; 
    /**
        Constructor.
    */
    public HTMLBody() {
        body = "";
    }

    /**
        It inserts in an HTML page body a tag <BR> followed by the text contained in 'txt'.
    */
    public void addLine(String txt) {
        body += "\n<BR>\n" + txt;
    }

    /**
        It inserts a new paragraph containing the text in 'txt'.
    */
    public void addParagraph(String txt) {
        body += "\n<P>\n" + txt;
    }

    /**
        It formats the text in 'txt' by sorrounding it with tags belonging to the family
        <H1> ... </H1> or <H2>...</H2>, etc. 
        The particular tag is chosen according to the value of 'dim'. So if 'dim=1' 
        <H1>...</H1> will be chosen. The so formatted text is added to the page body.
    */
    public void addHeading(int dim, String txt) {
        body += "\n<H" + dim + "> " + txt + "</H" + dim + ">\n";
    }

    /**
        It is used to begin an ordered list. The list is to be ended using method 'endOL'.
    */
    public void beginOL() {
        body += "\n<OL>";
    }

    /**
        It is used to end an ordered list, that was begun by using method 'beginOL'.
    */
    public void endOL() {
        body += "\n</OL>";
    }

    /**
        It is used to begin an unordered list. The list is to be ended using method 'endUL'.
    */
    public void beginUL() {
        body += "\n<UL>";
    }

    /**
        It is used to end an unordered list, that was begun by using method 'beginUL'.
    */
    public void endUL() {
        body += "\n</UL>";
    }

    /**
        It is used to to add items to an (un)ordered list. The text corresponding to the item
        is passed as an argument of the method when this is called.
    */
    public void addListItem(String item) {
        body += "\n<LI> " + item + "</LI>";
    }

    /**
        This is used to add some generic HTML code to a page body.
    */
    public void appendHTML(String html) {
        body += html;
    }

    /**
        This method is used to begin a FORM definition. The FORM definition is to be
        ended by using 'endForm'. The two arguments correspond respectively to
        the values that will be given to the tag modifiers 'method' and 'action'
        of the tag <FORM>.
    */
    public void beginForm(String md, String act) {
        body += "\n<FORM method=\"" + md + "\" action=\"" + act + "\">";
        body += "\n<TABLE>";
    }

    /**
        This is used in order to add a Radio Button to an HTML form.
        The first argument is the name that identifies the set of alternatives
        to which the button belongs, The second argument is the value that
        is assigned to the tag option 'value'. The last argument is a label
        that is added to the radio button so that the user can understand
        its meaning. 
    */
    public void addRadioBut(String group, String value, String label) {
        body += "\n<TR>\n<TD ALIGN=right><INPUT type=\"radio\" name=\"" + group +
                "\" value=\"" + value + "\"></TD>" + "<TD>" + label + "</TD></TR>";
    }

    /**
        This is used in order to add a Checkbox to an HTML form.
        The tag modifiers 'name' and 'value' are respectivelly assigned
        the values of the first and second argument of this method. The third
        argument is a label that is added to the checkbox so that the user can understand
        its meaning. 
    */
    public void addCheckBox(String name, String value, String label) {
        body += "\n<TR>\n<TD ALIGN=right><INPUT type=\"checkbox\" name=\"" + name +
                "\" value=\"" + value + "\"></TD><TD>" + label + "</TD></TR>";
    }

    /**
        This is used in order to add an <INPUT type=text ...> to an HTML form.
        The value of 'label' is used to describe to the final user what kind of information
        (s)he has to insert in the text field. The values of the second and of the third argument
        are used to assign a value to the tag modifiers 'value' and 'size' of tag <INPUT
        type=text ...>.
    */
    public void addInputText(String label, String name, int size) {
        body += "\n<TR>\n<TD ALIGN=right>" + label +
                ": </TD><TD><INPUT type=\"text\" name=\"" +
                name + "\" size=\"" + size + "\"></TD></TR>";
    }

    /**
        This method is used to end a FORM definition. The FORM definition is to be
        started by using 'beginForm'. It automatically adds buttons 'submit' and 
        'reset'.
    */
    public void endForm() {
        body += "\n<TR><TD></TD><TD>\n<INPUT type=\"submit\" value=\"confirm\">";
        body += "\n<INPUT type=\"reset\" value=\"cancel\">\n</TD></TR>";
        body += "\n</TABLE>\n</FORM>";
    }

    /**
        This method inserts a form that consists of a single button.
        'act' identifies the action that will occur when a click on the button occurs;
        the 'method' will always be set to 'post'.
        'label' will be the button label and 'name' will be the value of the tag
        modifier 'name' of the tag <INPUT type=submit ...> that is used to define
        the button.
    */
    public void addButton(String act, String label, String name) {
        body += "\n<FORM method=\"post\" action=\"" + act + "\">";
        body += "\n<TABLE>";
        body += "\n<TR><TD></TD><TD>\n<INPUT type=\"submit\" name=\"" + name 
                + "\" value=\"" + label + "\"></TD></TR>";
        body += "\n</TABLE>\n</FORM>";
    }

    /**
        This method is used to compose the HTML page body.
    */
    public String toString() {
        return prologue + iniziotabella + body + finetabella + epilogue;
    }
}
