/*
 * 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@ricochet.com>,
 * for Silverwrist Design Studios.  Portions created by Eric J. Bowersox are
 * Copyright (C) 2001-2004 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 m_src;                  // image source URL
  private String m_alt = null;           // image alternate text
  private String m_name = null;          // image NAME= attribute
  private int m_width;                   // width of image in pixels
  private int m_height;                  // height of image in pixels
  private boolean m_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
    m_src = loader.configGetAttribute(elt,"src");
    m_width = loader.configGetAttributeInt(elt,"width");
    m_height = loader.configGetAttributeInt(elt,"height");

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

  } // end constructor

  /*--------------------------------------------------------------------------------
   * Internal operations
   *--------------------------------------------------------------------------------
   */

  private final String getImageTag(RequestOutput out)
  {
    StringBuffer buf = new StringBuffer("<img src=\"");
    if (m_fixup)
    { // fix up the image path
      HTMLRendering html = (HTMLRendering)(out.queryService(HTMLRendering.class));
      m_src = html.getImagePath(m_src);
      m_fixup = false;

    } // end if

    buf.append(m_src).append("\" width=\"").append(m_width).append("\" height=\"").append(m_height).append("\"");
    if (m_alt!=null)
      buf.append(" alt=\"").append(m_alt).append("\" title=\"").append(m_alt).append("\"");
    if (m_name!=null)
      buf.append(" name=\"").append(m_name).append("\"");
    buf.append(" border=\"0\" />");
    return buf.toString();

  } // end getImageTag

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

  public void render(RequestOutput out, Writer wr, MenuTemplate template, Set defines) throws IOException
  {
    if (wr==null)
      wr = out.getWriter();
    template.renderItem(wr,"image",getImageTag(out),null);

  } // end render

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

  } // end render

  public String getTitle(RequestOutput out)
  {
    return null;

  } // end getTitle

} // end class ImageItem
