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

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 DisplayTag extends TagSupport
{
   protected String            sDataSource;
   protected String            sDataItem = null;

   
   public DisplayTag()
   {
   }

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

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

   /**
     * 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         {

            RowSet            rs = (RowSet)pageContext.getAttribute(sDataSource + "_RowSet");
            DataWebBeanImpl   dwb = (DataWebBeanImpl)pageContext.getAttribute(sDataSource);

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

            Row row = rs.getCurrentRow();

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

            if( row == null)
            {
               pageContext.getOut().print("&nbsp");
            }
            else
            {

               // get the renderer for the attribute type
               
               Object obj = row.getAttribute(sDataItem);

               HTMLRenderingContext ctx = dwb.getRenderingContext();
               HTMLFieldRenderer rndObj = null;

               rndObj = dwb.getDisplayFieldRenderer(row, rs.getViewObject().findAttributeDef(sDataItem));
               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;

      super.release();
   }
}

 