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

package oracle.jdeveloper.html;

import oracle.jdeveloper.jsp.wb.EditForm;
import oracle.jbo.*;
import java.io.PrintWriter;
/**
 *
 *	Represents a date field render.
 *
 * @version PUBLIC
 *
 **/
 
public class LOVField extends HTMLFieldRendererImpl
{
   protected   String viewObjectName;
   protected   String displayAttributes;
   protected   String dataAttribute;
   
   public LOVField()
   {
   }

   public void setViewObject(String voName)
   {
      viewObjectName = voName;
   }

   public String getViewObject()
   {
      return viewObjectName;
   }

   public void setDisplayAttributes(String dAttributes)
   {
      displayAttributes = dAttributes;
   }
   
   public String getDisplayAttributes()
   {
      return displayAttributes;
   }
   
   public void setDataAttributes(String dataAttr)
   {
      dataAttribute = dataAttr;
   }
   
   public String getDataAttributes()
   {
      return dataAttribute;
   }

   public String createLOVField(String sName, String sValue, String sWidth, String sMaxLength)
   {
      String lovname = sName;

      HTMLInputElement input = new HTMLInputElement();

      input.setType("TEXT");
      input.setMaxLength(sMaxLength);
      input.setSize(sWidth);
      input.setName(sName);
      input.setValue(sValue);

      DHTMLElementContainer cnt = new DHTMLElementContainer();

      cnt.addElement(input);
      if (displayAttributes == null)
      {
         displayAttributes = "";
      }
      cnt.addElement(new HTMLTextElement("<a href=\"#\" onClick=\"var lovname='" + lovname + "'; openModal('/webapp/jsp/lov.jsp?lovvo=" +
                                         this.viewObjectName + "&attr=" + this.getDataAttributes() + "&dattr=" + this.getDisplayAttributes() +
                                         "', 400, 450, '', document." + getFormName() + "." + sName + ", lovname, 'Y')\" >" +
                                         "<IMG SRC=\"/webapp/images/FNDLSTOV.gif\" BORDER=0 align=absmiddle></a>"));


      return cnt.getAsString();
   }

   protected void initializeFromRenderingContext(HTMLRenderingContext ctx)
   {
       if(ctx.userData.get("form_name") != null)
         this.setFormName(ctx.userData.get("form_name").toString());

       if(ctx.userData.get("lov_vo") != null)
         viewObjectName = ctx.userData.get("lov_vo").toString();

       if(ctx.userData.get("data_attribute") != null)
         this.dataAttribute = ctx.userData.get("data_attribute").toString();

       if(ctx.userData.get("display_attribute") != null)
         this.displayAttributes = ctx.userData.get("display_attribute").toString();
   }
   
   public String renderToString(HTMLRenderingContext ctx , RowSet rs, Row row , String sAttribute)
   {
      String sRet = "";
      
      try
      {
         AttributeDef aDef = rs.getViewObject().findAttributeDef(sAttribute);

         sValue = "&nbsp;";
         
         if(row.getAttribute(sAttribute) != null)
            sValue = row.getAttribute(sAttribute).toString();

         // create a WB to generate libraries into page
         WebBeanImpl wb = new WebBeanImpl();

         if(ctx.page != null)
         {
            wb.initialize(ctx.page);
         }
         else
         {
            wb.initialize(ctx.application, ctx.session, ctx.request, ctx.response, new PrintWriter(ctx.response.getOutputStream()));
         }

         wb.initBeanForJS(WebBean.JSModalPageConstructorLib);

         initializeFromRenderingContext(ctx);

         sRet = createLOVField(sAttribute , sValue, ""+aDef.getPrecision(), ""+aDef.getPrecision());
      }
      catch(Exception ex)
      {
         ex.printStackTrace();
      }

      return sRet;   
   }

}

 