/*
 * 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-02 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
 * 
 * Contributor(s): 
 */
package com.silverwrist.venice.ui.menus;

import java.io.IOException;
import java.io.Writer;
import java.util.Set;
import org.w3c.dom.*;
import com.silverwrist.util.DOMElementHelper;
import com.silverwrist.venice.except.ConfigException;
import com.silverwrist.venice.ui.RequestOutput;
import com.silverwrist.venice.ui.helpers.HTMLRendering;
import com.silverwrist.venice.util.XMLLoader;

class ImageItem implements MenuComponent
{
  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  private String src;                    // image source URL
  private String alt = null;             // image alternate text
  private String name = null;            // image NAME= attribute
  private int width;                     // width of image in pixels
  private int height;                    // height of image in pixels
  private boolean fixup;                 // does image source URL need fixing up?

  /*--------------------------------------------------------------------------------
   * Constructor
   *--------------------------------------------------------------------------------
   */

  ImageItem(Element elt) throws ConfigException
  {
    XMLLoader loader = XMLLoader.get();

    loader.configVerifyNodeName(elt,"image");

    // get image source & dimensions
    src = loader.configGetAttribute(elt,"src");
    width = loader.configGetAttributeInt(elt,"width");
    height = loader.configGetAttributeInt(elt,"height");

    // get the rest of the attributes
    DOMElementHelper h = new DOMElementHelper(elt);
    if (h.hasAttribute("alt"))
      alt = elt.getAttribute("alt");
    if (h.hasAttribute("name"))
      name = elt.getAttribute("name");
    if (h.hasAttribute("fixup"))
      fixup = true;

  } // end constructor

  /*--------------------------------------------------------------------------------
   * Implementations from interface MenuComponent
   *--------------------------------------------------------------------------------
   */

  public void render(RequestOutput out, Writer wr) throws IOException
  {
    this.render(out,wr,java.util.Collections.EMPTY_SET);

  } // end render

  public void render(RequestOutput out, Writer wr, Set defines) throws IOException
  {
    if (wr==null)
      wr = out.getWriter();
    wr.write("<IMG SRC=\"");
    if (fixup)
    { // fix up the image path
      HTMLRendering html = (HTMLRendering)(out.queryService(HTMLRendering.class));
      src = html.getImagePath(src);
      fixup = false;

    } // end if

    wr.write(src + "\" WIDTH=" + width + " HEIGHT=" + height);
    if (alt!=null)
      wr.write(" ALT=\"" + alt + "\"");
    if (name!=null)
      wr.write(" NAME=\"" + name + "\"");
    wr.write(" BORDER=0>");

  } // end render

  public void renderOnLeft(RequestOutput out, Writer wr) throws IOException
  {
    this.render(out,wr,java.util.Collections.EMPTY_SET);

  } // end renderOnLeft

  public void renderOnLeft(RequestOutput out, Writer wr, Set defines) throws IOException
  {
    this.render(out,wr,defines);

  } // end renderOnLeft

  public String getTitle(RequestOutput out)
  {
    return null;

  } // end getTitle

} // end class ImageItem
