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


package oracle.jdeveloper.html;

import java.io.PrintWriter;
import java.util.Vector;
import oracle.jbo.*;


/**
 * This class provides a base implementation of the <tt>HTMLFieldRenderer</tt>
 * interface. Use this class as
 * the base class for any new field renderers.
 *
 *
 *
 **/
public class HTMLFieldRendererImpl implements HTMLFieldRenderer
{
    protected   int nWidth = 0;
    protected   int nLength = 0;
    protected   int nHeight = 1;
    protected   String sPrompt;
    protected   String sClass;
    protected   String sValue;
    protected   String sName;
    protected   String sFormName = null;
    
    public HTMLFieldRendererImpl()
    {
    }

  /**
  *	Sets this FORM field's name. This will be the name of the input parameter
	*   when the HTML form is submitted.
  * <p>
  * @param sName name of this FORM field.
	*/
	public void setFieldName(String sName)
    {
      this.sName = sName;
    }

    /**
  *	Gets this FORM field's name. This will be the name of the input parameter
	*   when the HTML form is submitted.
  * <p>
  * @return this FORM field's name.
	*/	
    public String getFieldName()
    {
      return sName;
    }

	/**
  *	Sets the display width, in characters, of this field.
  * <p>
  * @param nWidth the display width, in characters.
	*/
	public void setDisplayWidth(int nWidth)
    {
        this.nWidth = nWidth;
    }
	
	/**
	*	Gets the display width, in characters, of this field.
  * <P>
  * @return the display width, in characters.
	*/
    public int  getDisplayWidth()
    {
        return nWidth;
    }

	/**
	*	Sets the display height, in characters, of this field.
  * <p>
  * @param nHeight the display height, in characters.
	*/
	public void  setDisplayHeight(int nHeight)
    {
      this.nHeight = nHeight;
    }

	/**
	*	Gets the display height, in characters, of this field.
  * <p>
  * @return the display height, in characters.
	*/	
    public int   getDisplayHeight()
    {
      return nHeight;
    }
	
	/**
	*	Sets the maximum input length, in characters, of this field. The user will
  * not be able to enter more than <tt>nLength</tt> characters.
  * <p>
  * @param nLength the maximum input length, in characters.
	*/
    public void setMaxDataLength(int nLength)
    {
        this.nLength = nLength;
    }

	/**
  * Gets the maximum input length, in characters, of this field. This is the maximum
  * number of characters the user can enter in the field.
  * <p>
	*	@return the maximum input length, in characters.
	*/
	public int  getMaxDataLength()
    {
        return nLength;
    }

	/**
	*	Sets the prompt to be displayed next to this input field.
  * <p>
  * @param sPrompt the prompt for this input field.
	*/	
    public void setPromptText(String sPrompt)
    {
        this.sPrompt = sPrompt;
    }

	/**
	*	Gets the prompt to be displayed next to this input field.
  * <p>
  * @return the prompt displayed next to this input field.
	*/	
   public String getPromptText()
   {
      return sPrompt;
   }

	/**
	*	Sets the CSS (cascading style sheet) class name to be used by this field. You
  * can the use this class name
	* in your CSS file to change the visual attributes of the field.
  * <p>
  * @param sClass the class name of the CSS.
	*/	
    public void setCSSClassName(String sClass)
    {
       this.sClass = sClass;
    }

	/**
  * Gets the name of the CSS (cascading style sheet) used by this field.
  * <p>
	*	@return the class name of the CSS.
	*/	
    public String getCSSClassName()
    {
       return sClass;
    }

	/**
	* Sets this field's current value.
  * <p>
  * @param sValue current value of this field.
	*/	
    public void setValue(String sValue)
    {
       this.sValue = sValue;
    }

	/**
  * Gets this field's current value.
	*	<p>
  * @return this field's current value.
	*/	
    public String getValue()
    {
        return sValue;
    }

   public String renderToString(HTMLRenderingContext ctx , RowSet rs, Row row , String sAttribute)
   {
      String sText;

      ViewObject   vo = rs.getViewObject();
      AttributeDef aDef = vo.findAttributeDef(sAttribute);
      String       sLabel = this.getPromptText();

      // use the field width only if it has been overriden
      if(this.nWidth == 0)
        this.setDisplayWidth(aDef.getPrecision());

      Object obj = row.getAttribute(sAttribute);

      if(obj == null)
      {
         sValue = "";
      }
      else
      {
         sValue = obj.toString();
      }

      sText = "<INPUT TYPE=\"TEXT\"  NAME=\""+ sAttribute + "\" VALUE=\"" + sValue + "\" ROWS=\"" + this.getDisplayHeight() + "\" SIZE=\"" + this.getDisplayWidth() + "\" />";

      // also add the hidden field for use in comparisons
      sText += "<INPUT TYPE=\"HIDDEN\" NAME=\"_" + sAttribute + "\" VALUE=\"" + sValue + "\" />";

      return sText;
   }

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

   public String getFormName()
   {
      return this.sFormName;
   }
}
