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

package oracle.jdeveloper.html;

import oracle.jdeveloper.jsp.wb.EditForm;
import oracle.jbo.*;

/**
 *
 *	Represents a date field render.
 *
 * @version PUBLIC
 *
 **/
 
public class DateField extends HTMLFieldRendererImpl
{
   
   WebBeanImpl wb = new WebBeanImpl();
      
   public DateField()
   {
   }

   public String createDateField(String theName , String sName , String sValue, String sWidth, String sMaxLength)
   {
      HTMLInputElement input = new HTMLInputElement();
      String imageBase = "/webapp/images";

      input.setType("TEXT");
      input.setMaxLength(sMaxLength);
      input.setSize(sWidth);
      input.setName(sName);
      input.setValue(sValue);
      input.setOnFocus("this.select()");
      input.setOnChange("this.value = datecheck(this.value,NLSFormat);checkForError(this,null)");

      DHTMLElementContainer cnt = new DHTMLElementContainer();
      cnt.addElement(input);
      cnt.addElement(new HTMLTextElement("<a href=\"javascript:void opencal(document." + theName + "." + sName + ");\"><img src=" + imageBase + "/FNDICLDR.gif align=absmiddle border=0 alt=\"Calendar for date entry assistance\"></a>"));

      return cnt.getAsString();
   }

   public String renderToString(HTMLRenderingContext ctx , RowSet rs, Row row , String sAttribute)
   {
      String sRet = "";
      
      try
      {
         AttributeDef aDef = rs.getViewObject().findAttributeDef(sAttribute);

         if(row.getAttribute(sAttribute) != null)
           sValue = row.getAttribute(sAttribute).toString();
         else
           sValue = null;

         if(ctx.page != null)
           wb.initialize(ctx.page);
         else
           wb.initialize(ctx.application, ctx.session, ctx.request, ctx.response, ctx.response.getWriter());
           
         
         // Need to pass down the nlsformat to the wb bean, because it will
         // use it during the initBeanForJS call
         String NLSFormat = (String) ctx.userData.get("NLSFormat");
         if (NLSFormat != null)
         {
            wb.getRenderingContext().userData.put("NLSFormat", NLSFormat);
         }
        
         wb.initBeanForJS(WebBean.JSCalendarConstructorLib);

         if (ctx.userData.get("form_name") != null)
         {
            sFormName = ctx.userData.get("form_name").toString();            
         }
         
         sRet = createDateField(sFormName, sAttribute, sValue, "20", "20");
      }
      catch(Exception ex)
      {
         ex.printStackTrace();
      }

      return sRet;   
   }
}

 