/*
 * 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) 2002-03 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
 * 
 * Contributor(s): 
 */
package com.silverwrist.dynamo.dialog;

import java.io.*;
import java.util.*;
import org.w3c.dom.*;
import com.silverwrist.util.*;
import com.silverwrist.util.xml.*;
import com.silverwrist.dynamo.except.*;
import com.silverwrist.dynamo.iface.*;

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

  private boolean m_reverse;            // if true, field is on left, false = right
  private String m_name;                // field name (parameter name)
  private String m_caption;             // primary caption
  private String m_caption2;            // secondary caption
  private boolean m_required;           // is this field required?
  private boolean m_default_enabled;    // are we enabled by default?
  private boolean m_enabled;            // are we currently enabled?

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

  protected BaseDialogField(boolean reverse, Element element) throws DialogException
  {
    m_reverse = reverse;
    XMLLoader loader = XMLLoader.get();
    try
    { // get the base class stuff
      m_name = loader.getAttribute(element,"name");
      m_caption = loader.getAttribute(element,"caption");
      m_caption2 = element.getAttribute("caption2");
      m_required = loader.getAttributeBoolean(element,"required",false);
      m_default_enabled = loader.getAttributeBoolean(element,"enabled",true);

    } // end try
    catch (XMLLoadException e)
    { // translate the exception and return it
      throw new DialogException(e);

    } // end catch

    m_enabled = m_default_enabled;

  } // end constructor

  protected BaseDialogField(BaseDialogField other)
  {
    m_reverse = other.m_reverse;
    m_name = other.m_name;
    m_caption = other.m_caption;
    m_caption2 = other.m_caption2;
    m_required = other.m_required;
    m_default_enabled = other.m_default_enabled;
    m_enabled = m_default_enabled;

  } // end constructor

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

  private final void renderCaption(TextRenderControl control, Map render_params)
      throws IOException, RenderingException
  {
    PrintWriter wr = control.getWriter();
    if (!m_enabled)
      wr.write("<span style=\"color: silver;\">");
    wr.write(StringUtils.encodeHTML(StringUtils.replaceAllVariables(m_caption,render_params)));
    if (StringUtils.isNotEmpty(m_caption2))
      wr.write(" " + StringUtils.encodeHTML(StringUtils.replaceAllVariables(m_caption2,render_params)));
    if (!m_reverse)
      wr.write(":");
    if (!m_enabled)
      wr.write("</span>");
    if (m_required)
      wr.write("<span style=\"color: red;\"><b>*</b></span>");

  } // end renderCaption

  protected final String getCaption()
  {
    return m_caption;

  } // end getCaption

  protected final void setCaption2(String s)
  {
    m_caption2 = s;

  } // end setCaption2

  protected boolean isNull(Object value)
  {
    return (value==null);

  } // end isNull

  /*--------------------------------------------------------------------------------
   * Abstract operations
   *--------------------------------------------------------------------------------
   */

  protected abstract void renderField(TextRenderControl control, Map render_params)
      throws IOException, RenderingException;

  protected abstract void validateContents(Request r, Object data) throws ValidationException;

  /*--------------------------------------------------------------------------------
   * Implementations from interface NamedObject
   *--------------------------------------------------------------------------------
   */

  public String getName()
  {
    return m_name;

  } // end getName

  /*--------------------------------------------------------------------------------
   * Abstract methods from interface DialogItem
   *--------------------------------------------------------------------------------
   */

  public abstract Object clone();

  /*--------------------------------------------------------------------------------
   * Implementations from interface DialogItem
   *--------------------------------------------------------------------------------
   */

  public void render(TextRenderControl control, Map render_params) throws IOException, RenderingException
  {
    PrintWriter wr = control.getWriter();
    wr.write("<tr valign=\"middle\">\n<td align=\"right\" class=\"content\">");
    if (m_reverse)
      renderField(control,render_params);
    else
      renderCaption(control,render_params);
    wr.write("</td>\n<td align=\"left\" class=\"content\">");
    if (m_reverse)
      renderCaption(control,render_params);
    else
      renderField(control,render_params);
    wr.write("</td>\n</tr>\n");

  } // end render

  public boolean isEnabled()
  {
    return m_enabled;

  } // end isEnabled

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

  } // end setEnabled

  /*--------------------------------------------------------------------------------
   * Abstract methods from interface DialogField
   *--------------------------------------------------------------------------------
   */

  public abstract Object getValue();

  public abstract boolean containsValue();

  public abstract void setValue(Object obj);

  public abstract void setValueFrom(Request r);

  public abstract void reset();

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

  public void validate(Request r) throws ValidationException
  {
    Object val = getValue();
    if (m_required && m_enabled && isNull(val))
    { // null field value - we need to bail out
      ValidationException ve = new ValidationException(BaseDialogField.class,"DialogMessages",
						       "required.field");
      ve.setParameter(0,m_caption);
      throw ve;

    } // end if

    if (m_enabled)
      validateContents(r,val);
				    
  } // end validate

  public int getFlags()
  {
    return (m_required ? FLAG_REQUIRED : 0);

  } // end getFlags

} // end class BaseDialogField
