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

import java.util.*;
import org.w3c.dom.*;
import com.silverwrist.util.DOMElementHelper;
import com.silverwrist.util.StringUtil;
import com.silverwrist.venice.except.*;
import com.silverwrist.venice.util.XMLLoader;

class ButtonHolder
{
  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  private int width;
  private int height;
  private Map id_to_caption;
  private Map id_to_image;
  private String tail_tag;

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

  ButtonHolder(Element cfg, String image_path) throws ConfigException
  {
    // get the initial header information
    XMLLoader loader = XMLLoader.get();
    loader.configVerifyNodeName(cfg,"buttons");
    width = loader.configGetAttributeInt(cfg,"width");
    height = loader.configGetAttributeInt(cfg,"height");
    tail_tag = "\" WIDTH=" + width + " HEIGHT=" + height + " BORDER=0>";

    // load the individual button descriptors
    HashMap tmp_caption = new HashMap();
    HashMap tmp_image = new HashMap();
    NodeList nl = cfg.getChildNodes();
    for (int i=0; i<nl.getLength(); i++)
    { // find all element nodes
      Node n = nl.item(i);
      if (n.getNodeType()==Node.ELEMENT_NODE)
      { // load up this button node
	loader.configVerifyNodeName(n,"button");
	Element be = (Element)n;
	String id = loader.configGetAttribute(be,"id");
	String capt = be.getAttribute("caption");
	DOMElementHelper h = new DOMElementHelper(be);
	String img = loader.configGetText(h);
	if (h.hasAttribute("fixup"))
	  img = image_path + img;
	if (!(StringUtil.isStringEmpty(capt)))
	  tmp_caption.put(id,capt);
	tmp_image.put(id,img);

      } // end if
      // else just ignore it

    } // end for

    if (tmp_caption.isEmpty())
      id_to_caption = Collections.EMPTY_MAP;
    else
      id_to_caption = Collections.unmodifiableMap(tmp_caption);
    if (tmp_image.isEmpty())
      id_to_image = Collections.EMPTY_MAP;
    else
      id_to_image = Collections.unmodifiableMap(tmp_image);

  } // end constructor

  final String getButtonVisual(String id)
  {
    // Look up the caption and image.
    String img = (String)(id_to_image.get(id));
    if (img==null)
      return "";
    String caption = (String)(id_to_caption.get(id));

    // Build the returned tag.
    StringBuffer buf = new StringBuffer("<IMG SRC=\"");
    buf.append(img).append("\" ALT=\"");
    if (caption!=null)  // add square brackets, like Links does around <INPUT TYPE=IMAGE>
      buf.append("[ ").append(caption).append(" ]");
    buf.append(tail_tag);
    return buf.toString();

  } // end getButtonVisual

  final String getButtonInput(String id)
  {
    // Look up the caption and image.
    String img = (String)(id_to_image.get(id));
    if (img==null)
      return "";
    String caption = (String)(id_to_caption.get(id));

    // Build the returned tag.
    StringBuffer buf = new StringBuffer("<INPUT TYPE=IMAGE SRC=\"");
    buf.append(img).append("\" NAME=\"").append(id).append("\" ALT=\"");
    if (caption!=null)
      buf.append(caption);
    buf.append(tail_tag);
    return buf.toString();

  } // end getButtonInput

} // end class ButtonHolder
