/*
  Copyright (C) 2006 Helge Hess

  This file is part of JOPE.

  JOPE is free software; you can redistribute it and/or modify it under
  the terms of the GNU Lesser General Public License as published by the
  Free Software Foundation; either version 2, or (at your option) any
  later version.

  JOPE is distributed in the hope that it will be useful, but WITHOUT ANY
  WARRANTY; without even the implied warranty of MERCHANTABILITY or
  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
  License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with JOPE; see the file COPYING.  If not, write to the
  Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
  02111-1307, USA.
*/

package org.opengroupware.jope.appserver.elements;

import java.util.Map;

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

/*
 * WOImage
 * 
 * This element renders an <img> tag, possibly pointing to dynamically
 * generated URLs.
 * 
 * Sample:
 *   Banner: WOImage {
 *     src    = "/images/banner.gif";
 *     border = 0;
 *   }
 * 
 * Renders:
 *   <img src="/images/banner.gif" border="0" />
 * 
 * Bindings (WOLinkGenerator for image resource):
 *   src              [in] - string
 *   filename         [in] - string
 *   framework        [in] - string
 *   actionClass      [in] - string
 *   directActionName [in] - string
 *   queryDictionary  [in] - Map<String,String>
 *   ?wosid           [in] - boolean (constant!)
 *   - all bindings starting with a ? are stored as query parameters.
 */
public class WOImage extends WOHTMLDynamicElement {
  
  protected WOLinkGenerator link = null;

  public WOImage(String _name, Map<String, WOAssociation> _assocs,
                 WOElement _template)
  {
    super(_name, _assocs, _template);
    
    this.link = WOLinkGenerator
      .rsrcLinkGeneratorForAssociations("src", _assocs);
  }
  
  /* responder */

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

    _r.appendBeginTag("img");
    
    if (this.link != null)
      _r.appendAttribute("src", this.link.fullHrefInContext(_ctx));
    
    this.appendExtraAttributesToResponse(_r, _ctx);
    // TODO: otherTagString
    
    _r.appendBeginTagClose();
  }
  
  /* description */
  
  public void appendAttributesToDescription(StringBuffer _d) {
    super.appendAttributesToDescription(_d);
    
    if (this.link != null) {
      _d.append(" src=");
      _d.append(this.link.toString());
    }
  }  
}
