/*
 * 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.*;
import com.silverwrist.venice.except.*;
import com.silverwrist.venice.ui.*;

public class ColorPalette implements ColorSelectors
{
  /*--------------------------------------------------------------------------------
   * Static data members
   *--------------------------------------------------------------------------------
   */

  private static final Map config_map;  // mapping from configuration names to color indexes
  private static final Map streetname_map;  // mapping from "street names" to color indexes

  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  private String[] colors_array;      // the actual colors

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

  ColorPalette(Element colors) throws ConfigException
  {
    colors_array = new String[config_map.size()];
    int i;
    NodeList nl = colors.getChildNodes();
    for (i=0; i<nl.getLength(); i++)
    { // look at all subelements and map their names to color selectors
      Node n = nl.item(i);
      if (n.getNodeType()==Node.ELEMENT_NODE)
      { // map the name of the node to a color selector
	Integer cs = (Integer)(config_map.get(n.getNodeName()));
	if (cs!=null)
	{ // get the color value and load it into the array
	  DOMElementHelper h = new DOMElementHelper((Element)n);
	  colors_array[cs.intValue()] = h.getElementText();

	} // end if

      } // end if

    } // end for

  } // end constructor

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

  public final String getColor(int selector)
  {
    if ((selector<0) || (selector>=colors_array.length))
      throw new IndexOutOfBoundsException("invalid color selector");
    return colors_array[selector];

  } // end getColor

  public final String getColor(String name)
  {
    Integer sel = (Integer)(streetname_map.get(name));
    if (sel==null)
      return name;
    else
      return colors_array[sel.intValue()];

  } // end getColor

  public final void fillParameterMap(String prefix, Map the_map)
  {
    Iterator it = streetname_map.entrySet().iterator();
    while (it.hasNext())
    { // read each entry, translate it, and add it to the new map
      Map.Entry ntry = (Map.Entry)(it.next());
      String new_name = prefix + ntry.getKey().toString();
      Integer sel = (Integer)(ntry.getValue());
      the_map.put(new_name,colors_array[sel.intValue()]);

    } // end while

  } // end fillParameterMap

  /*--------------------------------------------------------------------------------
   * Static initializer
   *--------------------------------------------------------------------------------
   */

  static
  { // Initialize the mapping of color names from render-config.xml to color selector IDs,
    // and the mapping from "street names" for the colors to the color selector IDs.
    HashMap m1 = new HashMap();
    HashMap m2 = new HashMap();
    Integer x;

    x = new Integer(FRAME_BACKGROUND);
    m1.put("frame-bg",x);
    m2.put("frame",x);
    x = new Integer(TITLE_BACKGROUND);
    m1.put("title-bg",x);
    m2.put("top.background",x);
    m2.put("top.bg",x);
    x = new Integer(TITLE_FOREGROUND);
    m1.put("title-fg",x);
    m2.put("top.foreground",x);
    m2.put("top.fg",x);
    x = new Integer(TITLE_LINK);
    m1.put("title-link",x);
    m2.put("top.link",x);
    x = new Integer(LEFT_BACKGROUND);
    m1.put("left-bg",x);
    m2.put("left.background",x);
    m2.put("left.bg",x);
    x = new Integer(LEFT_FOREGROUND);
    m1.put("left-fg",x);
    m2.put("left.foreground",x);
    m2.put("left.fg",x);
    x = new Integer(LEFT_LINK);
    m1.put("left-link",x);
    m2.put("left.link",x);
    x = new Integer(CONTENT_BACKGROUND);
    m1.put("content-bg",x);
    m2.put("content.background",x);
    m2.put("content.bg",x);
    x = new Integer(CONTENT_FOREGROUND);
    m1.put("content-fg",x);
    m2.put("content.foreground",x);
    m2.put("content.fg",x);
    x = new Integer(CONTENT_HEADER);
    m1.put("content-hdr",x);
    m2.put("content.header",x);
    x = new Integer(CONTENT_DISABLED);
    m1.put("content-disabled",x);
    m2.put("disabled",x);
    x = new Integer(CONTENT_ERROR);
    m1.put("content-error",x);
    m2.put("error",x);
    x = new Integer(SIDEBOX_TITLE_BACKGROUND);
    m1.put("sidebox-title-bg",x);
    m2.put("sidebox.top.background",x);
    m2.put("sidebox.top.bg",x);
    x = new Integer(SIDEBOX_TITLE_FOREGROUND);
    m1.put("sidebox-title-fg",x);
    m2.put("sidebox.top.foreground",x);
    m2.put("sidebox.top.fg",x);
    x = new Integer(SIDEBOX_CONTENT_BACKGROUND);
    m1.put("sidebox-content-bg",x);
    m2.put("sidebox.background",x);
    m2.put("sidebox.bg",x);
    x = new Integer(SIDEBOX_CONTENT_FOREGROUND);
    m1.put("sidebox-content-fg",x);
    m2.put("sidebox.foreground",x);
    m2.put("sidebox.fg",x);
    x = new Integer(SIDEBOX_CONTENT_LINK);
    m1.put("sidebox-content-link",x);
    m2.put("sidebox.link",x);
    x = new Integer(CONFIRM_TITLE_BACKGROUND);
    m1.put("confirm-title-bg",x);
    m2.put("dlg.confirm.title.background",x);
    m2.put("dlg.confirm.title.bg",x);
    x = new Integer(CONFIRM_TITLE_FOREGROUND);
    m1.put("confirm-title-fg",x);
    m2.put("dlg.confirm.title.foreground",x);
    m2.put("dlg.confirm.title.fg",x);
    x = new Integer(ERROR_TITLE_BACKGROUND);
    m1.put("error-title-bg",x);
    m2.put("dlg.error.title.background",x);
    m2.put("dlg.error.title.bg",x);
    x = new Integer(ERROR_TITLE_FOREGROUND);
    m1.put("error-title-fg",x);
    m2.put("dlg.error.title.foreground",x);
    m2.put("dlg.error.title.fg",x);

    config_map = Collections.unmodifiableMap(m1);
    streetname_map = Collections.unmodifiableMap(m2);
    
  } // end static initializer

} // end class ColorPalette
