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

package com.togethersoft.modules.xmleditor.xmlcomponent;

import java.util.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;

import com.togethersoft.modules.xmleditor.dtd.*;
import com.togethersoft.modules.xmleditor.xmltree.*;

import com.togethersoft.openapi.ide.inspector.IdeInspectorProperty;
import com.togethersoft.openapi.ide.inspector.IdeInspectorPropertySetComponent;
import com.togethersoft.openapi.ide.inspector.util.table.PropertyTableComponentUI;
import com.togethersoft.openapi.ide.util.UIComponentService;

import java.beans.PropertyEditor;
import com.togethersoft.openapi.ide.inspector.util.editors.*;

public class InspectorPane extends JPanel
{
  /**
   * @supplierCardinality 1
   */
  private NodeManager  m_nodeManager;
  private ElementInst  m_elementInst;

  private PropertyTableComponentUI m_valueInspectorComp = null;
  private PropertyTableComponentUI m_attrInspectorComp = null;

  //--------------------------------------------------------------------
  public InspectorPane (NodeManager nodeManager)
  {
    m_nodeManager = nodeManager;
    m_elementInst = m_nodeManager.getElementInst();

    setLayout (new BorderLayout());

    createAttrInspectorComp();

    JLabel attrTitle = new JLabel ("Element Attributes", SwingConstants.CENTER);

    Font titleFont = attrTitle.getFont().deriveFont (Font.BOLD);
    Color titleColor = Color.blue.darker().darker();
    attrTitle.setFont (titleFont);
    attrTitle.setForeground (titleColor);

    if (m_elementInst.getElementDef().getElementType() == DTDElementDef.ET_PCDATA)
    {
      JLabel valueEditorTitle = new JLabel ("Element Value", SwingConstants.CENTER);
      valueEditorTitle.setFont (titleFont);
      valueEditorTitle.setForeground (titleColor);

      JPanel panel1 = new JPanel (new BorderLayout());
      panel1.add (valueEditorTitle, BorderLayout.NORTH);
      panel1.add (createValueInspectorComp(), BorderLayout.CENTER);
      panel1.add (new JLabel(" "), BorderLayout.SOUTH);
      add (panel1, BorderLayout.NORTH);

      if (m_attrInspectorComp != null)
      {
        JPanel panel2 = new JPanel (new BorderLayout());
        panel2.add (attrTitle, BorderLayout.NORTH);
        panel2.add (m_attrInspectorComp, BorderLayout.CENTER);
        add (panel2, BorderLayout.CENTER);
      }
    }
    else
    {
      if (m_attrInspectorComp != null)
      {
        add (attrTitle, BorderLayout.NORTH);
        add (m_attrInspectorComp, BorderLayout.CENTER);
      }
    }
  }

  //--------------------------------------------------------------------
  private Component createValueInspectorComp()
  {
    IdeInspectorPropertySetComponent propertySetComponent = new IdeInspectorPropertySetComponent (null);

    propertySetComponent.addProperty (new ValueInspectorProperty (m_elementInst), null);
    propertySetComponent.update();

    m_valueInspectorComp = new PropertyTableComponentUI (propertySetComponent);
    propertySetComponent.setService (UIComponentService.class, m_valueInspectorComp);

    JTable jtable = m_valueInspectorComp.getTable();
    Dimension d = jtable.getPreferredScrollableViewportSize();
    d.height = jtable.getRowHeight();
    jtable.setPreferredScrollableViewportSize (d);

    return m_valueInspectorComp.getUIComponent();
  }

  //--------------------------------------------------------------------
  private Component createAttrInspectorComp()
  {
    if (! m_elementInst.hasAttributes())
      return null;

    IdeInspectorPropertySetComponent propertySetComponent = new IdeInspectorPropertySetComponent (null);

    Enumeration attrEnum = m_elementInst.getAttributes();
    int countVisible = 0;
    while (attrEnum.hasMoreElements())
    {
      AttributeInst attrInst = (AttributeInst) attrEnum.nextElement();

      if (attrInst.getAttributeDef().isHidden())
        continue;

      propertySetComponent.addProperty (new ValueInspectorProperty (attrInst), null);
      countVisible ++;
    }

    if (countVisible == 0)
      return null;

    propertySetComponent.update();

    m_attrInspectorComp = new PropertyTableComponentUI (propertySetComponent);
    propertySetComponent.setService (UIComponentService.class, m_attrInspectorComp);

    return m_attrInspectorComp.getUIComponent();
  }

  //--------------------------------------------------------------------
  public void stopEditing()
  {
    if (m_valueInspectorComp != null)
      m_valueInspectorComp.stopCellEditing();

    if (m_attrInspectorComp != null)
      m_attrInspectorComp.stopCellEditing();
  }

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