/*
 * 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 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.*;
import com.silverwrist.dynamo.util.*;

class CheckBoxField extends BaseDialogField
{
  /*--------------------------------------------------------------------------------
   * Static data members
   *--------------------------------------------------------------------------------
   */

  private static final String YES = "Y";

  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  private String m_value;
  private boolean m_default_checked;
  private boolean m_checked;

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

  CheckBoxField(Element elt) throws DialogException
  {
    super(true,elt);
    XMLLoader loader = XMLLoader.get();
    try
    { // get the checkbox value
      m_value = loader.getAttribute(elt,"value",YES);
      m_default_checked = loader.getAttributeBoolean(elt,"checked",false);

    } // end try
    catch (XMLLoadException e)
    { // translate into DialogException
      throw new DialogException(e);

    } // end catch

    m_checked = m_default_checked;

  } // end constructor

  protected CheckBoxField(CheckBoxField other)
  {
    super(other);
    m_value = other.m_value;
    m_default_checked = other.m_default_checked;
    m_checked = m_default_checked;

  } // end constructor

  /*--------------------------------------------------------------------------------
   * Overrides from class BaseDialogField
   *--------------------------------------------------------------------------------
   */

  protected boolean isNull(Object value)
  {
    return false;

  } // end isNull

  public int getFlags()
  {
    return 0;

  } // end getFlags

  /*--------------------------------------------------------------------------------
   * Abstract implementations from class BaseDialogField
   *--------------------------------------------------------------------------------
   */

  protected void renderField(TextRenderControl control, Map render_params)
      throws IOException, RenderingException
  {
    PrintWriter wr = control.getWriter();
    wr.write("<input type=\"checkbox\" name=\"" + getName() + "\" value=\"" + m_value + "\"");
    if (!isEnabled())
      wr.write(" disabled=\"disabled\"");
    if (m_checked)
      wr.write(" checked=\"checked\"");
    wr.write(" />");

  } // end renderField

  protected void validateContents(Request r, Object data) throws ValidationException
  { // do nothing
  } // end validateContents

  public Object clone()
  {
    return new CheckBoxField(this);

  } // end clone

  public Object getValue()
  {
    return Boolean.valueOf(m_checked);

  } // end getValue

  public boolean containsValue()
  {
    return true;

  } // end containsValue

  public void setValue(Object obj)
  {
    if (obj==null)
      m_checked = m_default_checked;
    else if (obj instanceof Boolean)
      m_checked = ((Boolean)obj).booleanValue();
    else if (obj instanceof Number)
      m_checked = (((Number)obj).intValue()!=0);
    else
    { // look at the string equivalent
      String s = obj.toString();
      if (StringUtils.isBooleanTrue(s))
	m_checked = true;
      else if (StringUtils.isBooleanFalse(s))
	m_checked = false;

    } // end else

  } // end setValue

  public void setValueFrom(Request r)
  {
    RequestHelper rh = new RequestHelper(r);
    String[] vals = rh.getStringParameterValues(getName());
    if (vals!=null)
    { // if no values are set, we're definitely NOT checked
      for (int i=0; i<vals.length; i++)
      { // look and see if our value is among the values for this item
	if (m_value.equals(vals[i]))
	{ // found our value!  bail out!
	  m_checked = true;
	  return;

	} // end if

      } // end for

    } // end if

    m_checked = false;

  } // end setValueFrom

  public void reset()
  {
    m_checked = m_default_checked;

  } // end reset

} // end class CheckBoxField
