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

import java.util.Map;
import org.w3c.dom.*;
import com.silverwrist.venice.except.*;
import com.silverwrist.venice.ui.*;
import com.silverwrist.venice.ui.config.RootConfig;
import com.silverwrist.venice.util.XMLLoader;

class CommunityMenuEntry implements Comparable
{
  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  private String symbol;
  private int index;
  private int sequence;
  private LinkItem item;

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

  CommunityMenuEntry(Element svc, RootConfig conf) throws ConfigException
  {
    XMLLoader loader = XMLLoader.get();
    symbol = loader.configGetAttribute(svc,"id");
    index = loader.configGetAttributeInt(svc,"index");

    Element link_elt = loader.configGetSubSection(svc,"link");
    sequence = loader.configGetAttributeInt(link_elt,"sequence");
    item = new LinkItem(link_elt,conf);

  } // end constructor

  /*--------------------------------------------------------------------------------
   * Overrides from class Object
   *--------------------------------------------------------------------------------
   */

  public boolean equals(Object o)
  {
    if ((o==null) || !(o instanceof CommunityMenuEntry))
      return false;
    CommunityMenuEntry other = (CommunityMenuEntry)o;
    return (sequence==other.sequence);

  } // end equals

  public int hashCode()
  {
    return sequence;

  } // end hashCode

  /*--------------------------------------------------------------------------------
   * Implementations from interface Comparable
   *--------------------------------------------------------------------------------
   */

  public int compareTo(Object o)
  {
    if (o==null)
      throw new NullPointerException("compareTo null object!");
    CommunityMenuEntry other = (CommunityMenuEntry)o;  // may throw ClassCastException
    return sequence - other.sequence;

  } // end compareTo

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

  final String getSymbol()
  {
    return symbol;

  } // end getSymbol

  final int getIndex()
  {
    return index;

  } // end getIndex

  final int getSequence()
  {
    return sequence;

  } // end getSequence

  final LinkItem resolveItem(Map vars)
  {
    return new LinkItem(item,vars);

  } // end resolveItem

  final String getServletAddress(RequestOutput ro, Map vars)
  {
    return item.getAddress(ro,vars);

  } // end getServletAddress

  final String getServletAddress(RequestInput ri, Map vars)
  {
    return item.getAddress(ri,vars);

  } // end getServletAddress

} // end class CommunityMenuEntry
