/*
 * 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.ui.dlg;

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

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

  private static final String YES = "Y";
  private static final String NO = "";

  public static final String TAGNAME = "checkbox";

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

  public CheckBoxField(String name, String caption, String caption2)
  {
    super(true,name,caption,caption2,false);

  } // end constructor

  public CheckBoxField(Element elt) throws ConfigException
  {
    super(true,elt,true,false);

  } // end constructor

  protected CheckBoxField(CheckBoxField other)
  {
    super(other);

  } // end constructor

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

  protected void renderField(RequestOutput out) throws IOException
  {
    out.write("<INPUT TYPE=CHECKBOX NAME=\"" + getName() + "\" VALUE=\"" + YES + "\"");
    if (!isEnabled())
      out.write(" DISABLED");
    if (YES.equals(getStringValue()))
      out.write(" CHECKED");
    out.write(">");

  } // end renderField

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

  public Object getValue()
  {
    return YES.equals(super.getValue().toString()) ? Boolean.TRUE : Boolean.FALSE;

  } // end getValue

  public void setValue(Object o)
  {
    boolean state = false;
    if (o!=null)
    { // determine what kind of object it is
      if (o instanceof Boolean)
      { // it's a Boolean - map it accordingly
	Boolean b = (Boolean)o;
	state = b.booleanValue();

      } // end if
      else if (o instanceof Number)
      { // it's a number - use false==0, true==nonzero
	Number n = (Number)o;
	state = (n.intValue()!=0);

      } // end else if
      else
      { // it's something else - parse a string equivalent
	String s = o.toString().trim().toLowerCase();
	try
	{ // decode it into a number and set state that way
	  int tmp = Integer.parseInt(s);
	  state = (tmp!=0);

	} // end try
	catch (NumberFormatException nfe)
	{ // try some string equivalents
	  state = (s.equals("true") || s.equals("on") || s.equals("y") || s.equals("yes"));

	} // end catch

      } // end else

    } // end if

    super.setValue(state ? YES : NO);

  } // end setValue

  public void setValueFrom(RequestInput ri)
  {
    this.setValue(ri.getParameter(getName()));

  } // end setValueFrom

  public DialogField duplicate()
  {
    return new CheckBoxField(this);

  } // end duplicate

} // end class CheckBoxField
