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

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

  private static final int MINSIZE = 2;

  public static final String TAGNAME = "password";

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

  private int size;               // the SIZE of the field
  private int maxlength;          // the MAXLENGTH of the field
  private int real_maxlength;     // the actual maximum length

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

  public PasswordField(String name, String caption, String caption2, boolean required, int size, int maxlength)
  {
    super(false,name,caption,caption2,required);
    this.size = Math.max(size,MINSIZE);
    this.maxlength = Math.max(maxlength,MINSIZE);
    this.real_maxlength = maxlength;

  } // end constructor

  public PasswordField(String name, String caption, String caption2, boolean required, int size)
  {
    super(false,name,caption,caption2,required);
    this.size = Math.max(size,MINSIZE);
    this.maxlength = Math.max(size,MINSIZE);
    this.real_maxlength = size;

  } // end constructor

  public PasswordField(Element elt) throws ConfigException
  {
    super(false,elt,true,true);
    XMLLoader loader = XMLLoader.get();
    int my_size = loader.configGetAttributeInt(elt,"size");
    DOMElementHelper h = new DOMElementHelper(elt);
    if (h.hasAttribute("maxlength"))
    { // read the maxlength attribute and fill in the fields
      this.real_maxlength = loader.configGetAttributeInt(elt,"maxlength");
      this.size = Math.max(my_size,MINSIZE);
      this.maxlength = Math.max(this.real_maxlength,MINSIZE);
      
    } // end if
    else
    { // fill in the fields
      this.real_maxlength = my_size;
      this.size = this.maxlength = Math.max(my_size,MINSIZE);

    } // end else

  } // end constructor

  protected PasswordField(PasswordField other)
  {
    super(other);
    this.size = other.size;
    this.maxlength = other.maxlength;
    this.real_maxlength = other.real_maxlength;

  } // end constructor

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

  protected void renderField(RequestOutput out) throws IOException
  {
    out.write("<SPAN CLASS=\"cinput\"><INPUT TYPE=PASSWORD CLASS=\"cinput\" NAME=\"" + getName() + "\" SIZE="
	      + size + " MAXLENGTH=" + maxlength);
    if (!isEnabled())
      out.write(" DISABLED");
    out.write(" VALUE=\"");
    if (getStringValue()!=null)
      out.write(getStringValue());
    out.write("\"></SPAN>");

  } // end renderField

  protected void validateContents(String value) throws ValidationException
  {
    if (value.length()>real_maxlength)
      throw new ValidationException("The value of the '" + getCaption() + "' field must be no longer than "
				    + real_maxlength + " characters.");

  } // end validateContents

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

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

  } // end duplicate

} // end class PasswordField
