/*
 * 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.*;
import com.silverwrist.venice.except.ValidationException;

public class CDIntegerFormField extends CDTextFormField
{
  /*--------------------------------------------------------------------------------
   * Static data members
   *--------------------------------------------------------------------------------
   */

  private static final double LN10 = Math.log(10.0);

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

  private int min_value;
  private int max_value;

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

  public CDIntegerFormField(String name, String caption, String caption2, int min, int max)
  {
    super(name,caption,caption2,true,numDigits(min,max),numDigits(min,max));
    this.min_value = min;
    this.max_value = max;

  } // end constructor

  protected CDIntegerFormField(CDIntegerFormField other)
  {
    super(other);
    this.min_value = other.min_value;
    this.max_value = other.max_value;

  } // end constructor

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

  private static int numDigits(int v)
  {
    if (v==0)
      return 1;
    else if (v>0)
      return 1 + (int)(Math.floor(Math.log((double)v) / LN10));
    else
      return 2 + (int)(Math.floor(Math.log((double)(-v)) / LN10));

  } // end numDigits

  private static int numDigits(int min, int max)
  {
    return Math.max(Math.max(numDigits(min),numDigits(max)),2);

  } // end numDigits

  /*--------------------------------------------------------------------------------
   * Overrides from class CDTextFormField
   *--------------------------------------------------------------------------------
   */

  protected void renderActualField(Writer out, RenderData rdat) throws IOException
  {
    super.renderActualField(out,rdat);
    out.write("&nbsp;(" + min_value + " - " + max_value + ")");

  } // end renderActualField

  protected void validateContents(String value) throws ValidationException
  {
    super.validateContents(value);
    try
    { // convert to an integer and check against range
      int x = Integer.parseInt(value);
      if ((min_value>Integer.MIN_VALUE) && (x<min_value))
	throw new ValidationException("The value of the '" + getCaption()
				      + "' field must be greater than or equal to " + min_value + ".");
      if ((max_value<Integer.MAX_VALUE) && (x>max_value))
	throw new ValidationException("The value of the '" + getCaption()
				      + "' field must be less than or equal to " + max_value + ".");

    } // end try
    catch (NumberFormatException nfe)
    { // integer conversion failed
      throw new ValidationException("Invalid non-numeric character in the '" + getCaption() + "' field.");

    } // end catch
    
  } // end validateContents

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

  public Object getObjValue()
  {
    try
    { // build an Integer and return it
      return new Integer(getValue());

    } // end try
    catch (NumberFormatException nfe)
    { // shouldn't happen
      return null;

    } // end catch

  } // end getObjValue

  public CDFormField duplicate()
  {
    return new CDIntegerFormField(this);

  } // end clone

} // end class CDIntegerFormField
