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

package com.togethersoft.modules.xmleditor.xmlcomponent;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;
import com.togethersoft.openapi.ide.window.*;
import com.togethersoft.modules.xmleditor.dtd.*;
import com.togethersoft.modules.xmleditor.xmltree.*;


public class ElementButtonPane extends JScrollPane implements ActionListener, ChangeListener
{
  /**
   * @supplierCardinality 1 
   */
  private NodeManager m_nodeManager;

  private ElementButton[] m_buttons;

  private JViewport m_viewport;

  //--------------------------------------------------------------------
  public ElementButtonPane (NodeManager nodeManager)
  {
    super (JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    m_nodeManager = nodeManager;

    DTDElementDef elementDef = nodeManager.getElementInst().getElementDef();
    Vector childDefs = elementDef.getChildElementDefs();

    if (elementDef.getElementType() == DTDElementDef.ET_ANY)
      Collections.sort (childDefs);

    m_buttons = new ElementButton [childDefs.size()];

    JPanel buttonPanel = new JPanel();

    if (m_buttons.length > 0)
    {
      buttonPanel.setLayout (new GridLayout (m_buttons.length, 1, 0, 2));
      buttonPanel.setBorder (BorderFactory.createEmptyBorder (6,8,6,8));

      IdeDialogButtonGroup buttonGroup =
         IdeWindowManagerAccess.getWindowManager().createButtonGroup();

      int i = 0;
      Enumeration childDefEnum = childDefs.elements();
      while (childDefEnum.hasMoreElements())
      {
        DTDElementDef childDef = (DTDElementDef) childDefEnum.nextElement();
        ElementButton button = new ElementButton (childDef);
        buttonGroup.add (button);
        button.addActionListener (this);
        m_buttons[i ++] = button;
        buttonPanel.add (button);
      }
    }

    JPanel panel = new JPanel (new BorderLayout());
    panel.add (buttonPanel, BorderLayout.NORTH);

    setViewportView (panel);
    m_viewport = getViewport();
    m_viewport.addChangeListener (this);

    updateButtonStates();
  }

  //--------------------------------------------------------------------
  public void stateChanged (ChangeEvent e)
  {
    if (e.getSource() == m_viewport)
    {
      int newWidth = m_viewport.getExtentSize().width;
      Dimension viewSize = m_viewport.getViewSize();
      if (viewSize.width != newWidth)
      {
        viewSize.width = newWidth;
        m_viewport.setViewSize (viewSize);

	      revalidate();
	      repaint();
      }
    }
  }

  //--------------------------------------------------------------------
  public void actionPerformed (ActionEvent event)
  {
    Object caller = event.getSource();

    if (caller instanceof ElementButton)
    {
      m_nodeManager.newChildElementInst (((ElementButton) caller).getElementDef().getTagName());
      return;
    }
  }

  //--------------------------------------------------------------------
  public void updateButtonStates()
  {
    ElementInst parentElement = m_nodeManager.getElementInst();
    for (int i = 0; i < m_buttons.length; i ++)
    {
      ElementButton button = m_buttons [i];
      int allowance = parentElement.checkNewChildElementAllowance (button.getElementDef());
      button.setButtonState (allowance);
    }
  }

  //--------------------------------------------------------------------
}
