/*
 * @(#)SetDomainRendererTag.java
 *
 * Copyright 2001-2002 by Oracle Corporation,
 * 500 Oracle Parkway, Redwood Shores, California, 94065, U.S.A.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information
 * of Oracle Corporation.
 */

package oracle.jbo.html.jsp.datatags;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.Tag;
import javax.servlet.jsp.tagext.TagSupport;
import oracle.jbo.JboException;
import oracle.jbo.html.HtmlServices;
import oracle.jdeveloper.html.HTMLFieldRenderer;

public class SetDomainRendererTag extends TagSupport
{
   protected String fieldType;
   protected String sDomainClassName;
   protected String sClassRenderer;
   protected String sScope;

   public void setFieldtype(String sType)
   {
      if ("DISPLAY".equalsIgnoreCase(sType))
      {
         fieldType = HtmlServices.DISP_RENDERER_KEY;
      }
      else if ("EDIT".equalsIgnoreCase(sType))
      {
         fieldType = HtmlServices.EDIT_RENDERER_KEY;
      }
   }

   public void setDomain(String sValue)
   {
      sDomainClassName = sValue;
   }
   
   public void setClassname(String className)
   {
      sClassRenderer = className;
   }

   public void setScope(String sValue)
   {
      sScope = 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
   {
      int scope;
      
      if ("page".equalsIgnoreCase(sScope))
      {
         scope = PageContext.PAGE_SCOPE;
      }
      else if ("request".equalsIgnoreCase(sScope))
      {
         scope = PageContext.REQUEST_SCOPE;
      }
      else if ("session".equalsIgnoreCase(sScope))
      {
         scope = PageContext.SESSION_SCOPE;
      }
      else if ("application".equalsIgnoreCase(sScope))
      {
         scope = PageContext.APPLICATION_SCOPE;
      }
      else
      {
         throw new JboException(Res.getString(Res.SETDOMAINRENDERER_INVALID_SCOPE));
      }

      HTMLFieldRenderer rField = HtmlServices.getFieldRendererFromClassName(sClassRenderer, pageContext);

      String rendererKey = HtmlServices.getRendererKeyFromDomainName(sDomainClassName, fieldType);

      pageContext.setAttribute(rendererKey, sClassRenderer, scope);
      
      return Tag.SKIP_BODY;
   }


   public void release()
   {
      fieldType = null;
      sDomainClassName = null;
      sClassRenderer = null;
      sScope = null;
      
      super.release();
   }
}

