/*------------------------------------------------------------------------------------
Copyright (c)2000 TogetherSoft Corporation. Patents pending. All rights reserved. 
------------------------------------------------------------------------------------*/ 

package com.togethersoft.modules.xmleditor.xmltree;

import java.util.*;
import com.togethersoft.modules.xmleditor.dtd.*;

abstract public class InstHolder
{
  private DTDItemDef m_itemDef;

  /**
   *@link aggregation
   *     @associates <{com.togethersoft.modules.xmleditor.xmltree.Inst}>
   * @supplierCardinality 0..**/
  protected Vector m_instances = new Vector();

  //--------------------------------------------------------------------
  public InstHolder (DTDItemDef itemDef)
  {
    m_itemDef = itemDef;
  }

  //--------------------------------------------------------------------
  public abstract int     checkElementInstAllowance (DTDElementDef elementDef);
  public abstract boolean putElementInst (ElementInst elementInst);
  public abstract boolean deleteElementInst (ElementInst elementInst);
  public abstract Vector  getContainedElements();

  //--------------------------------------------------------------------
  public boolean isComplete()
  {
    // check completeness contained instances
    Enumeration instEnum = m_instances.elements();
    while (instEnum.hasMoreElements())
    {
      Inst inst = (Inst) instEnum.nextElement();
      if (! inst.isComplete())
        return false;
    }

    return true;
  }

  //--------------------------------------------------------------------
  public boolean isEmpty()
  {
    return m_instances.size() == 0;
  }

  //--------------------------------------------------------------------
  public static InstHolder newInstHolder (DTDItemDef itemDef)
  {
    return (itemDef instanceof DTDElementItemDef)
      ? (InstHolder) new ElementInstHolder ((DTDElementItemDef) itemDef)
      : (InstHolder) new GroupInstHolder ((DTDGroupItemDef) itemDef);
  }
}
