/*
 * 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) 2003 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
 * 
 * Contributor(s): 
 */
package com.silverwrist.venice.menu;

import java.util.*;

class MenuDefinition
{
  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  private int m_id;
  private String m_namespace;
  private String m_name;
  private String m_title;
  private String m_subtitle;
  private HashSet m_vars = new HashSet();
  private HashMap m_defaults = new HashMap();
  private ArrayList m_items = new ArrayList();
  private boolean m_deleted = false;

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

  MenuDefinition(int id, String namespace, String name, String title, String subtitle)
  {
    m_id = id;
    m_namespace = namespace;
    m_name = name;
    m_title = title;
    m_subtitle = subtitle;

  } // end constructor

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

  synchronized void addVariableDef(String name, String default_val)
  {
    m_vars.add(name);
    if (default_val!=null)
      m_defaults.put(name,default_val);

  } // end addVariableDef

  synchronized void removeVariableDef(String name)
  {
    m_vars.remove(name);
    m_defaults.remove(name);

  } // end removeVariableDef

  synchronized void setDefaultVal(String variable, String value)
  {
    if (value!=null)
      m_defaults.put(variable,value);
    else
      m_defaults.remove(variable);

  } // end setDefaultVal

  synchronized void addItemDef(MenuItemDefinition item)
  {
    m_items.add(item);

  } // end addItemDef

  synchronized void removeItemDef(MenuItemDefinition item)
  {
    m_items.remove(item);

  } // end removeItemDef

  Set getVars()
  {
    return Collections.unmodifiableSet(m_vars);

  } // end getVars

  Map getVarDefaults()
  {
    return Collections.unmodifiableMap(m_defaults);

  } // end getVarDefaults

  int getID()
  {
    return m_id;

  } // end getID

  String getNamespace()
  {
    return m_namespace;

  } // end getNamespace

  String getName()
  {
    return m_name;

  } // end getName

  String getTitle()
  {
    return m_title;

  } // end getTitle

  synchronized void setTitle(String title)
  {
    m_title = title;

  } // end setTitle

  String getSubtitle()
  {
    return m_subtitle;

  } // end getSubtitle

  synchronized void setSubtitle(String subtitle)
  {
    m_subtitle = subtitle;

  } // end setSubtitle

  boolean isVariable(String name)
  {
    return m_vars.contains(name);

  } // end isVariable

  int getItemCount()
  {
    return m_items.size();

  } // end getItemCount

  MenuItemDefinition getItem(int ndx)
  {
    return (MenuItemDefinition)(m_items.get(ndx));

  } // end getItem

  int getIndexOf(MenuItemDefinition item)
  {
    return m_items.indexOf(item);

  } // end getIndexOf

  int getItemContainingLinkText(String text)
  {
    for (int i=0; i<m_items.size(); i++)
    { // simple linear search through menu items
      MenuItemDefinition md = (MenuItemDefinition)(m_items.get(i));
      if ((md.getLink()!=null) && (md.getLink().indexOf(text)>=0))
	return i;

    } // end for

    return -1;

  } // end getItemContainingLinkText

  int getItemBySequence(int seq)
  {
    for (int i=0; i<m_items.size(); i++)
    { // simple linear search through menu items
      MenuItemDefinition md = (MenuItemDefinition)(m_items.get(i));
      int md_seq = md.getSequence();
      if (md_seq==seq)
	return i;
      else if (md_seq>seq)
	return -1;

    } // end for

    return -1;

  } // end if

  int getItemByTypeText(String type, String text)
  {
    for (int i=0; i<m_items.size(); i++)
    { // simple linear search through menu items
      MenuItemDefinition md = (MenuItemDefinition)(m_items.get(i));
      if ((md.getItemType().equals(type)) && (md.getText().equals(text)))
	return i;

    } // end for

    return -1;

  } // end getItemByTypeText

  synchronized void resequence()
  {
    if (m_items.size()>1)
      Collections.sort(m_items);

  } // end resequence

  boolean isDeleted()
  {
    return m_deleted;

  } // end isDeleted

  synchronized void delete()
  {
    m_deleted = true;
    Iterator it = m_items.iterator();
    while (it.hasNext())
    { // delete all the items as well
      MenuItemDefinition item = (MenuItemDefinition)(it.next());
      item.delete();

    } // end while

    m_items.clear();
    m_vars.clear();
    m_defaults.clear();

  } // end delete

} // end class MenuDefinition
