/*
 * 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.servlets.format.menus;

import java.io.Writer;
import java.io.IOException;
import org.apache.log4j.*;
import org.w3c.dom.*;
import com.silverwrist.util.*;
import com.silverwrist.venice.except.ConfigException;
import com.silverwrist.venice.servlets.format.ColorSelectors;
import com.silverwrist.venice.servlets.format.ComponentRender;
import com.silverwrist.venice.servlets.format.RenderData;

class LinkItem implements ComponentRender, ColorSelectors
{
  /*--------------------------------------------------------------------------------
   * Static data members
   *--------------------------------------------------------------------------------
   */

  private static final String ABSOLUTE = "absolute";
  private static final String SERVLET = "servlet";
  private static final String FRAME = "frame";
  private static final String[] valid_types = { ABSOLUTE, SERVLET, FRAME };

  private static Category logger = Category.getInstance(LinkItem.class);

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

  private String href;
  private String type;
  private boolean enabled = true;
  private String target = null;
  private ComponentRender contents;

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

  LinkItem(Element elt) throws ConfigException
  {
    if (!(elt.getNodeName().equals("link")))
    { // just some shorts-checking here to make sure the element is OK
      logger.fatal("huh?!? this should have been a <link/> if it got here!");
      throw new ConfigException("not a <link/> element");

    } // end if

    DOMElementHelper h = new DOMElementHelper(elt);
    href = elt.getAttribute("href");
    if (h.hasAttribute("type"))
    { // get the "type" and check it
      type = elt.getAttribute("type").trim().toLowerCase();
      boolean err = true;
      for (int i=0; err && (i<valid_types.length); i++)
	if (type.equals(valid_types[i]))
	  err = false;  // found it!
      if (err)
      { // type value is not valid
	logger.fatal("type=\"" + type + "\" is not a valid value");
	throw new ConfigException("invalid type= attribute of <link/>");

      } // end if

    } // end if
    else  // default to "absolute"
      type = ABSOLUTE;

    // load the "target"
    if (h.hasAttribute("target"))
      target = elt.getAttribute("target");

    // load the "disabled" attribute
    if (h.hasAttribute("disabled"))
      enabled = false;

    // load the contents
    Element x = h.getSubElement("image");
    if (x!=null)
      contents = new ImageItem(x);
    else
      contents = new TextItem(h.getElementText());

  } // end constructor

  /*--------------------------------------------------------------------------------
   * Implementations from interface ComponentRender
   *--------------------------------------------------------------------------------
   */

  public void renderHere(Writer out, RenderData rdat) throws IOException
  {
    if (enabled)
    { // write the opening <A> and <FONT> tags
      out.write("<A HREF=\"");
      if (type.equals(ABSOLUTE))
	out.write(href);
      else if (type.equals(SERVLET))
	out.write(rdat.getEncodedServletPath(href));
      else if (type.equals(FRAME))
	out.write(rdat.getEncodedServletPath("frame/" + href));
      out.write("\" CLASS=\"lbar\"");
      if (target!=null)
	out.write(" TARGET=\"" + target + "\"");
      out.write("><FONT COLOR=\"" + rdat.getStdColor(LEFT_LINK) + "\">");
    } // end if
    else  // write the "disabled" indicator
      out.write("<FONT COLOR=\"" + rdat.getStdColor(CONTENT_DISABLED) + "\">");
    contents.renderHere(out,rdat);
    out.write("</FONT>");
    if (enabled)
      out.write("</A>");
    out.write("<BR>\n");

  } // end renderHere

} // end class LinkItem
