/********************************************************************
 *  Oracle JDeveloper
 *  Copyright (c) 1997, 1999, Oracle Corporation. All rights reserved.
 ********************************************************************/


package oracle.jdeveloper.html;

import java.io.PrintWriter;
import java.util.Vector;

/**
 *  Represents an HTML document. All top level HTML objects are added to this object.
 *
 *    This class represents the HTML page. All forms,tables,scripts,... are added to the
 * HTML page via the interfaces of this class.
 *
 *
 * @author  Juan Oropeza
 * @version PUBLIC
 */
public class HTMLDocument extends HTMLElementContainer
{
   protected   String            theTitle;
   protected   String            theImage;
   protected   Vector            theScripts = new Vector();
   protected   Vector            theScriptLibraries = new Vector();
   protected   String            theStyleSheet = null;
   protected   String            clickHandler = null;
   protected   String            theStyle =  null;       // inline style
   
   public  HTMLDocument()
   {
   }
     
   /**
   *	Sets the CSS style to be used by this document. This is added as a STYLE property on the document
   */
   public void setStyle(String style)
   {
      theStyle = style;
   }

   /**
   *	Sets the JavaScript click handler function that will be called when this document receives a mouse click.
   */
   public void setClickHandler(String sHandler)
   {
      clickHandler = sHandler;
   }

   /**
   *	Sets the CSS style sheet URL to be used by this document.
   */   
   public void setStyleSheet(String sSheet)
   {
      theStyleSheet = sSheet;
   }
   
   protected void renderScriptLibraries(PrintWriter out) throws Exception
   {
      int         nSize = theScriptLibraries.size();
      HTMLElement elem = null;
      
      for(int i = 0 ; i < nSize ; i++)
      {
         elem = (HTMLElement)theScriptLibraries.elementAt(i);
         elem.render(out);
      }      
   }
   
   protected void renderScripts(PrintWriter out) throws Exception
   {
      int         nSize = theScripts.size();
      HTMLElement elem = null;
      
      renderScriptLibraries(out);
      
      for(int i = 0 ; i < nSize ; i++)
      {
         elem = (HTMLElement)theScripts.elementAt(i);
         elem.render(out);
      }      
   }
   
   protected void renderStyleSheet(PrintWriter out)
   {
      if(theStyleSheet == null)
          return;
      
      out.println("<LINK REL=STYLESHEET TYPE=\"text/css\" ");
      out.println("HREF=" + theStyleSheet + ">");
   }

   
   protected void renderContainerHeader(PrintWriter out) throws Exception
   {
      out.println("<HTML>");
      out.println("<!...Generated by Oracle HTML Framework !>");
      
      if(theTitle != null)
      {
         out.println("<HEAD><TITLE>" + theTitle + "</TITLE>");

         renderStyleSheet(out);
         
         // render the additional style tags
         if(theStyle != null)
         {
            out.println("<style>\n<!--");
            out.println(theStyle);
            out.println("\n--!></style>");
         }
         
         // render the scripts here
         renderScripts(out);
         
         out.println("</HEAD>");
      }
      else
      {
         out.println("<HEAD>");

         renderStyleSheet(out);
         
         // render the scripts here
         renderScripts(out);
         
         out.println("</HEAD>");         
      }

      if(theImage != null)
      {
         out.print("<BODY CLASS=\"" + getCSSClassName() + "\" BACKGROUND=\"" + theImage + "\" ");
      }
      else
      {
         out.print("<BODY CLASS=\"" + getCSSClassName() + "\" ");
      }

      if(clickHandler != null)
      {
         out.print(" onclick=\""+ clickHandler + "\"");
      }

      out.println(">");

   }
   
   protected void RenderContainerFooter(PrintWriter out) throws Exception
   {
      out.println("</BODY></HTML>");      
   }
   

   /**
    *    Adds an HTML script to the document.
    */
   public void addScript(HTMLScript aScript)
   {
      theScripts.addElement(aScript);
   }
   
   /**
    *    Adds a script library to the document.
    *
    *    The script library is added to the HEAD section of the document.
    * This is very useful for global JavaScript routines that are located
    * in a .js file. This is used instead of generating the same set of
    * functions into multiple HTML pages.
    */
   public void addScriptLibrary(String LibraryPath)
   {

      String sText;

      sText = "<SCRIPT  SRC=\"" + LibraryPath + "\"></SCRIPT>";
      
      theScriptLibraries.addElement(new HTMLTextElement(sText));
   }
   
   public void render(PrintWriter out) throws Exception
   {
      super.render(out);

      out.flush();
      
   }

   /**
    *    Sets the HTML document's title.
    */
   public void setTitle(String sTitle)
   {
      theTitle = sTitle;
   }

   public String getTitle()
   {
      return theTitle;
   }

   /**
    *    Sets the HTML document's background image.
    */
   public void setBackgroundImage(String sImage)
   {
      theImage = sImage;
   }

   /**
   *	returns the currently set background image
   */
   public String getBackgroundImage()
   {
      return theImage;
   }



 }
