/*
 * 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-02 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
 * 
 * Contributor(s): 
 */
package com.silverwrist.venice.ui.dlg;

import java.io.IOException;
import org.w3c.dom.*;
import com.silverwrist.util.*;
import com.silverwrist.venice.except.*;
import com.silverwrist.venice.ui.*;
import com.silverwrist.venice.ui.helpers.HTMLRendering;
import com.silverwrist.venice.util.XMLLoader;

public abstract class BaseDialogField implements DialogField
{
  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  private boolean reverse;              // if true, field is on left, false = right
  private String name;                  // field name (parameter name)
  private String caption;               // primary caption
  private String caption2;              // secondary caption
  private boolean required;             // is this field required?
  private String value = null;          // string representation of value
  private boolean enabled;              // are we enabled?

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

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

  } // end constructor

  protected BaseDialogField(boolean reverse, Element elt, boolean get_caption2, boolean get_required)
      throws ConfigException
  {
    XMLLoader loader = XMLLoader.get();
    this.reverse = reverse;
    this.name = loader.configGetAttribute(elt,"name");
    this.caption = loader.configGetAttribute(elt,"capt");
    if (get_caption2)
      this.caption2 = elt.getAttribute("capt2");
    else
      this.caption2 = null;
    DOMElementHelper h = new DOMElementHelper(elt);
    if (get_required)
      this.required = h.hasAttribute("required");
    else
      this.required = false;
    this.enabled = !(h.hasAttribute("disabled"));

  } // end constructor

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

  } // end constructor

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

  private final void renderCaption(RequestOutput out) throws IOException
  {
    HTMLRendering html = (HTMLRendering)(out.queryService(HTMLRendering.class));

    out.write(html.getFontTag(enabled ? html.CONTENT_FOREGROUND : html.CONTENT_DISABLED,"content")
	      + StringUtil.encodeHTML(caption));
    if (!(StringUtil.isStringEmpty(caption2)))
      out.write(" " + StringUtil.encodeHTML(caption2));
    if (!reverse)
      out.write(":");
    out.write("</FONT>");
    if (required)
      out.write(html.getFontTag("red","content") + "*</FONT>");

  } // end renderCaption

  protected String getCaption()
  {
    return caption;

  } // end getCaption

  protected String getStringValue()
  {
    return value;

  } // end getStringValue

  protected void setCaption2(String s)
  {
    caption2 = s;

  } // end setCaption2

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

  protected abstract void renderField(RequestOutput out) throws IOException;

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

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

  /*--------------------------------------------------------------------------------
   * Implementations from interface RenderDirect
   *--------------------------------------------------------------------------------
   */

  public void render(RequestOutput out) throws IOException
  {
    out.write("<TR VALIGN=MIDDLE>\n<TD ALIGN=RIGHT CLASS=\"content\">");
    if (reverse)
      renderField(out);
    else
      renderCaption(out);
    out.write("</TD>\n<TD ALIGN=LEFT CLASS=\"content\">");
    if (reverse)
      renderCaption(out);
    else
      renderField(out);
    out.write("</TD>\n</TR>\n");

  } // end render

  /*--------------------------------------------------------------------------------
   * Implementations from interface DialogField
   *--------------------------------------------------------------------------------
   */

  public String getName()
  {
    return name;

  } // end getName

  public Object getValue()
  {
    return value;

  } // end getValue

  public void setValue(Object o)
  {
    value = (o==null) ? null : o.toString();

  } // end setValue

  public void setValueFrom(RequestInput ri)
  {
    value = ri.getParameter(name);

  } // end setValueFrom

  public boolean isRequired()
  {
    return required;

  } // end isRequired

  public boolean isFile()
  {
    return false;

  } // end isFile

  public boolean isHidden()
  {
    return false;

  } // end isHidden

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

  } // end validate

  public boolean isEnabled()
  {
    return enabled;

  } // end isEnabled

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

  } // end setEnabled

  public Object sendMessage(String msg, Object data)
  {
    return null;

  } // end sendMessage

} // end class BaseDialogField
