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

package com.togethersoft.modules.xmleditor.xmlcomponent;

import javax.swing.tree.*;
import java.util.*;
import com.togethersoft.modules.xmleditor.dtd.*;
import com.togethersoft.modules.xmleditor.xmltree.*;


public class NodeManager
{
  /**
   * @supplierCardinality 1
   */
  private XMLEditorComponent  m_xmlEditor;

  private ElementInst  m_elementInst;
  private DefaultMutableTreeNode m_treeNode = null;

  /**
   * @supplierCardinality 1
   */
  private InspectorPane m_inspectorPane = null;

  /**
   * @supplierCardinality 1
   */
  private ElementButtonPane  m_buttonPane = null;

  public double dividerProportionalLocation = -1;

  //--------------------------------------------------------------------
  public NodeManager (XMLEditorComponent xmlEditor, ElementInst elementInst)
  {
    m_xmlEditor = xmlEditor;
    m_elementInst = elementInst;
  }

  //--------------------------------------------------------------------
  public void setTreeNode (DefaultMutableTreeNode treeNode)
  {
    m_treeNode = treeNode;
  }

  //--------------------------------------------------------------------
  public ElementInst getElementInst()
  {
    return m_elementInst;
  }

  //--------------------------------------------------------------------
  public String toString()
  {
	  return m_elementInst.getDisplayName();
  }

  //--------------------------------------------------------------------
  public InspectorPane getInspectorPane()
  {
    if (m_inspectorPane == null)
      m_inspectorPane = new InspectorPane (this);

    return m_inspectorPane;
  }

  //--------------------------------------------------------------------
  public void stopEditing()
  {
    if (m_inspectorPane != null)
      m_inspectorPane.stopEditing();
  }

  //--------------------------------------------------------------------
  public ElementButtonPane getButtonPane()
  {
    if (m_buttonPane == null)
      m_buttonPane = new ElementButtonPane (this);

    return m_buttonPane;
  }

  //--------------------------------------------------------------------
  public void newChildElementInst (String tagName)
  {
    try
    {
      ElementInst childElementInst = m_elementInst.getXMLTreeManager().createElementInst (tagName, m_elementInst);
      if (! m_elementInst.putChildElementInst (childElementInst))
        return;

      m_buttonPane.updateButtonStates();

      int childIndex = m_elementInst.getChildIndex (childElementInst);
      m_xmlEditor.insertChildNode (m_treeNode, childElementInst, childIndex);
    }
    catch (UnknownXMLElementException e) { }
  }

  //--------------------------------------------------------------------
  public void deleteChildElementInst (ElementInst childElementInst)
  {
    m_elementInst.deleteChildElementInst (childElementInst);
    m_buttonPane.updateButtonStates();
  }
}
