/*
 * The contents of this file are subject to the Mozilla Public License Version 1.1
 * (the "License"); you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at <http://www.mozilla.org/MPL/>.
 * 
 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
 * language governing rights and limitations under the License.
 * 
 * The Original Code is the Venice Web Communities System.
 * 
 * The Initial Developer of the Original Code is Eric J. Bowersox <erbo@silcom.com>,
 * for Silverwrist Design Studios.  Portions created by Eric J. Bowersox are
 * Copyright (C) 2001 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
 * 
 * Contributor(s): 
 */
package com.silverwrist.venice.servlets.format;

import java.io.Writer;
import java.io.IOException;
import com.silverwrist.util.StringUtil;
import com.silverwrist.venice.except.ValidationException;

public abstract class CDBaseFormField implements CDFormField, ColorSelectors
{
  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  private String name;
  private String caption;
  private String caption2;
  private boolean required;
  private String value = null;
  private boolean enabled;

  /*--------------------------------------------------------------------------------
   * Constructors
   *--------------------------------------------------------------------------------
   */

  protected CDBaseFormField(String name, String caption, String caption2, boolean required)
  {
    this.name = name;
    this.caption = caption;
    this.caption2 = caption2;
    this.required = required;
    this.enabled = true;

  } // end constructor

  protected CDBaseFormField(CDBaseFormField other)
  {
    this.name = other.name;
    this.caption = other.caption;
    this.caption2 = other.caption2;
    this.required = other.required;
    this.enabled = true;
    // N.B.: do NOT copy value!

  } // end constructor

  /*--------------------------------------------------------------------------------
   * Internal operations
   *--------------------------------------------------------------------------------
   */

  protected final String getCaption()
  {
    return caption;

  } // end getCaption

  /*--------------------------------------------------------------------------------
   * Abstract functions which MUST be overriden
   *--------------------------------------------------------------------------------
   */

  protected abstract void renderActualField(Writer out, RenderData rdat)
      throws IOException;

  /*--------------------------------------------------------------------------------
   * Overridable operations
   *--------------------------------------------------------------------------------
   */

  protected void validateContents(String value) throws ValidationException
  { // this is a do-nothing value
  } // end validateContents

  /*--------------------------------------------------------------------------------
   * Implementations from interface ComponentRender
   *--------------------------------------------------------------------------------
   */

  public void renderHere(Writer out, RenderData rdat) throws IOException
  {
    out.write("<TR VALIGN=MIDDLE>\n<TD ALIGN=RIGHT CLASS=\"content\"><FONT COLOR=\""
	      + rdat.getStdColor(enabled ? CONTENT_FOREGROUND : CONTENT_DISABLED) + "\">"
	      + StringUtil.encodeHTML(caption));
    if (caption2!=null)
      out.write(" " + StringUtil.encodeHTML(caption2));
    out.write(":</FONT>");
    if (required)
      out.write(rdat.getRequiredBullet());
    out.write("</TD>\n<TD ALIGN=LEFT CLASS=\"content\">");
    renderActualField(out,rdat);
    out.write("</TD>\n</TR>\n");

  } // end renderHere

  /*--------------------------------------------------------------------------------
   * Implementations from interface CDFormField
   *--------------------------------------------------------------------------------
   */

  public String getName()
  {
    return name;

  } // end getName

  public String getValue()
  {
    return value;

  } // end getValue

  public void setValue(String value)
  {
    this.value = value;

  } // end setValue

  public Object getObjValue()
  {
    return value;

  } // end getObjValue

  public void setObjValue(Object obj)
  {
    this.value = obj.toString();

  } // end setObjValue

  public boolean isRequired()
  {
    return required;

  } // end isRequired

  public CDCommandButton findButton(String name)
  {
    return null;

  } // end findButton

  public void validate() throws ValidationException
  {
    if (required && enabled && StringUtil.isStringEmpty(value))
      throw new ValidationException("The '" + caption + "' field is required.");
    validateContents(value);

  } // end validate

  public boolean isEnabled()
  {
    return enabled;

  } // end isEnabled

  public void setEnabled(boolean flag)
  {
    enabled = flag;

  } // end setEnabled

} // end CDBaseFormField
