/**
 * 
 */
package e4s.tutorial;

import e4s.html.*;
import e4s.html.ajax.E4AjaxData;
import e4s.html.ajax.E4AjaxData_Intf;
import e4s.servlet.E4ModuleImplementation;
import e4s.servlet.E4ServletImplementation_Intf;
import e4s.util.E4StringBufferHtml;

/**
 * Ajax timer-refresh example.
 *
 * {@tutorial Example_Ajax_Timer}
 *
 * @see e4s.html.ajax.Ajax_Intf
 */
public class Example_Ajax_Timer extends E4ModuleImplementation
{
   public static E4Method start = null;
   
   private static class Data extends E4AjaxData implements E4AjaxData_Intf
   {
      private int m_num_calls = 1;
     
      /**
       * Get an identification for this Ajax data object, for the case that more
       * instances of this object are used within the same session, then a unique
       * identifier is required. 
       */
      public String getDivId()
      {
         return "TIMER";
      }
      
      public TABLE makeContent()
      {
         TABLE res = new TABLE();
         res.setBorder();
         
         TR tr1 = res.TR();
         tr1.TD().print("Random Value:");
         tr1.TD().print((int)(Math.random() * 1000.0));

         TR tr2 = res.TR();
         tr2.TD().B().print("Number of Calls:");
         tr2.TD().print(m_num_calls++);

         TR tr3 = res.TR(); 
         tr3.TD().I().print("A Link:");
         A branch = tr3.TD().A("http://www.google.com");
         branch.setTarget(new E4FrameName("_newwin"));
         branch.print("www.google.at");
         
         return res;
      }
      
      public void toHtml( E4StringBufferHtml buf, E4CgiParams params, E4ServletImplementation_Intf servlet, boolean initial ) 
      throws Exception
      {
         TABLE table = makeContent();
         table.toHtml(buf,servlet);
      }
   }
 
   
   public void start( HTML html )
   {
      BODY body = html.BODY();
      
      // this is the data object, it will be displayed asynchronly whenever
      // the client's timeout indicates a new display refresh
      Data data = new Data();

      // create a table, this will later contain the Ajax-HTML element
      TABLE table = body.TABLE(TABLE.E4S_DEFAULT_TABLE());
      
      // this are label texts only
      TD tdLabel[] = table.TR().TD(2);
      TD tdAjax = table.TR().TD();
      
      tdLabel[0].print("Ajax Example");
      tdLabel[0].setBgColor(E4Color.LIGHT_YELLOW);
      
      tdLabel[1].I().print("Please note, that only the content in the table cell left is updated dynamically.");
      tdLabel[1].setRowspan(2);
      
      // this is the Ajax Element, compatible with the E4S framework and
      // cab be integrated into e.g. the TD element. Basically, the ae element
      // is a <DIV> tag with a specified - or in this case - unique generic 
      // identification.
      E4AjaxElement ae = tdAjax.AjaxElement(data,getServlet());
      
      // we set the refresh timeout for this Ajax Element here
      ae.setTimeout(2000);
      
      // the body tag requires preperation
      ae.prepare(body);
   }
}