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

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

  public static final int DEFAULT_ROWS = 4;
  public static final int DEFAULT_COLS = 40;
  public static final int DEFAULT_MAXLENGTH = -1;
  public static final String DEFAULT_WRAP = "soft";

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

  private String m_value = null;
  private int m_rows;
  private int m_cols;
  private int m_maxlength;
  private String m_wraptype;

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

  public MultiLineTextField(Element elt) throws DialogException
  {
    super(false,elt);
    XMLLoader loader = XMLLoader.get();
    try
    { // load the attributes specific to a multiline field
      m_rows = loader.getAttributeInt(elt,"rows",DEFAULT_ROWS);
      m_cols = loader.getAttributeInt(elt,"cols",DEFAULT_COLS);
      m_maxlength = loader.getAttributeInt(elt,"maxlength",DEFAULT_MAXLENGTH);
      m_wraptype = loader.getAttribute(elt,"wrap",DEFAULT_WRAP);

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

    } // end catch
    
  } // end constructor

  protected MultiLineTextField(MultiLineTextField other)
  {
    super(other);
    m_rows = other.m_rows;
    m_cols = other.m_cols;
    m_maxlength = other.m_maxlength;
    m_wraptype = other.m_wraptype;

  } // 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("<textarea class=\"content\" name=\"" + getName() + "\" rows=\"" + m_rows + "\" cols=\"" + m_cols
             + "\" wrap = \"" + m_wraptype + "\"");
    if (!isEnabled())
      wr.write(" disabled=\"disabled\"");
    wr.write(">");
    if (m_value!=null)
      wr.write(StringUtils.encodeHTML(m_value));
    wr.write("</textarea>");

  } // end renderField

  protected void validateContents(Request r, Object data) throws ValidationException
  {
    if ((data!=null) && (m_maxlength>0) && (data.toString().length()>m_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_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

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

  } // end clone

} // end class MultiLineTextField
