package org.opengroupware.jope.appserver.elements;

import java.util.Map;

import org.opengroupware.jope.appserver.WOAssociation;
import org.opengroupware.jope.appserver.WOContext;
import org.opengroupware.jope.appserver.WOElement;
import org.opengroupware.jope.appserver.WOResponse;

/*
 * WOPasswordField
 * 
 * Create HTML form password fields. Remember that such are only secure over
 * secured connections (eg SSL).
 * 
 * Sample:
 *   Firstname: WOPasswordField {
 *     name  = "password";
 *     value = password;
 *   }
 * 
 * Renders:
 *   <input type="password" name="password" value="abc123" />
 * 
 * Bindings (WOInput):
 *   name     [in] - string
 *   value    [io] - object
 *   disabled [in] - boolean
 * Bindings:
 *   size     [in] - int
 */
public class WOPasswordField extends WOInput {
  
  protected WOAssociation size;

  public WOPasswordField(String _name, Map<String, WOAssociation> _assocs,
                         WOElement _template)
  {
    super(_name, _assocs, _template);
    
    this.size = grabAssociation(_assocs, "size");
  }

  /* responder */
  
  public void appendToResponse(WOResponse _r, WOContext _ctx) {
    if (_ctx.isRenderingDisabled())
      return;

    _r.appendBeginTag("input");
    _r.appendAttribute("type", "password");
    _r.appendAttribute("name", this.elementNameInContext(_ctx));
    
    if (this.value != null) {
      String s = this.value.stringValueInComponent(_ctx.cursor());
      if (s != null)
        _r.appendAttribute("value", s);
    }
    
    if (this.size != null) {
      int s;
      if ((s = this.size.intValueInComponent(_ctx.cursor())) > 0)
        _r.appendAttribute("size", s);
    }
    
    if (this.disabled != null) {
      if (this.disabled.booleanValueInComponent(_ctx.cursor()))
        _r.appendAttribute("disabled", "disabled");
    }
    
    this.appendExtraAttributesToResponse(_r, _ctx);
    // TODO: otherTagString
    
    _r.appendBeginTagClose();
  }
  
  /* description */
  
  public void appendAttributesToDescription(StringBuffer _d) {
    super.appendAttributesToDescription(_d);
    
    this.appendAssocToDescription(_d, "size", this.size);
  }  
}
