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

import java.io.IOException;
import org.w3c.dom.*;
import com.silverwrist.util.*;
import com.silverwrist.venice.ui.*;

public class ImageHandler implements RenderDirect
{
  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  private String url;               // URL of the image
  private boolean fixup;            // does the image need to be fixed up?
  private String alt;               // ALT text for the image
  private int width;                // width of the image
  private int height;               // height of the image

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

  public ImageHandler(Element tag, String def_url, String def_alt, int def_width, int def_height)
  {
    if (tag!=null)
    { // extract the details from the tag
      DOMElementHelper h = new DOMElementHelper(tag);
      url = h.getElementText();
      if (StringUtil.isStringEmpty(url))
      { // default the URL and fixup parameter
	url = def_url;
	fixup = true;

      } // end if
      else  // get the "fixup" parameter
	fixup = h.hasAttribute("fixup");

      alt = tag.getAttribute("alt");
      if (StringUtil.isStringEmpty(alt))
	alt = def_alt;

      Integer tmp = h.getAttributeInt("width");
      width = (tmp!=null) ? tmp.intValue() : def_width;
      tmp = h.getAttributeInt("height");
      height = (tmp!=null) ? tmp.intValue() : def_height;

    } // end if
    else
    { // just fill in all the defaults
      url = def_url;
      fixup = true;
      alt = def_alt;
      width = def_width;
      height = def_height;

    } // end else

  } // end constructor

  public ImageHandler(Element tag)
  {
    boolean defined = (tag!=null);
    DOMElementHelper h = null;
    if (defined)
    { // check the URL and see if it's defined
      h = new DOMElementHelper(tag);
      url = h.getElementText();
      defined = !(StringUtil.isStringEmpty(url));

    } // end if

    Integer tmp = null;
    if (defined)
    { // get the ALT text if it's there
      alt = tag.getAttribute("alt");
      if (StringUtil.isStringEmpty(alt))
	alt = "";

      // get the width
      tmp = h.getAttributeInt("width");
      defined = (tmp!=null);

    } // end if

    if (defined)
    { // stash the width, get the height
      width = tmp.intValue();
      tmp = h.getAttributeInt("height");
      defined = (tmp!=null);

    } // end if

    if (defined)
      height = tmp.intValue();
    else
    { // not defined - clear it out
      url = null;
      fixup = false;
      alt = null;
      width = -1;
      height = -1;

    } // end else

  } // end constructor

  /*--------------------------------------------------------------------------------
   * Implementations from interface RenderDirect
   *--------------------------------------------------------------------------------
   */

  public void render(RequestOutput out) throws IOException
  {
    if (url!=null)
      out.write(getRendering(out));

  } // end render

  /*--------------------------------------------------------------------------------
   * External operations
   *--------------------------------------------------------------------------------
   */

  public final boolean isDefined()
  {
    return (url!=null);

  } // end if

  public final int getWidth()
  {
    return width;

  } // end getWidth

  public final int getHeight()
  {
    return height;

  } // end getHeight

  public final String getRendering(RequestOutput out)
  {
    if (url==null)
      return "";

    synchronized (this)
    { // this may be called by more than one class, so protect this part
      if (fixup)
      { // fix up the host tag URL
	HTMLRendering html = (HTMLRendering)(out.queryService(HTMLRendering.class));
	url = html.getImagePath(url);
	fixup = false;

      } // end if

    } // end synchronized block

    StringBuffer buf = new StringBuffer("<IMG SRC=\"");
    buf.append(url).append("\" ALT=\"").append(alt).append("\" WIDTH=").append(width).append(" HEIGHT=");
    buf.append(height).append(" BORDER=0>");
    return buf.toString();

  } // end getRendering

} // end class ImageHandler
