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

class MenuEditItemImpl implements MenuEditItemObject
{
  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  private MenuDatabaseOps m_ops;
  private MenuDefinition m_mdef;
  private MenuItemDefinition m_item;

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

  MenuEditItemImpl(MenuDatabaseOps ops, MenuDefinition mdef, MenuItemDefinition item)
  {
    m_ops = ops;
    m_mdef = mdef;
    m_item = item;

  } // end constructor

  /*--------------------------------------------------------------------------------
   * Internal operations
   *--------------------------------------------------------------------------------
   */

  private final void baleeted() throws DatabaseException
  {
    if ((m_ops==null) || (m_mdef==null) || (m_item==null))
      throw new DatabaseException(MenuEditItemImpl.class,"MenuMessages","item.deleted");
    if (m_item.isDeleted() || m_mdef.isDeleted())
    { // isolate the object and throw the exception
      m_ops = null;
      m_mdef = null;
      m_item = null;
      throw new DatabaseException(MenuEditItemImpl.class,"MenuMessages","item.deleted");
      
    } // end if

  } // end baleeted

  /*--------------------------------------------------------------------------------
   * Implementations from interface MenuEditItemObject
   *--------------------------------------------------------------------------------
   */

  public String getItemType()
  {
    return (m_item==null) ? null : m_item.getItemType();

  } // end getItemType

  public int getIndexOfItem()
  {
    if ((m_item==null) || (m_mdef==null))
      return -1;
    int rc = m_mdef.getIndexOf(m_item);
    assert (m_mdef.getItem(rc)==m_item);
    return rc;

  } // end getIndexOfItem

  public int getSequence()
  {
    return (m_item==null) ? -1 : m_item.getSequence();

  } // end getSequence

  public void setSequence(int seq) throws DatabaseException
  {
    baleeted();
    if (m_item.getSequence()==seq)
      return;
    m_ops.setItemSequence(m_mdef.getID(),m_item.getSequence(),seq);
    m_item.setSequence(seq);
    m_mdef.resequence();

  } // end setSequence

  public boolean isEnabled()
  {
    return (m_item==null) ? false : m_item.isEnabled();

  } // end isEnabled

  public void setEnabled(boolean flag) throws DatabaseException
  {
    baleeted();
    if (m_item.isEnabled()==flag)
      return;
    m_ops.setItemEnable(m_mdef.getID(),m_item.getSequence(),flag);
    m_item.setEnabled(flag);

  } // end setEnabled

  public int getIndent()
  {
    return (m_item==null) ? 0 : m_item.getIndentLevel();

  } // end getIndent

  public void setIndent(int indent) throws DatabaseException
  {
    baleeted();
    if (indent<0)
      throw new IllegalArgumentException("indent must be >= 0");
    if (indent==m_item.getIndentLevel())
      return;
    m_ops.setItemIndent(m_mdef.getID(),m_item.getSequence(),indent);
    m_item.setIndentLevel(indent);

  } // end setIndent

  public String getText()
  {
    return (m_item==null) ? null : m_item.getText();

  } // end getText

  public void setText(String text) throws DatabaseException
  {
    baleeted();
    m_ops.setItemText(m_mdef.getID(),m_item.getSequence(),text);
    m_item.setText(text);

  } // end setText

  public String getLinkType()
  {
    return (m_item==null) ? null : m_item.getLinkType();

  } // end getLinkType

  public String getLink()
  {
    return (m_item==null) ? null : m_item.getLink();

  } // end getLink

  public void setLink(String type, String link) throws DatabaseException
  {
    baleeted();
    m_ops.setItemLink(m_mdef.getID(),m_item.getSequence(),type,link);
    m_item.setLink(type,link);

  } // end setLink

  public String getTarget()
  {
    return (m_item==null) ? null : m_item.getTarget();

  } // end getTarget

  public void setTarget(String target) throws DatabaseException
  {
    baleeted();
    m_ops.setItemTarget(m_mdef.getID(),m_item.getSequence(),target);
    m_item.setTarget(target);

  } // end setTarget

  public String getTitle()
  {
    return (m_item==null) ? null : m_item.getTitle();

  } // end getTitle

  public void setTitle(String title) throws DatabaseException
  {
    baleeted();
    m_ops.setItemTitle(m_mdef.getID(),m_item.getSequence(),title);
    m_item.setTitle(title);

  } // end setTitle

  public String getOnClick()
  {
    return (m_item==null) ? null : m_item.getOnClick();

  } // end getOnClick

  public void setOnClick(String on_click) throws DatabaseException
  {
    baleeted();
    m_ops.setItemOnClick(m_mdef.getID(),m_item.getSequence(),on_click);
    m_item.setOnClick(on_click);

  } // end setOnClick

  public QualifiedNameKey getPermission()
  {
    return (m_item==null) ? null : m_item.getPermission();

  } // end getPermission

  public void setPermission(String namespace, String name) throws DatabaseException
  {
    baleeted();
    m_ops.setItemPermission(m_mdef.getID(),m_item.getSequence(),namespace,name);
    m_item.setPermission(namespace,name);

  } // end setPermission

  public String getIfDefVar()
  {
    return (m_item==null) ? null : m_item.getIfDefVar();

  } // end getIfDefVar

  public void setIfDefVar(String var) throws DatabaseException
  {
    baleeted();
    m_ops.setItemIfDef(m_mdef.getID(),m_item.getSequence(),var);
    m_item.setIfDefVar(var);

  } // end setIfDefVar

  public String getIfNotDefVar()
  {
    return (m_item==null) ? null : m_item.getIfNotDefVar();

  } // end getIfNotDefVar

  public void setIfNotDefVar(String var) throws DatabaseException
  {
    baleeted();
    m_ops.setItemIfNotDef(m_mdef.getID(),m_item.getSequence(),var);
    m_item.setIfNotDefVar(var);

  } // end setIfNotDefVar

  public void delete() throws DatabaseException
  {
    baleeted();
    m_ops.deleteItem(m_mdef.getID(),m_item.getSequence());
    m_mdef.removeItemDef(m_item);
    m_item.delete();
    m_ops = null;
    m_mdef = null;
    m_item = null;

  } // end delete

} // end class MenuEditItemImpl
