// Copyright (c) 1999, 2000 Oracle Corporation
package oracle.jbo.html.jsp.datatags;

import oracle.jbo.html.jsp.datatags.*;
import javax.servlet.jsp.tagext.*;
import oracle.jdeveloper.html.*;
import javax.servlet.jsp.tagext.*;
import javax.servlet.jsp.*;
import javax.servlet.*;
import oracle.jbo.*;

/**
 * A Class class.
 * <P>
 * @author Juan Oropeza
 */
public class InputSelectBase extends InputTagBase {

   protected String sDataAttribute;
   protected String sDisplayAttribute;
   protected String sDisplaySource;
   protected String sControlType = "RADIOGROUP";
   protected String sFormName = "";
   protected HTMLFieldRenderer pick = null;
   protected boolean bUseLineBreaks = true;
   /**
    * Constructor
    */
   public InputSelectBase() {
   }

   public HTMLFieldRenderer getFieldRenderer()
   {
      if(pick == null)
         pick = new PickList();
      return pick;
   }

   public void setFormname(String sValue)
   {
      this.sFormName = sValue;
   }

   public void setDisplayvaluedataitem(String sValue)
   {
      this.sDataAttribute = sValue;
   }

   public void setDisplaydataitem(String sValue)
   {
      this.sDisplayAttribute = sValue;
   }

   public void setDisplaydatasource(String sValue)
   {
      this.sDisplaySource = sValue;
   }

   public String getControlType()
   {
      return sControlType;
   }

   public void setLinebreaks(String sValue)
   {
      if(sValue.equalsIgnoreCase("yes"))
      {
         bUseLineBreaks = true;
      }
      else
      {
         bUseLineBreaks = false;
      }
   }

   /**
     * Process the start tag for this instance.
     *
     * The doStartTag() method assumes that all setter methods have been
     * invoked before.
     *
     * When this method is invoked, the body has not yet been invoked.
     *
     * @returns EVAL_BODY_INCLUDE if the tag wants to process body, SKIP_BODY if it
     * does ont want to process it.
     */
   public int doStartTag() throws JspException
   {
         try
         {
               internalInitialize();

               // locate the data source
               oracle.jdeveloper.html.DataWebBeanImpl ds = (oracle.jdeveloper.html.DataWebBeanImpl)pageContext.getAttribute(sDisplaySource);

               if(ds == null)
               {
                  throw new Exception(Res.format(Res.DATA_SOURCE_NOT_FOUND,sDisplaySource));
               }

               String dsPath = ds.getApplicationName() + "." + ds.getViewObjectName();

               ctx.userData.put("control_type", getControlType());

                if(this.sFormName != null)
                  ctx.userData.put("form_name", this.sFormName);
                  
               if(dsPath != null)
                  ctx.userData.put("lov_vo",  dsPath);

               if(sDataAttribute != null)
                  ctx.userData.put("data_attribute", sDataAttribute);

               if(sDisplayAttribute != null)
                  ctx.userData.put("display_attribute", sDisplayAttribute);

                 //setup line breaks
               HTMLFieldRenderer rdr = getFieldRenderer();

               if(rdr instanceof PickList)
               {
                  ((PickList)rdr).setUseLineBreaks(bUseLineBreaks);
               }

               System.out.println("Range Size=" + rs.getRangeSize());

               String sOutput = rndObj.renderToString(ctx, rs, row , sDataItem);

               pageContext.getOut().print(sOutput);
         }
         catch(Exception ex)
         {
            throw new JspException(ex.getMessage());
         }
         return Tag.SKIP_BODY;
   }
}

