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

abstract class CommonTextField extends BaseDialogField
{
  /*--------------------------------------------------------------------------------
   * Static data members
   *--------------------------------------------------------------------------------
   */

  private static final int MINSIZE = 2;

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

  private String m_tagname;
  private String m_value = null;
  private int m_size;
  private int m_maxlength;
  private int m_real_maxlength;

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

  protected CommonTextField(String tagname, Element elt) throws DialogException
  {
    super(false,elt);
    m_tagname = tagname;
    XMLLoader loader = XMLLoader.get();
    try
    { // load the "size" attributes for the text field
      int my_size = loader.getAttributeInt(elt,"size");
      DOMElementHelper h = new DOMElementHelper(elt);
      if (h.hasAttribute("maxlength"))
      { // get the maxlength and figure it in...
	m_real_maxlength = loader.getAttributeInt(elt,"maxlength");
	m_maxlength = Math.max(m_real_maxlength,MINSIZE);
	m_size = Math.max(my_size,MINSIZE);

      } // end if
      else
      { // "size" is all we got - maxlength is equal to it
	m_real_maxlength = my_size;
	m_maxlength = m_size = Math.max(my_size,MINSIZE);

      } // end else

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

    } // end catch

  } // end constructor

  protected CommonTextField(String tagname, int width, Element elt) throws DialogException
  {
    super(false,elt);
    m_tagname = tagname;
    m_real_maxlength = width;
    m_maxlength = m_size = Math.max(width,MINSIZE);

  } // end constructor

  protected CommonTextField(CommonTextField other)
  {
    super(other);
    m_tagname = other.m_tagname;
    m_size = other.m_size;
    m_maxlength = other.m_maxlength;
    m_real_maxlength = other.m_real_maxlength;

  } // end constructor

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

  protected boolean isNull(Object value)
  {
    return StringUtils.isEmpty((String)value);

  } // end isNull

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

  protected void renderField(TextRenderControl control, Map render_params)
      throws IOException, RenderingException
  {
    PrintWriter wr = control.getWriter();
    wr.write("<input type=\"" + m_tagname + "\" class=\"content\" name=\"" + getName() + "\" size=\"" + m_size
	     + "\" maxlength=\"" + m_maxlength + "\"");
    if (!isEnabled())
      wr.write(" disabled=\"disabled\"");
    wr.write(" value=\"");
    if (m_value!=null)
      wr.write(m_value);
    wr.write("\" />");

  } // end renderField

  protected void validateContents(Request r, Object data) throws ValidationException
  {
    if ((data!=null) && (data.toString().length()>m_real_maxlength))
    { // this value is too long
      ValidationException ve = new ValidationException(CommonTextField.class,"DialogMessages","text.tooLong");
      ve.setParameter(0,getCaption());
      ve.setParameter(1,String.valueOf(m_real_maxlength));
      throw ve;

    } // end if

  } // end validateContents

  public Object getValue()
  {
    return m_value;

  } // end getValue

  public boolean containsValue()
  {
    return StringUtils.isNotEmpty(m_value);

  } // end containsValue

  public void setValue(Object obj)
  {
    if (obj==null)
      m_value = null;
    else
      m_value = obj.toString();
    if ((m_value!=null) && StringUtils.isEmpty(m_value))
      m_value = null;

  } // end setValue

  public void setValueFrom(Request r)
  {
    RequestHelper rh = new RequestHelper(r);
    m_value = rh.getParameterString(getName());
    if ((m_value!=null) && StringUtils.isEmpty(m_value))
      m_value = null;

  } // end setValueFrom

  public void reset()
  {
    m_value = null;

  } // end reset

} // end class CommonTextField
