package com.treelight.krnl;

/**
 * A container that allows a structure node to be
 * reused in another location. Adds a "displayed"
 * flag so we can tell if the node is already
 * displayed, and insert a "cycle" pointer, instead.
 *
 * Architectural Note:<br>
 * We exend StructureNode, only to override every method
 * it defines, and delegate them to a StructureNode we
 * contain. This design raises the downstream possiblity
 * that someone would add something to StructureNode and
 * forget to override and delegate it here. We therefore
 * need a StructureNexus interface, so we can implement
 * that, instead.
 *
 * @version 0.1
 * @author Eric Armstrong
 * @see ../NodesAndLists.html
 */
public class ContainedNode extends StructureNode 
implements Categorizable {
  static final String entityType = STRUCTURE_LINK;

  // The structure node this node encapsulates, and to which
  // all behaviors are delegated. Defined when the node is constructed.
  // ___TODO: Define a structure node interface, and implement that,
  //    instead of extending StructureNode. That will guarantee that future
  //    additions to structure node behaviors will be properly delegated,
  //    instead of inherited.
  private StructureNode structureNode;
  public String getStructureNode() { return structureNode; }

  // Used by the display system, to avoid cycles when expanding a node.
  // There is no versioning associated with this value, so there is a setter.
  private boolean displayed;
  public boolean getDisplayed() { return this.displayed; }
  public void setDisplayed(boolean displayed) { this.displayed = displayed; }

  /**
   * Add a list to the list of lists that
   * this node contains.
   */
  public void add(List list) 
  throws ListExistsException { 
    structureNode.add(list); 
  }

  /**
   * Return the named list from the list of lists that
   * this node contains.
   *
   * @exception ListNotFoundException if the named list does
   *            not exist.
   */
  public List get(String listType) 
  throws ListNotFoundException {
    return structureNode.get(listType);
  }

  /**
   * See if the named list exists in the list of
   * lists that this node contains.
   *
   * @exception ListNotFoundException if the named list does not exist.
   */
  public boolean hasList(String listType) {
    return structureNode.hasList(listType);
  }

  public void remove(String listType)
  throws ListNotFoundException { 
    structureNode.remove(listType);
  }

  /**
   * Return the node's content.
   */
  public String getContent() {
    return strucutreNode.getContent();
  }  

  /** 
   * Called by {@link NodeFactory} to
   * construct a node, specifying the kind of node it is.
   * (In XML, the specification would be the element name.)
   */
  protected ContainedNode(String structureNode) {
    this.structureNode = structureNode;
  }

}//ContainedNode
