
// Copyright (c) 1999, 2000 Oracle Corporation
package 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.*;

public class InputTagBase extends TagSupport
{
   protected String            sDataSource;
   protected String            sDataItem = null;
   protected HTMLFieldRenderer rndObj = null;
   protected RowSet            rs = null;
   protected DataWebBeanImpl   dwb = null;
   protected Row               row = null;
   protected HTMLRenderingContext ctx = null;
   protected Object            dataItem = null;
   protected int               nCols = 0;

   public InputTagBase()
   {
   }

   public void setDatasource(String sDataSource)
   {
      this.sDataSource = sDataSource;
   }

   public void setDataitem(String sValue)
   {
      this.sDataItem = sValue;
   }


   public void setCols(String sValue) throws JspException
   {
    try
    {
      nCols = Integer.parseInt(sValue);
    }
    catch(Exception ex)
    {
      throw new JspException(ex.getMessage());
    }
   }

   public HTMLFieldRenderer getFieldRenderer()
   {
      return new TextField();
   }
   
   public void internalInitialize() throws JspException
   {
      try
      {
            rs = (RowSet)pageContext.getAttribute(sDataSource + "_RowSet");
            dwb = (DataWebBeanImpl)pageContext.getAttribute(sDataSource);

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

            ctx = dwb.getRenderingContext();

            row = rs.getCurrentRow();

            if( row == null)
               row = rs.first();

            // get the renderer for the attribute type
            if(row != null)
               dataItem = row.getAttribute(sDataItem);

            rndObj = getFieldRenderer();

            if(rndObj != null)
            {
                if(nCols > 0)
                   rndObj.setDisplayWidth(nCols);
            }

         }
         catch(Exception ex)
         {
            throw new JspException(ex.getMessage());
         }
   }
   
   /**
     * 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();

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

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

   /**
     * release() called after doEndTag() to reset state
     */
   public void release()
   {
      sDataSource = null;
      sDataItem = null;
      rndObj = null;
      rs = null;
      dwb = null;
      row = null;
      ctx = null;
      dataItem = null;

      super.release();
   }

}

 